android 入門-android自訂控制項

來源:互聯網
上載者:User

標籤:

第一種:繼承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=150,Canvas_H=150;    private static final int Rect_W=100,Rect_H=100;    private Bitmap bitmap;    private int bitmap_W,bitmap_H;    private int LT_X=0,LT_Y=0;    private int RT_X=100,RT_Y=0;    private int RB_X=100,RB_Y=100;    private int Bitmap_X=25,Bitmap_Y=25;    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, 0);        int srcRightTopId = attrs.getAttributeResourceValue(null, SrcRT, 0);        int srcRightBottomId = attrs.getAttributeResourceValue(null, SrcRB, 0);//        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)/2;        Bitmap_Y=(Canvas_H-bitmap_H)/2;        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(100);        canvas.drawRect(mRect,paint);        paint.setAlpha(255);        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);    }}

 

android 入門-android自訂控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.