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. python-网络-udp

    python-网络-udp 标签(空格分隔): python 开发环境:windows Pycharm+python3.* 工具:网络调试助手 UDP[client]-发送数据 from socket ...

  2. MySql创建指定字符集的数据库

    以创建字符集为utf8的数据库为例: CREATE DATABASE db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; CREAT ...

  3. C# App.config 自定义 配置节 出现的问题:配置系统未能初始化

    C# 读取app.config配置文件 节点键值,提示 "配置系统未能初始化" 错误的解决方案 新建C#项目,在app.config中添加了appSettings项,运行时出现&q ...

  4. iview2.0 父组件访问子组件 方法

    //从父组件中访问子组件 可以给子组件定义标识 通过ref="chead" 定义  通过父组件就可以访问了

  5. POJ 1144 Network【割顶】

    学习的这一篇:https://www.byvoid.com/blog/biconnect 割顶:对于无向图G,如果删除某个点u后,连通分量数目增加,称u为图的关节点或者割顶 u为割顶的条件: (1)u ...

  6. Linux 运维笔试题(一)

    试题:   1.说出下列服务对应的端口或者端口对应的服务 21  23  25  873  161  111  110  53  123  2049   2.文件atime,ctime,mtime的区 ...

  7. ORA-01555错误总结(二)

    这篇文章紧接着上一篇"ORA-01555错误总结(一)".主要描写叙述延迟块清除导致的ORA-01555错误. 以下是一些简介(上一遍已经介绍过) ORA-01555错误是一种在o ...

  8. DataTable转成Json

    /// <summary>         /// DataTable转成Json /// </summary>         /// <param name=&quo ...

  9. msiexec

    msiexec: runCmd = new String[]{ "msiexec", "/i", exeName, "/quiet", &q ...

  10. RAID信息存放位置!

    今天偶然的机会,客户打电话说有一台DELL T110的服务器换了主板电池RAID信息没了进不去系统了,问我怎么处理,T110的RAID是主板集成的S100的RAID卡(算是软RAID,通过BIOS配置 ...