Android进阶篇-onTouchEvent的使用
来源:互联网 发布:const是java关键字吗 编辑:程序博客网 时间:2024/06/08 12:02
这里通过演示一个可以拖动颜色球的例子来展示Android中onTouchEvent的使用以及自定义View。
实体类ColorBall:
/** * @author gongchaobin * * 实体类 颜色球 */public class ColorBall { private Bitmap img; //小球的图片 private int coordX = 0; //画布上的X坐标 private int coordY = 0; //画布上的Y坐标 private int id; private static int count = 1; private boolean goRight = true; private boolean goDown = true; public ColorBall(Context context, int drawable) {BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; img = BitmapFactory.decodeResource(context.getResources(), drawable); id=count;count++;}public ColorBall(Context context, int drawable, Point point) {BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; img = BitmapFactory.decodeResource(context.getResources(), drawable); id=count;count++;coordX= point.x;coordY = point.y;}public static int getCount() {return count;}void setX(int newValue) { coordX = newValue; }public int getX() {return coordX;}void setY(int newValue) { coordY = newValue; }public int getY() {return coordY;}public int getID() {return id;}public Bitmap getBitmap() {return img;}public void moveBall(int goX, int goY) {// check the borders, and set the direction if a border has reachedif (coordX > 270){goRight = false;}if (coordX < 0){goRight = true;}if (coordY > 400){goDown = false;}if (coordY < 0){goDown = true;}if (goRight){coordX += goX;}else{coordX -= goX;}if (goDown){coordY += goY;}else{coordY -= goY;}}}自定义DrawView:
/** * @author gongchaobin * * 自定义View */public class DrawView extends View { private ColorBall[] colorballs = new ColorBall[3]; private int balID = 0; public DrawView(Context context) { super(context); setFocusable(true); /**在画布上设置三个初始的点*/ Point point1 = new Point(); point1.x = 50; point1.y = 20; Point point2 = new Point(); point2.x = 100; point2.y = 20; Point point3 = new Point(); point3.x = 150; point3.y = 20; /**New三个颜色球*/ colorballs[0] = new ColorBall(context,R.drawable.bol_groen, point1); colorballs[1] = new ColorBall(context,R.drawable.bol_rood, point2); colorballs[2] = new ColorBall(context,R.drawable.bol_blauw, point3); } @Override protected void onDraw(Canvas canvas) { /**重绘颜色球*/ for (ColorBall ball : colorballs) { canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null); } } public boolean onTouchEvent(MotionEvent event) { int eventaction = event.getAction(); int X = (int)event.getX(); int Y = (int)event.getY(); switch (eventaction ) { /**当初始进来的时候 ,向下移动的状态*/ case MotionEvent.ACTION_DOWN: balID = 0; for (ColorBall ball : colorballs) { /**获取到小球的中心点*/ int centerX = ball.getX() + 25; int centerY = ball.getY() + 25; double radCircle = Math.sqrt( (double) (((centerX-X)*(centerX-X)) + (centerY-Y)*(centerY-Y))); /**如果移动的距离小于23,小球没动*/ if (radCircle < 23){ balID = ball.getID(); break; } } break; /**balId>0,小球的移动状态*/ case MotionEvent.ACTION_MOVE: if (balID > 0) { colorballs[balID-1].setX(X-25); colorballs[balID-1].setY(Y-25); } break; case MotionEvent.ACTION_UP: break; } /**invalidate方法 进行界面刷新重绘*/ invalidate(); return true; }}
- Android进阶篇-onTouchEvent的使用
- Android进阶篇-onTouchEvent的使用
- onTouchEvent的使用 onTouchEvent注意事项
- onTouchEvent方法的使用
- onTouchEvent方法的使用
- onTouchEvent方法的使用
- onTouchEvent方法的使用
- onTouchEvent方法的使用
- onTouchEvent方法的使用
- onTouchEvent方法的使用
- onTouchEvent方法的使用
- onTouchEvent方法的使用
- onTouchEvent方法的使用
- onTouchEvent方法的使用
- onTouchEvent方法的使用
- onTouchEvent方法的使用
- onTouchEvent方法的使用
- android学习笔记——onTouchEvent方法的使用
- 计算机达人成长之路之我爱数组(1)
- 数据仓库、商业智能的体系结构
- JDBC连接池与数据源学习总结
- SQL Server 日期的加减函数: DATEDIFF DATEADD
- C语言的位操作符
- Android进阶篇-onTouchEvent的使用
- explain的使用
- PHP自动提取关键字
- python list转字符换
- oracle 表空间
- Android菜单设计(1) : 使用xml文件布局创建 options menu
- 源码推荐: html解析for iOS
- boost之ptree学习(json)
- libmemcached-win32 工程编译方法及下载