第一种:继承View
实现自己的属性
<com.cc.imagewithmarkersample.MyView
android:id="@+id/myviewid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
SrcLT="@drawable/red"
SrcRT="@drawable/green"
SrcRB="@drawable/green"/>
public class MyView extends View { // private String mtext;
private int msrclt, msrcrt, msrcrb;
private static final String SrcLT = "SrcLT";
private static final String SrcRT = "SrcRT";
private static final String SrcRB = "SrcRB";
private static final int Canvas_W=,Canvas_H=;
private static final int Rect_W=,Rect_H=;
private Bitmap bitmap;
private int bitmap_W,bitmap_H;
private int LT_X=,LT_Y=;
private int RT_X=,RT_Y=;
private int RB_X=,RB_Y=;
private int Bitmap_X=,Bitmap_Y=;
private Rect mRect=null; public MyView(Context context) {
super(context);
} public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// int textId = attrs.getAttributeResourceValue(null, "Text",0);
int srcLeftTopId = attrs.getAttributeResourceValue(null, SrcLT, );
int srcRightTopId = attrs.getAttributeResourceValue(null, SrcRT, );
int srcRightBottomId = attrs.getAttributeResourceValue(null, SrcRB, );
// mtext = context.getResources().getText(textId).toString();
msrclt = srcLeftTopId;
msrcrt = srcRightTopId;
msrcrb = srcRightBottomId; mRect=new Rect(Bitmap_X,Bitmap_Y,Rect_W+Bitmap_X,Rect_H+Bitmap_Y);
} public void setImageBitmap(Bitmap bm) {
/** 获取图片宽高 **/
bitmap_W = bm.getWidth();
bitmap_H = bm.getHeight(); Bitmap_X=(Canvas_W-bitmap_W)/;
Bitmap_Y=(Canvas_H-bitmap_H)/; if (bitmap != bm) {
bitmap=bm;
} } @Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.GRAY); canvas.drawBitmap(bitmap, Bitmap_X, Bitmap_Y, paint); paint.setAlpha();
canvas.drawRect(mRect,paint); paint.setAlpha();
onDrawLt(canvas, paint);
onDrawRt(canvas, paint);
onDrawRb(canvas, paint); // canvas.drawText(mtext, bw / 2, 30, paint);
} private void onDrawLt(Canvas canvas, Paint paint) {
InputStream is = getResources().openRawResource(msrclt);
Bitmap mBitmap = BitmapFactory.decodeStream(is);
int bh = mBitmap.getHeight();
int bw = mBitmap.getWidth();
canvas.drawBitmap(mBitmap, LT_X, LT_Y, paint); } private void onDrawRt(Canvas canvas, Paint paint) {
InputStream is = getResources().openRawResource(msrcrt);
Bitmap mBitmap = BitmapFactory.decodeStream(is);
int bh = mBitmap.getHeight();
int bw = mBitmap.getWidth();
canvas.drawBitmap(mBitmap, RT_X, RT_Y, paint);
} private void onDrawRb(Canvas canvas, Paint paint) {
InputStream is = getResources().openRawResource(msrcrb);
Bitmap mBitmap = BitmapFactory.decodeStream(is);
int bh = mBitmap.getHeight();
int bw = mBitmap.getWidth();
canvas.drawBitmap(mBitmap, RB_X, RB_Y, paint);
}
}