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

[java] view plain copy

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

具体实现:

[java] view plain copy

  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

[java] view plain copy

  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>

[java] view plain copy

  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. }

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

时间: 2024-08-03 02:37:06

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

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

[java] view plain copy //设置颜色 public void setColour(int color){ co = color; } //设置边框宽度 public void setBorderWidth(int width){ borderwidth = width; } 具体实现: [java] view plain copy package xiaosi.imageborder; import android.app.Activity; import android.

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

<1>简介 之前往往是通过Bitmap.Drawable和Canvas配合完成,需要写一系列繁杂的逻辑去缩小原有图片,从而得到缩略图. 现在我给大家介绍一种比较简单的方法:(网上有) 在Android 2.2版本中,新增了一个ThumbnailUtils工具类来是实现缩略图,此工具类的功能是强大的,使用是简单,它提供了一个常量和三个方法.利用这些常数和方法,可以轻松快捷的实现图片和视频的缩略图功能. <2>ThumbnailUtils工具类 常量: OPTIONS_RECYCLE_

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

<1>简介 之前往往是通过Bitmap.Drawable和Canvas配合完成,需要写一系列繁杂的逻辑去缩小原有图片,从而得到缩略图. 现在我给大家介绍一种比较简单的方法:(网上有) 在Android 2.2版本中,新增了一个ThumbnailUtils工具类来是实现缩略图,此工具类的功能是强大的,使用是简单,它提供了一个常量和三个方法.利用这些常数和方法,可以轻松快捷的实现图片和视频的缩略图功能. <2>ThumbnailUtils工具类 常量: OPTIONS_RECYCLE_

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

Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java [java] view plaincopy package xiaosi.handWriting; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import andro

Android学习笔记进阶16之BitmapShader

<1>简介 具体的看一下博文:Android学习笔记进阶15之Shader渲染 public   BitmapShader(Bitmap bitmap,Shader.TileMode tileX,Shader.TileMode tileY) 调用这个方法来产生一个画有一个位图的渲染器(Shader). bitmap   在渲染器内使用的位图 tileX      The tiling mode for x to draw the bitmap in.   在位图上X方向花砖模式 tileY  

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

1.首先创建一个Bitmap图片,并指定大小: 2.在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可: 3.需要保存的目录File,注意如果写的目录如"/sdcard/song/"如果不存在的话,要先创建(file.mkdirs).. 4.需要添加权限:<uses-permission Android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> Canvas是一个画布,你可以建

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

1.首先创建一个Bitmap图片,并指定大小: 2.在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可: 3.需要保存的目录File,注意如果写的目录如"/sdcard/song/"如果不存在的话,要先创建(file.mkdirs).. 4.需要添加权限:<uses-permission Android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> Canvas是一个画布,你可以建

Android学习笔记:如何高效显示图片,避免内存溢出 和 ImageView无法显示大尺寸的图片

因为手机的内存资源是有限的,每个app可使用的内存是受限的.而现在采用高分辨率拍的照片往往很大.如果加载时不注意方法,很有可能会引起java.lang.OutofMemoryError: bitmap size exceeds VM budget. 异常而导致app奔溃退出. 另外ImageView支持的图片大小也是受限制的,比如整个App虽然只放一张图片,该图片大小也没超过整个app的内存上限.但该图片大小超过了ImageView的最大值,这也是有问题的.这时需要采取方法,在加载图片时缩小加载

Android学习笔记进阶15之Shader渲染

Android提供的Shader类主要是渲染图像以及一些几何图形. Shader有几个直接子类: BitmapShader    : 主要用来渲染图像 LinearGradient  :用来进行线性渲染 RadialGradient  : 用来进行环形渲染 SweepGradient   : 扫描渐变---围绕一个中心点扫描渐变就像电影里那种雷达扫描,用来梯度渲染. ComposeShader   : 组合渲染,可以和其他几个子类组合起来使用. 1   BitmapShader 渲染器着色一个位