1. //设置颜色
  2. public void setColour(int color){
  3. co = color;
  4. }
  5. //设置边框宽度
  6. public void setBorderWidth(int width){
  7. borderwidth = width;
  8. }

具体实现:

  1. package xiaosi.imageborder;
  2. import android.app.Activity;
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. public class ImageBorderActivity extends Activity {
  6. /** Called when the activity is first created. */
  7. private myImageView image = null;
  8. private myImageView image1 = null;
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.main);
  13. image = (myImageView)findViewById(R.id.iamge);
  14. image.setColour(Color.YELLOW);
  15. image.setBorderWidth(10);
  16. image1 = (myImageView)findViewById(R.id.iamge1);
  17. image1.setColour(Color.GREEN);
  18. image1.setBorderWidth(5);
  19. }
  20. }

main.xml

  1. <LinearLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:background="@drawable/playerbackground"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <xiaosi.imageborder.myImageView
  7. android:id="@+id/iamge"
  8. android:layout_width="200px"
  9. android:layout_height="230px"
  10. android:layout_alignParentRight="true"
  11. android:src="@drawable/v"
  12. android:layout_centerInParent="true"
  13. android:layout_marginRight="3px"
  14. />
  15. <xiaosi.imageborder.myImageView
  16. android:id="@+id/iamge1"
  17. android:layout_width="200px"
  18. android:layout_height="230px"
  19. android:layout_alignParentRight="true"
  20. android:src="@drawable/v"
  21. android:layout_centerInParent="true"
  22. android:layout_marginRight="3px"
  23. />
  24. </LinearLayout>
  1. package xiaosi.imageborder;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Paint;
  5. import android.graphics.Rect;
  6. import android.util.AttributeSet;
  7. import android.widget.ImageView;
  8. public class myImageView extends ImageView {
  9. private int co;
  10. private int borderwidth;
  11. public myImageView(Context context) {
  12. super(context);
  13. }
  14. public myImageView(Context context, AttributeSet attrs,
  15. int defStyle) {
  16. super(context, attrs, defStyle);
  17. }
  18. public myImageView(Context context, AttributeSet attrs) {
  19. super(context, attrs);
  20. }
  21. //设置颜色
  22. public void setColour(int color){
  23. co = color;
  24. }
  25. //设置边框宽度
  26. public void setBorderWidth(int width){
  27. borderwidth = width;
  28. }
  29. @Override
  30. protected void onDraw(Canvas canvas) {
  31. super.onDraw(canvas);
  32. // 画边框
  33. Rect rec = canvas.getClipBounds();
  34. rec.bottom--;
  35. rec.right--;
  36. Paint paint = new Paint();
  37. //设置边框颜色
  38. paint.setColor(co);
  39. paint.setStyle(Paint.Style.STROKE);
  40. //设置边框宽度
  41. paint.setStrokeWidth(borderwidth);
  42. canvas.drawRect(rec, paint);
  43. }
  44. }

源代码下载:点击打开链接

Android学习笔记进阶19之给图片加边框的更多相关文章

  1. Android学习笔记进阶19 之给图片加边框

    //设置颜色 public void setColour(int color){ co = color; } //设置边框宽度 public void setBorderWidth(int width ...

  2. Android学习笔记进阶20 之得到图片的缩略图

    <1>简介 之前往往是通过Bitmap.Drawable和Canvas配合完成,需要写一系列繁杂的逻辑去缩小原有图片,从而得到缩略图. 现在我给大家介绍一种比较简单的方法:(网上有) 在A ...

  3. Android学习笔记进阶20之得到图片的缩略图

    <1>简介 之前往往是通过Bitmap.Drawable和Canvas配合完成,需要写一系列繁杂的逻辑去缩小原有图片,从而得到缩略图. 现在我给大家介绍一种比较简单的方法:(网上有) 在A ...

  4. Android学习笔记进阶之在图片上涂鸦(能清屏)

    Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...

  5. Android学习笔记进阶16之BitmapShader

    <1>简介 具体的看一下博文:Android学习笔记进阶15之Shader渲染 public   BitmapShader(Bitmap bitmap,Shader.TileMode ti ...

  6. Android学习笔记进阶17之LinearGradient

    具体的看一下博文:Android学习笔记进阶15之Shader渲染 package xiaosi.BitmapShader; import android.app.Activity; import a ...

  7. Android学习笔记进阶18 之画图并保存图片到本地

    1.首先创建一个Bitmap图片,并指定大小:   2.在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可:   3.需要保存的目录File,注意如果写的目录如“/sdcard/so ...

  8. Android学习笔记进阶18之画图并保存图片到本地

    1.首先创建一个Bitmap图片,并指定大小:   2.在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可:   3.需要保存的目录File,注意如果写的目录如“/sdcard/so ...

  9. Android学习笔记(二)之异步加载图片

    最近在android开发中碰到比较棘手的问题,就是加载图片内存溢出.我开发的是一个新闻应用,应用中用到大量的图片,一个界面中可能会有上百张图片.开发android应用的朋友可能或多或少碰到加载图片内存 ...

随机推荐

  1. Spring Cloud学习笔记【八】服务网关 Zuul(过滤器)

    在上篇文章中我们了解了 Spring Cloud Zuul 作为网关所具备的最基本功能:路由(Router),下面我们将关注 Spring Cloud Zuul 的另一核心功能:过滤器(Filter) ...

  2. CSU1608: Particle Collider(后缀数组)

    Description In the deep universe, there is a beautiful planet named as CS on which scientists have d ...

  3. EBS TimeZone问题

    记录问题 :  http://blog.csdn.net/cymm_liu/article/details/29234919     --     Oracle 数据库中的 时间 时区 http:// ...

  4. Cordic 算法入门

    三角函数的计算是个复杂的主题,有计算机之前,人们通常通过查找三角函数表来计算任意角度的三角函数的值.这种表格在人们刚刚产生三角函数的概念的时候就已经有了,它们通常是通过从已知值(比如sin(π/2)= ...

  5. html关于不换行代码

    类似这种效果  这是基于a标签 不是用table实现的 <a style="display: inline-block;white-space:nowrap;width: 100px; ...

  6. 关于自适应的矩形css布局

    在面试中遇到的问题,做一个自适应的宽高2:1的矩形,这个问题考察了padding的细节,padding-bottom的百分比值,相对的是该盒模型的height,设置矩形之后,里面的内容如果太多会溢出, ...

  7. BZOJ 4034 线段树+DFS序

    思路: 先搞出来每个点的DFS序 (要有入栈和出栈两种状态的) 处理出来 线段树区间有多少入栈的和多少出栈的 加区间的时候就加(入-出)*wei 查前缀和 //By SiriusRen #includ ...

  8. POJ 3281 网络流

    题意: 思路: 网络流 重在建图- 建完了图 就一切都好说了 这道题 我的想法是 先把源点和所有的食品连上边 (容量为1) 再把食品和对应的奶牛连上边 (容量为1) 这个时候要拆点 因为每只奶牛只能才 ...

  9. Ubuntu16.04 “有线未托管”有线网络不可用问题解决

    Ubuntu16.04 “有线未托管”问题解决 电脑上安装的Ubuntu16.04 是通过先安装Ubuntu Server后在通过命令 sudo tasksel 安装的Gnome桌面环境,安装完成后发 ...

  10. Windows环境下VMware虚拟机的自启动与自动关机--命令行操作

    .设置开机免密登录系统 1. 按下Windows + R 组合键,输入“netplwiz”,点击回车. 2. 去除需要密码登录的勾. 3. 如果需要密码,输入密码,点击确认.   二.编辑vmware ...