0
我有一個animatedview.java啓動時,它看起來像後續圖像圖像動畫點擊圖像
我是儘量使這個應用程序爲跟蹤要求 這就是我想在圖像上單擊,然後只將開始動畫,否則它會等待我的點擊,這個我寫這樣的代碼,
Animatedview.java
public class AnimatedView extends ImageView{
static int count=0;
private Context mContext;
int x = 150;
int y = 450;
private float a,b;
private int xVelocity = 10;
private int yVelocity = 15;
private Handler h;
private final int FRAME_RATE = 25;
BitmapDrawable ball;
boolean touching;
boolean dm_touched = false;
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() {
if(touching = true)
invalidate();
}
};
@Override
protected void onDraw(Canvas c) {
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){
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;
}
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");
if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) {
count++;
Log.i("bharat",""+count);
}
dm_touched = true;
case MotionEvent.ACTION_UP:
default:
dm_touched = true;
touching = true;
}
return true;
}
}
但是,我不能讓我的要求,請幫我
通過使用XML風格我無法做更遠的動畫,請你鍛鍊我的代碼 – vishnub1626