//自定义的View
public class MyView extends View {
private static final int WIDTH = 90;
private Rect rect = new Rect(0, 0, WIDTH, WIDTH);// 绘制矩形的区域
private int deltaX, deltaY;// 点击位置和图形边界的偏移量
private int moveX, moveY;
private static Paint paint = new Paint();// 画笔
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
paint = new Paint();
paint.setColor(Color.RED);// 填充红色
paint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
//super.onDraw(canvas);
canvas.drawCircle(rect.centerX(), rect.centerY(), WIDTH/2, paint);
// canvas.drawText("画圆:", 10, 20, p);// 画文本
//canvas.drawCircle(60, 20, 90, p);// 小圆
// p.setAntiAlias(true);// 设置画笔的锯齿效果。 true是去除,大家一看效果就明白了
// canvas.drawCircle(120, 20, 20, p);// 大圆
//
// canvas.drawText("画矩形:", 10, 80, p);
// p.setColor(Color.GRAY);// 设置灰色
// p.setStyle(Paint.Style.FILL);//设置填满
// canvas.drawRect(60, 60, 80, 80, p);// 正方形
// canvas.drawRect(60, 90, 160, 100, p);// 长方形
//
// canvas.drawText("画三角形:", 10, 200, p);
// // 绘制这个三角形,你可以绘制任意多边形
//
// Path path = new Path();
// path.moveTo(80, 200);// 此点为多边形的起点
// path.lineTo(120, 250);
// path.lineTo(80, 250);
// path.close(); // 使这些点构成封闭的多边形
// canvas.drawPath(path, p);
//
// //画圆角矩形
// p.setStyle(Paint.Style.FILL);//充满
// p.setColor(Color.LTGRAY);
// p.setAntiAlias(true);// 设置画笔的锯齿效
// canvas.drawText("画圆角矩形:", 10, 260, p);
// RectF oval3 = new RectF(80, 260, 200, 300);// 设置个新的长方形
// canvas.drawRoundRect(oval3, 20, 15, p);//第二个参数是x半径,第三个参数是y半径
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
if(!rect.contains(x, y)) {
return false;//没有在矩形上点击,不处理触摸消息
}
deltaX = x -rect.left;
deltaY = y -rect.top;
break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
Rect old = new Rect(rect);
//更新矩形的位置
rect.left = x - deltaX;
rect.top = y - deltaY;
rect.right = rect.left + WIDTH;
rect.bottom = rect.top + WIDTH;
old.union(rect);//要刷新的区域,求新矩形区域与旧矩形区域的并集
invalidate();//出于效率考虑,设定脏区域,只进行局部刷新,不是刷新整个view
break;
}
return true;//处理了触摸消息,消息不再传递
}
}