android自定义控件——深入篇

来源:互联网 发布:linux rm rf 编辑:程序博客网 时间:2024/05/16 10:08

Android软件之自定义控件【深入理解】

在android中的各式各样的控件有很多,但在实际开发中,系统提供给我们的控件有时候却不能满足我们的需求,这时我们就需要自定义一个控件。满足自己的需求。

下面就让我介绍一下如何进行自定义控件吧(●'◡'●)

首先,先写一个自定义的类,具体代码如下【里面的注释还是很详细滴( ̄▽ ̄)"】:

package com.example.myview;import android.R.integer;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Paint.Style;import android.util.AttributeSet;import android.widget.TextView;public class MyTextView extends TextView {private int color;private int size;private String text;private Paint paint;// 构造方法的实现public MyTextView(Context context) {this(context, null);}public MyTextView(Context context, AttributeSet attrs) {this(context, attrs, -1);}public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.MyTextView);if (array != null) {text = (String) array.getText(R.styleable.MyTextView_text);color = array.getColor(R.styleable.MyTextView_color, Color.BLACK);size = (int) array.getDimension(R.styleable.MyTextView_size,getResources().getDisplayMetrics().density);setText(text);setTextSize(size);setTextColor(color);}// 设置控件的内边距setPadding(4, 4, 4, 4);initPaint();array.recycle();}// 对画板进行设置@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubsuper.onDraw(canvas);canvas.drawColor(Color.RED);// 设置字体在画板的位置// paint.descent() - paint.ascent() - getPaddingTop()使字体位于画板的中央canvas.drawText(text, getPaddingLeft(),paint.descent() - paint.ascent() - getPaddingTop(), paint);}// 定义一个方法,对画笔的各属性进行设置private void initPaint() {paint = new Paint();paint.setColor(color);paint.setTextSize(size);paint.setAntiAlias(true);paint.setStyle(Style.STROKE);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// TODO Auto-generated method stub// 计算控件的大小// 规定一个变量type 若为1 ,计算宽的大小,若为2,计算长的大小super.onMeasure(widthMeasureSpec, heightMeasureSpec);// 设置所测量控件的大小setMeasuredDimension(meaSure(widthMeasureSpec, 1),meaSure(heightMeasureSpec, 2));}public int meaSure(int spec, int type) {// 首先获取定义的类型int mode = MeasureSpec.getMode(spec);// 获取系统默认的大小int whsize = MeasureSpec.getSize(spec);switch (mode) {// 包裹内容case MeasureSpec.AT_MOST:int padding = 0;int measureText = 0;if (type == 1) {padding = getPaddingLeft() + getPaddingRight();measureText = (int) paint.measureText(text);} else {padding = getPaddingBottom() + getPaddingTop();measureText = (int) (paint.descent() - paint.ascent());}// 包裹文字宽的实际长度whsize = padding + measureText;break;default:break;}return whsize;}}
然后呢,就需要在res文件夹下的value文件夹中新建一个XML文件,具体代码如下:

<?xml version="1.0" encoding="utf-8"?><resources>     <declare-styleable name="MyTextView">        <attr name="text" format="string"></attr>        <attr name="size" format="dimension"></attr>        <attr name="color" format="color"></attr>    </declare-styleable></resources>

然后就是在MainActivity.XML中进行各属性的设值了。代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"    android:layout_height="match_parent"    >    <com.example.myview.MyTextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        app:text="自定义控件"        app:size="20sp"        app:color="#00ff00"        /></RelativeLayout>
其实,相对于其他知识点来说,自定义控件还是比较简单的。可能在计算控件的高度那块,对于逻辑思维的要求还是很强的。

如有问题或者建议什么的,可以提出,我一定虚心向各位前辈学习。。。O(∩_∩)O


1 0
原创粉丝点击