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. Reading and writing

    A text file is a sequence of characters stored on a permanent medium like a hard drive, flash memory ...

  2. BZOJ 1588 平衡树 模板题

    Treap: //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; int si ...

  3. AngularJs轻松入门源码托管至Github

    Github是全球最大的代码托管平台,笔者玩Github有一段时间了,有很多开源项目的源码都托管在Github上,笔者在上面也发现了不少优秀的开源代码. 每次写完博文想在最后附上文章相关的代码,但是由 ...

  4. error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead解决方案

    vs2012使用c语言函数fopen操作文件时报错: 错误 1 error C4996: 'fopen': This function or variable may be unsafe. Consi ...

  5. 集合HashSet的使用

    集合中的HashSet底层是通过Hash表实现,HashSet的特点是元素唯一,但用到Hash表就跟hashCode()有了密不可分的联系,所以HashSet的唯一性是通过hashCode()方法来保 ...

  6. mysql 导出到 mongodb 与快速插入测试数据

    快速插入数据: 因为 MongoDB 的底层引擎是 JS 引擎,所以完全可以使用一些 Js 的语法.   for(var i=0;i<10000;i++){    db.ceshi.insert ...

  7. (转载)详细图解mongodb下载、安装、配置与使用

    记得在管理员模式下运行CMD,否则服务将启动失败 转载:http://blog.csdn.net/boby16/article/details/51221474 详细图解,记录 win7 64 安装m ...

  8. Chrome开启无界面浏览模式Python+Windows环境

    环境:Python 3.5.x + Selenium 3.4.3 + Chromedriver 2.30 + Chrome 60 beta版 + WIN7/WIN10 chrome_options = ...

  9. Maven的SSH搭建以及部署

    本人有点傻,研究Maven研究了有一段时间,刚刚有些入门,记录下来方便以后使用 工作环境:jdk7 myeclipse10 maven3.1.1 1 下载maven3.1.1 http://maven ...

  10. F - Humidex(1.4.2)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Descr ...