日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 入门-android自定义控件

發布時間:2023/12/13 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 入门-android自定义控件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一種:繼承View

實現自己的屬性

<com.cc.imagewithmarkersample.MyViewandroid: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;}}@Overrideprotected 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);} }

?

轉載于:https://www.cnblogs.com/luquanmingren/p/4892737.html

總結

以上是生活随笔為你收集整理的android 入门-android自定义控件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。