我有一个类,即animatedview.java。这个类给出了一个动画,比如,图像从下到上,从上到下移动。我要求我想将图像从中间和底部的底部移动到中点,如下面的屏幕!
我希望以后能够达到这个位置
mycode是:
公共类AnimatedView扩展了ImageView {
static int count=0;
private Context mContext;
int x = 150;
int y = 450;
private float a,b;
private int yVelocity = 20;
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) {
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);
if (x<0 && y <0) {
//x = this.getWidth()/2;
y = c.getHeight()/2;
} else {
y += yVelocity;
if (y >( this.getHeight() - ball.getBitmap().getHeight()) ||(y <0)) {
yVelocity = yVelocity*-1;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
if(touching){
h.postDelayed(r, FRAME_RATE);
bm_w=ball.getBitmap().getWidth();
bm_h=ball.getBitmap().getHeight();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int touchType = event.getAction();
switch(touchType){
case MotionEvent.ACTION_MOVE:
a = event.getX();
b = event.getY();
touching = true;
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:
a = event.getX();
b = event.getY();
if(a>x+20&&a<330&&b<=y+320&&b>y)
invalidate();
default:
dm_touched = true;
touching = true;
}
return true;
}
}
请帮帮我
答案 0 :(得分:1)
如果我理解正确,这应该解决它: 而不是y&lt; 0,你应该把y&lt; c.getHeight()/ 2
@Override
protected void onDraw(Canvas c) {
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);
if (x<0 && y <0) {
//x = this.getWidth()/2;
y = c.getHeight()/2;
} else {
y += yVelocity;
if (y >( this.getHeight() - ball.getBitmap().getHeight()) ||(y <c.getHeight()/2)) {
yVelocity = yVelocity*-1;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
if(touching){
h.postDelayed(r, FRAME_RATE);
bm_w=ball.getBitmap().getWidth();
bm_h=ball.getBitmap().getHeight();
}
}