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
- Android自定义控件初学
- 初学android-自定义控件以及控件属性
- android初学------自定义进度条
- Android初学之自定义土司
- [Android自定义控件] Android自定义控件
- Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- Android 自定义控件 单页翻书控件
- android虚线控件---自定义控件
- android自定义控件实例 --控件
- Android自定义控件--组合控件
- android 控件 自定义组合控件
- 【android自定义控件】ProgressBar自定义
- Android自定义控件 自定义属性
- 第三章 46题
- 排列之和 UVa11076
- mysql 5.5 更改默认字符集
- Java,Android类加载顺序真正详解
- eclipse不自动弹出提示(alt+/快捷键失效)
- Android自定义控件初学
- 设置button图像内间距:上左下右
- 如何使用java中的数组
- JQuery find 获取 带命名空间 匹配元素值
- Android SharedPreferences 使用
- LeetCode的medium题集合(C++实现)十
- 所有节点对最短路径 超时 优先队列 + dijkstra + 遍历前驱子图
- JAXBContext解析XML集合对象
- Spring事务管理—aop:pointcut expression解析