900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > [Android自定义控件] Android自定义控件

[Android自定义控件] Android自定义控件

时间:2022-06-11 16:42:01

相关推荐

[Android自定义控件] Android自定义控件

public class MyView extends View { private String mtext; private int msrc; public MyView(Context context) { super(context); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); int resourceId = 0; int textId = attrs.getAttributeResourceValue(null, "Text",0); int srcId = attrs.getAttributeResourceValue(null, "Src", 0); mtext = context.getResources().getText(textId).toString(); msrc = srcId; } @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.RED); InputStream is = getResources().openRawResource(msrc); Bitmap mBitmap = BitmapFactory.decodeStream(is); int bh = mBitmap.getHeight(); int bw = mBitmap.getWidth(); canvas.drawBitmap(mBitmap, 0,0, paint); //canvas.drawCircle(40, 90, 15, paint); canvas.drawText(mtext, bw/2, 30, paint); }}

布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.example.myimageview2.MyView android:id="@+id/myView1" android:layout_width="wrap_content" android:layout_height="wrap_content" Text="@string/hello_world" Src="@drawable/xh"/></LinearLayout>

属性Text, Src在自定义View类的构造方法中读取。

2)通过XML为View注册属性。与Android提供的标准属性写法一样。

案例: 实现一个带文字说明的ImageView (ImageView+TextView组合,文字说明,可在布局文件中设置位置)

public class MyImageView extends LinearLayout { public MyImageView(Context context) { super(context); } public MyImageView(Context context, AttributeSet attrs) { super(context, attrs); int resourceId = -1; TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyImageView); ImageView iv = new ImageView(context); TextView tv = new TextView(context); int N = typedArray.getIndexCount(); for (int i = 0; i < N; i++) { int attr = typedArray.getIndex(i); switch (attr) { case R.styleable.MyImageView_Oriental: resourceId = typedArray.getInt( R.styleable.MyImageView_Oriental, 0); this.setOrientation(resourceId == 1 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL); break; case R.styleable.MyImageView_Text: resourceId = typedArray.getResourceId( R.styleable.MyImageView_Text, 0); tv.setText(resourceId > 0 ? typedArray.getResources().getText( resourceId) : typedArray .getString(R.styleable.MyImageView_Text)); break; case R.styleable.MyImageView_Src: resourceId = typedArray.getResourceId( R.styleable.MyImageView_Src, 0); iv.setImageResource(resourceId > 0 ?resourceId:R.drawable.ic_launcher); break; } } addView(iv); addView(tv); typedArray.recycle(); }}

attrs.xml进行属性声明, 文件放在values目录下

<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MyImageView"> <attr name="Text" format="reference|string"></attr> <attr name="Oriental" > <enum name="Horizontal" value="1"></enum> <enum name="Vertical" value="0"></enum> </attr> <attr name="Src" format="reference|integer"></attr> </declare-styleable></resources>

MainActivity的布局文件:先定义命名空间xmlns:uview="/apk/res/com.example.myimageview2" (com.example.myimageview2为你

在manifest中定义的包名)

然后可以像使用系统的属性一样使用:uview:Oriental="Vertical"

<LinearLayout xmlns:android="/apk/res/android" xmlns:uview="/apk/res/com.example.myimageview2" xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <com.example.myimageview2.MyImageView android:id="@+id/myImageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" uview:Text="这是一个图片说明" uview:Src="@drawable/tw" uview:Oriental="Vertical"> </com.example.myimageview2.MyImageView></LinearLayout>

四、控件绘制 onDraw()

五、

六:自定义View的方法

onFinishInflate() 回调方法,当应用从XML加载该组件并用它构建界面之后调用的方法

onMeasure() 检测View组件及其子组件的大小

onLayout() 当该组件需要分配其子组件的位置、大小时

onSizeChange() 当该组件的大小被改变时

onDraw() 当组件将要绘制它的内容时

onKeyDown 当按下某个键盘时

onKeyUp 当松开某个键盘时

onTrackballEvent 当发生轨迹球事件时

onTouchEvent 当发生触屏事件时

onWindowFocusChanged(boolean) 当该组件得到、失去焦点时

onAtrrachedToWindow() 当把该组件放入到某个窗口时

onDetachedFromWindow() 当把该组件从某个窗口上分离时触发的方法

onWindowVisibilityChanged(int): 当包含该组件的窗口的可见性发生改变时触发的方法

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。