Android自定义控件初学

来源:互联网 发布:脚本级网络游戏编程 编辑:程序博客网 时间:2024/05/18 00:53

Android自定义控件初学

花了半个小时学了下自定义控件

自定义控件参考 http://blog.csdn.net/cym492224103/article/details/38438727 不再赘述
Paint属性参考 http://blog.csdn.net/xanxus46/article/details/7883554

控件代码

package com.example.customwidget;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.RectF;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.View;public class MyVolumeWidget extends View{    private static final String TAG = "MyVolumeWidget";    private Paint mPaint;    private float strokeWidth=5;    private RectF rect;     // 控件宽高      private int w;      private int h;    public MyVolumeWidget(Context context) {        super(context);        // TODO Auto-generated constructor stub        initView();    }    public MyVolumeWidget(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        // TODO Auto-generated constructor stub        initView();    }    public MyVolumeWidget(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub        initView();    }    private void initView() {        // TODO Auto-generated method stub        mPaint = new Paint();        mPaint.setAntiAlias(true);        mPaint.setStrokeWidth(strokeWidth);        mPaint.setStrokeCap(Paint.Cap.ROUND);        mPaint.setStyle(Paint.Style.STROKE);        rect = new RectF(0,0,100,100);    }    @Override    protected void onDraw(Canvas canvas) {        // TODO Auto-generated method stub        super.onDraw(canvas);        Log.d(TAG,"onDraw(Canvas canvas)");        canvas.drawRect(rect, mPaint);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        // TODO Auto-generated method stub        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        w = resolveSize(100, widthMeasureSpec);          h = resolveSize(100, heightMeasureSpec);         setMeasuredDimension(w, h);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        // TODO Auto-generated method stub        Log.d(TAG,"onTouchEvent=>" + event.getAction());        switch(event.getAction()){        case MotionEvent.ACTION_DOWN:            mPaint.setColor(Color.BLUE);            break;        case MotionEvent.ACTION_UP:            mPaint.setColor(Color.GRAY);            break;        }        invalidate();        return true;    }}

注意点

系统默认调用两个形参的构造函数View(Context context, AttributeSet attrs)

最好在每个构造函数里调用初始化函数,本文是initView()函数。在这里面建好Panit和需要的类,不要在onDraw()里面调用。因为onDraw()函数会频繁调用,在里面新建类开销大。

onTouchEvent(MotionEvent event)里面写上触摸后动作。如果需要改变外观,需要在最后调用invalidate();函数。它会自动调用onDraw()函数,否则外观不刷新。

经测试当此函数返回值是return super.onTouchEvent(event);时,在一次触摸中只能识别第一个动作,即MotionEvent.ACTION_DOWN ,之后的滑动以及MotionEvent.ACTION_UP 无法检测。需要把返回改为return true;

onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法里面需要调用resolveSize()方法来获得正确的大小,不太懂。

按下的颜色
按下的颜色

松开的颜色
松开的颜色

0 0
原创粉丝点击