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. MapReduce JOB 的输出与输出笔记。

    提高 MapReduce 价值,自定义输入和输出. 比如跳过存储到 HDFS 中这个耗时的布置. 而只是从原始数据源接受数据,或者直接将数据发送给某些处理程序. 这些处理程序在 MapReduce 作 ...

  2. ArcGIS api for javascript——地理处理任务-服务区域任务

    描述 本例展示了如何发送一些输入值到ArcGIS Server上的GIS模型并获得结果反馈给web应用.本例中,模型计算自在地图上单击点起始的驱动时间多边形. 例如,这个分析类型可用于想要知道多少人生 ...

  3. [Python] Generating random numbers using numpy lib

    import numpy as np def test_run(): data=np.random.random((3,4)) """ [[ 0.80150549 0.9 ...

  4. Java 中的事件监听机制

    看项目代码时遇到了好多事件监听机制相关的代码.现学习一下: java事件机制包含三个部分:事件.事件监听器.事件源. 1.事件:继承自java.util.EventObject类,开发人员自己定义. ...

  5. CCNP路由实验之十 组播(多播)

                        CCNP路由实验之十 组播(多播) 种方法: 在交换机上配置静态的多播MAC地址到用户接口的映射 使用CGMP.执行CGMP的多播路由器能够将用户发送给自己 ...

  6. leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

    1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...

  7. POJ 3974 Manacher算法(模板)

    Manacher模板题 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm&g ...

  8. UltraEdit Companion Utility

    UltraEdit Companion Utility 配色组件 http://www.danielwmoore.com/extras/index.php?action=downloads;sa=vi ...

  9. Python正则表达式初识(二)

    前几天给大家分享了Python正则表达式初识(一),介绍了正则表达式中的三个特殊字符“^”.“.”和“*”,感兴趣的伙伴可以戳进去看看,今天小编继续给大家分享Python正则表达式相关特殊字符知识点. ...

  10. DEDECMS教程:列表页缩略图随机调用

    如果用过DEDECMS的朋友应该都知道,有些模板列表页面需要用到缩略图,调用内容中的缩略图可以使用系统自带的脚本调用第一张图片.但是,并不是我们所有的内容里都有图片,有时候第一张图片也不一定是适合尺寸 ...