自定义带边框imageView

时间:2022-07-07 21:26:38

首先,重写ImageView。

[java] view plaincopyprint?
  1. package org.xxx.picUpload.util;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.graphics.Rect;
  7. import android.util.AttributeSet;
  8. import android.widget.ImageView;
  9. public class MyImageViewextends ImageView {
  10. public MyImageView(Context context) {
  11. super(context);
  12. // TODO Auto-generated constructor stub
  13. }
  14. public MyImageView(Context context, AttributeSet attrs,
  15. int defStyle) {
  16. super(context, attrs, defStyle);
  17. // TODO Auto-generated constructor stub
  18. }
  19. public MyImageView(Context context, AttributeSet attrs) {
  20. super(context, attrs);
  21. // TODO Auto-generated constructor stub
  22. }
  23. @Override
  24. protected void onDraw(Canvas canvas) {//这里就是重写的方法了,想画什么形状自己动手
  25. // TODO Auto-generated method stub
  26. super.onDraw(canvas);
  27. // 画边框
  28. Rect rec = canvas.getClipBounds();
  29. rec.bottom--;
  30. rec.right--;
  31. Paint paint = new Paint();
  32. paint.setColor(Color.GRAY); //颜色
  33. paint.setStyle(Paint.Style.STROKE);
  34. paint.setStrokeWidth(5);
  35. canvas.drawRect(rec, paint);
  36. }
  37. }



然后 在xml文件中直接引用。

[html] view plaincopyprint?
  1. <org.xxx.picUpload.util.MyImageView
  2. android:id="@+id/imageView"
  3. android:layout_width="400px"
  4. android:layout_height="200px"/>


有图有真相(图片预览):

自定义带边框imageView



原地址:http://blog.csdn.net/baolong47/article/details/7301160