自定义view需要继承View类,重写两个构造方法
//用在代码new该view对象,初始化
public MyView(Context context) {
super(context);
init();
}
//一般添加构造---》view放进布局,系统实例化
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}绘制view需要使用Paint对象,相当于画笔。初始化需要给这个画笔设置一些属性。
对Paint实例化,注释写的很清楚了,有三个常用的初始化方法:
private void init() {
paint=new Paint();
//设置画笔的颜色
paint.setColor(Color.GREEN);
//抗锯齿,画图质量提高
paint.setAntiAlias(true);
//设置画笔样式,空心
paint.setStyle(Style.STROKE);
}要画图形,最起码要有三个对象:1.颜色对象 Color2.画笔对象 Paint3.画布对象 Canvas
为了绘制自定义View,需要重写onDraw()方法,参数:(Canvas canvas)是个画布对象,就是在这个canvas绘制的。
以下展示了绘制矩形,圆形,文本,图片,直线,弧线的过程,其中弧线的绘制过程依赖于矩形,这个有点有别于其他的。
//声明矩形对象(为了绘制弧线)
private RectF rectF;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
rectF=new RectF(10, 10, 70, 60);//实例化矩形
//画矩形
<span style="white-space:pre"> </span>//canvas.drawRect(10, 10, 70, 60, paint);两种方式,如果第一句没有声明RecF,就要按多个参数的构造方法绘制。<span style="white-space:pre"> </span>
canvas.drawRect(rectF, paint);
//画弧线
canvas.drawArc(rectF, 0, 290, true, paint);
//画圆:参数1、2:圆心的坐标 参数3:半径
canvas.drawCircle(100, 100, 30, paint);
//画图片<span style="white-space:pre"> </span>图片资源<span style="white-space:pre"> </span>图片左上角的坐标(X,Y)
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), 200, 200, paint);
//画文本<span style="white-space:pre"> </span>文本左上角的坐标
canvas.drawText("nihaohao", 200, 300, paint);
//画线<span style="white-space:pre"> </span>起点坐标 重点坐标 paint对象
canvas.drawLine(205, 305, 255, 355, paint);
}Shape对象的绘制
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:width="60px"
android:height="60px"></size>
<stroke
android:color="#ff0000"
android:width="2px"></stroke>
<gradient
android:startColor="#ff0000"
android:centerColor="#00ff00"
android:endColor="#0000ff"></gradient>
</shape>
private void init() {
drawable = getResources().getDrawable(R.drawable.cir);
}ondraw()方法里,注释已经很清楚了
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 设置 drawable bounds 指定 x,y
Rect bounds = drawable.getBounds();
bounds.left = (int)x;
bounds.top = (int)y;
bounds.right = bounds.left + 60;//只是保证离边界有一定距离
bounds.bottom =bounds.top + 60;
drawable.setBounds(bounds);
//画到canvas上
drawable.draw(canvas);
}init()方法里
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
onDraw()方法里,需要注意的是,drawBitmap()方法里传的参数,参数一:bitmap对象参数二:bitmap左上角x坐标 参数三:bitmap左上角y坐标。所以如果想要坐标指定在图片中间,需要x-bitmap.getWidth()/2, y-bitmap.getHeight()/2。这样做的好处是:在onTouchEvent()事件中,定义的x,y坐标可以定在图片中间,完成一定的手势动作。
canvas.drawBitmap(bitmap, x-bitmap.getWidth()/2, y-bitmap.getHeight()/2, paint);
这个方法响应了触摸屏幕发生的事件,包括按下、抬起、移动等等,都可以被监听到。
可以实现一定的拖动,绘图功能。通过event能get到这些动作和当前X,Y坐标等等。
public boolean onTouchEvent(MotionEvent event) {
//这个方法里往往需要重绘界面,使用这个方法可以自动调用onDraw()方法。(主线程)
invalidate();
//非UI线程用
postInvalidate();
//使系统响应事件,返回true
return true;
} public boolean onTouchEvent(MotionEvent event) {
//获取手指在屏幕上的坐标
float x = event.getX();
float y = event.getY();
//获取手指的操作--》按下、移动、松开
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN://按下
Log.i(TAG, "ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE://移动
Log.i(TAG, "ACTION_MOVE");
break;
case MotionEvent.ACTION_UP://松开
Log.i(TAG, "ACTION_UP");
break;
}
return true;
}
public class WritePadView extends View{
//视图放置在布局中,,,实例化
public WritePadView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
//代码new控件
public WritePadView(Context context) {
super(context);
init();
}
private void init() {
linesX=new ArrayList<ArrayList<Float>>();
linesY=new ArrayList<ArrayList<Float>>();
paint=new Paint();
//设置颜色
paint.setColor(Color.GREEN);
//设置抗锯齿
paint.setAntiAlias(true);
//设置画笔的样式
paint.setStyle(Style.FILL);
}
//绘制视图
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for(int j=0;j<linesX.size();j++){
//连点成线 3
for(int i=0;i<linesX.get(j).size()-1;i++){
canvas.drawLine(linesX.get(j).get(i), linesY.get(j).get(i),linesX.get(j).get(i+1), linesY.get(j).get(i+1), paint);
}
}
}
/**两个集合--》记录手指的轨迹的点---》一个线段*/
private ArrayList<Float> xs , ys;
private Paint paint;
//装所有线段的集合
private List<ArrayList<Float>> linesX,linesY;
//手指触摸屏幕
@Override
public boolean onTouchEvent(MotionEvent event) {
//获取手指的位置
float x = event.getX();
float y = event.getY();
if(event.getAction()==MotionEvent.ACTION_DOWN){//按下--》一个新的线段开始了
xs=new ArrayList<Float>();
ys=new ArrayList<Float>();
linesX.add(xs);
linesY.add(ys);
}
//把手指的位置记录下来
xs.add(x);
ys.add(y);
//通知视图重绘制、
invalidate();
return true;
}
}
在布局xml里,使用自定义视图要用 包名.类名引用。
<com.bless.myViewDemo.MyView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Paint:
void setARGB(int a, int r, int g, int b) 设置Paint对象颜色,参数一为alpha透明通道
void setAlpha(int a) 设置alpha不透明度,范围为0~255
void setAntiAlias(boolean aa) //是否抗锯齿
void setColor(int color) //设置颜色,这里Android内部定义的有Color类包含了一些常见颜色定义
void setFakeBoldText(boolean fakeBoldText) //设置伪粗体文本
void setLinearText(boolean linearText) //设置线性文本
PathEffect setPathEffect(PathEffect effect) //设置路径效果
Rasterizer setRasterizer(Rasterizer rasterizer) //设置光栅化
Shader setShader(Shader shader) //设置阴影
void setTextAlign(Paint.Align align) //设置文本对齐
void setTextScaleX(float scaleX) //设置文本缩放倍数,1.0f为原始
void setTextSize(float textSize) //设置字体大小
Typeface setTypeface(Typeface typeface) //设置字体,Typeface包含了字体的类型,粗细,还有倾斜、颜色等。
void setUnderlineText(boolean underlineText) //设置下划线
android 自定义view,绘制与onTouchEvent事件(一)
原文:http://blog.csdn.net/bless2015/article/details/46013329