单击图像时的图像动画

时间:2014-02-03 04:38:05

标签: android android-imageview android-animation

我有图像它在画布上向所有方向移动     我的要求是当我点击移动图像时它将方向改为顶部但我无法做到这一点,下面我会删除总代码,请帮帮我

animatedview.java:

public class AnimatedView extends ImageView{

    int c=0;
    static int count=0;
    private Context mContext;
    int x = 150;
    int y = 450;
    private float a,b;
    private int xVelocity = 20;
    private int yVelocity = 20;
    private Handler h;
    private final int FRAME_RATE = 25;
    BitmapDrawable ball;
    boolean touching;
    boolean dm_touched = false;
    float move=3;
    int bm_x = 0, bm_y = 0, bm_offsetx, bm_offsety,bm_w,bm_h;

    public AnimatedView(Context context, AttributeSet attrs)  {  
        super(context, attrs);  
        mContext = context;  
        h = new Handler();

    } 
    private Runnable r = new Runnable() {
        @Override
        public void run() {
            //Log.e("bharat","run called");
            if(touching = true)
            invalidate(); 
        }
    };


    @Override
    protected void onDraw(Canvas c) {  
        //Log.e("bharat","ondraw called");
        //int z= c.getHeight()/2;
        //Log.e("bharat","z is"+z);

        BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);  
        if (x<0 && y <0) {
            //x = this.getWidth()/2;
            y = c.getHeight()/2;


        } else {
            x += xVelocity;
            y += yVelocity;
            if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
                xVelocity = xVelocity*-1;
            }
            if (y >( this.getHeight() - ball.getBitmap().getHeight()) ||y <0) {
                yVelocity = yVelocity*-1;
            }
        }
        c.drawBitmap(ball.getBitmap(), x, y, null);  
       //Log.e("sarat",""+touching);
      if(touching){
         // Log.e("bharat","iftouch called called");
        h.postDelayed(r, FRAME_RATE);    
        bm_w=ball.getBitmap().getWidth();
        bm_h=ball.getBitmap().getHeight();
      }

    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Log.d("bharat","ontouch called");
        int touchType = event.getAction();

         switch(touchType){
            case MotionEvent.ACTION_MOVE: 
                a = event.getX();
                b = event.getY();
                touching = true;
               /* if (dm_touched) {
                    x = (int) a - bm_offsetx;
                    y = (int) b - bm_offsety;
                }*/
                //invalidate();
                break;

             case MotionEvent.ACTION_DOWN: 
                 //x and y give you your touch coordinates
                  a = event.getX();
                  b = event.getY();
                  touching = true;
                  //Log.d("bharat","action_down called");
                  Log.e("s",""+ a);
                  Log.e("s",""+ b);
                  Log.e("s",""+ x);
                  Log.e("s",""+ y);
                  Log.e("s",""+ bm_w);
                  Log.e("s",""+ bm_h);
                  if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) { 
                      count++;
                    MainActivity.setCount(count);
                      invalidate();
                      Log.i("bharat",""+count);
                     /* if(b>450)
                      {
                         // Toast.makeText(getContext(),"game over",Toast.LENGTH_LONG).show();
                      }*/
                    }
                  if (dm_touched) {
                      if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) { 
                      move+=2;
                        //x = (int) a - bm_offsetx;
                        y = (int)b-300;
                    }}
                   // dm_touched = true;
             case MotionEvent.ACTION_UP:c++;
                 a = event.getX();
                  b = event.getY();
                 if(a>x+20&&a<330&&b<=y+320&&b>y){
                 invalidate();}
                 if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) { 
                    Log.e("bharat","clicked");
                }

                default:

                    dm_touched = true;
                    //touching = true;

                }

                return true;
    }

    @Override
    public void destroyDrawingCache() {

        count=0;
        super.destroyDrawingCache();
    }
}

0 个答案:

没有答案