Tecnologia do Blogger.
RSS

Re: [androidbrasil-dev] Mudar imagem ao clicar

No DOWN você muda e no UP você volta pra anterior.




Thiago


2012/3/6 Sérgio Fantin <sergio.lnd.fantin@gmail.com>
Pessoal,

desculpa a ignorância, mas sou dummie no negócio. Hehe...

Esse código faz um drag em uma bolinha.

Quero mudar de imagem ao posicionar o dedo na mesma e ao retirar o dedo da imagem quero que a imagem original apareça.

Qual a ideia?

public class DrawView extends View {
  
   private ColorBall ball;
   private int balID = 0;
   
    public DrawView(Context context) {
        super(context);
        setFocusable(true);
       
        // setting the start point for the balls
        Point point1 = new Point();
        point1.x = 50;
        point1.y = 20;
          
        ball = new ColorBall(context, R.drawable.ball1, point1);
    }
   
    // the method that draws the balls
    @Override protected void onDraw(Canvas canvas) {
        //canvas.drawColor(0xFFCCCCCC);     //if you want another background color      
       
        //draw the balls on the canvas
        canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
    }
   
    // events when touching the screen
    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: // touch down so check if the finger is on a ball
            balID = 0;
           
            // 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;
            }

            break;

        case MotionEvent.ACTION_MOVE:   // touch drag with the ball
            // move the balls the same as the finger
            if (balID > 0) {
                ball.setX(X-25);
                ball.setY(Y-25);
            }
           
            break;

        case MotionEvent.ACTION_UP:
               // touch drop - just do things here after dropping

             break;
        }
       
        // redraw the canvas
        invalidate();
        return true;
   
    }
}

Abraços...
--
Sérgio Fantin
http://serjaum.wordpress.com



--
Thiago Rosa

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comentários:

Postar um comentário