Android的拖拉功能-彩球示例

public class ColorBall { private Bitmap img; // the image of the ball private int coordX = 0; // the x coordinate at the canvas private int coordY = 0; // the y coordinate at the canvas private int id; // gives every ball his own id, for now not necessary 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 reached if (coordX > 270){ goRight = false; } if (coordX < 0){ goRight = true; } if (coordY > 400){ goDown = false; } if (coordY < 0){ goDown = true; } // move the x and y if (goRight){ coordX += goX; }else { coordX -= goX; } if (goDown){ coordY += goY; }else { coordY -= goY; } } }

public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new DrawView(this)); } }

public class DrawView extends View { // array that holds the balls private ColorBall[] colorballs = new ColorBall[3]; // variable to know what ball is being dragged private int balID = 0; public DrawView(Context context) { super(context); setFocusable(true); //necessary for getting the touch events // setting the start point for the balls Point point1 = new Point(); point1.x = 50; point1.y = 20; Point point2 = new Point(); point2.x = 100; point2.y = 40; Point point3 = new Point(); point3.x = 150; point3.y = 60; // declare each ball with the ColorBall class 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); } // the method that draws the balls @Override protected void onDraw(Canvas canvas) { //if you want another background color //canvas.drawColor(0xFFCCCCCC); //draw the balls on the canvas for (ColorBall ball : colorballs) { canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null); } } // events when touching the screen public boolean onTouchEvent(MotionEvent event) { int X = (int)event.getX(); int Y = (int)event.getY(); switch (event.getAction() ) { // touch down so check if the finger is on a ball case MotionEvent.ACTION_DOWN: balID = 0; for (ColorBall ball : colorballs) { // check if inside the bounds of the ball (circle) // get the center for the ball int centerX = ball.getX() + 25; int centerY = ball.getY() + 25; // calculate the radius from the touch to the center of the ball double radCircle = Math.sqrt( (double) (((centerX-X)*(centerX-X)) + (centerY-Y)*(centerY-Y))); // if the radius is smaller then 23 (radius of a ball is 22), then it must be on the ball if (radCircle < 23) { balID = ball.getID(); break; } // check all the bounds of the ball (square) //if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){ // balID = ball.getID(); // break; //} } break; // touch drag with the ball case MotionEvent.ACTION_MOVE: // move the balls the same as the finger if (balID > 0) { colorballs[balID-1].setX(X-25); colorballs[balID-1].setY(Y-25); } break; case MotionEvent.ACTION_UP: // touch drop - just do things here after dropping break; } // redraw the canvas invalidate(); return true; } }

效果演示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值