首先,重写ImageView。
- package org.xxx.picUpload.util;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.util.AttributeSet;
- import android.widget.ImageView;
- public class MyImageViewextends ImageView {
- public MyImageView(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- }
- public MyImageView(Context context, AttributeSet attrs,
- int defStyle) {
- super(context, attrs, defStyle);
- // TODO Auto-generated constructor stub
- }
- public MyImageView(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- }
- @Override
- protected void onDraw(Canvas canvas) {//这里就是重写的方法了,想画什么形状自己动手
- // TODO Auto-generated method stub
- super.onDraw(canvas);
- // 画边框
- Rect rec = canvas.getClipBounds();
- rec.bottom--;
- rec.right--;
- Paint paint = new Paint();
- paint.setColor(Color.GRAY); //颜色
- paint.setStyle(Paint.Style.STROKE);
- paint.setStrokeWidth(5);
- canvas.drawRect(rec, paint);
- }
- }
package org.xxx.picUpload.util;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ImageView;
public class MyImageView extends ImageView {
public MyImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyImageView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) { //这里就是重写的方法了,想画什么形状自己动手
// TODO Auto-generated method stub
super.onDraw(canvas);
// 画边框
Rect rec = canvas.getClipBounds();
rec.bottom--;
rec.right--;
Paint paint = new Paint();
paint.setColor(Color.GRAY); //颜色
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
canvas.drawRect(rec, paint);
}
}
然后 在xml文件中直接引用。
- <org.xxx.picUpload.util.MyImageView
- android:id="@+id/imageView"
- android:layout_width="400px"
- android:layout_height="200px"/>
<org.xxx.picUpload.util.MyImageView
android:id="@+id/imageView"
android:layout_width="400px"
android:layout_height="200px"/>
有图有真相(图片预览):
原地址:http://blog.csdn.net/baolong47/article/details/7301160