【问题标题】:How to move ImageView from specified position?如何从指定位置移动 ImageView?
【发布时间】:2011-12-04 23:42:15
【问题描述】:

我从 2 天前开始做一个基本的游戏项目,现在我想制作一个需要将我的球移动到指定位置的关卡,我有 3 个球(我的球使用ImageView):

FstBall=(ImageView)findViewById(R.id.FirstBall);
SecBall=(ImageView)findViewById(R.id.SecondBall);
ThdBall=(ImageView)findViewById(R.id.ThirdBall);

我用这些方法来移动我的球,但是没有用,有什么想法吗?

@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
int FirstBallX = FstBall.getLeft();
int FirstBallY = FstBall.getTop();
int SecondBallX = SecBall.getLeft();
int SecondBallY = SecBall.getTop();
int ThirdBallX = ThdBall.getLeft();
int ThirdBallY = ThdBall.getTop();
int action = event.getAction();
int x = (int)event.getX();
int y = (int)event.getY();
// Dynamic X and Y :
int FstBaseX= 0;
int FstBaseY= 0;
int SecBaseX= 0;
int SecBaseY= 0;
int ThdBaseX= 0;
int ThdBaseY= 0;
int PrimeyX = 0;
int PrimeryY = 0;
switch (action) {
case MotionEvent.ACTION_DOWN:
if (x > FirstBallX & y > FirstBallY){
FstBaseX = x;
FstBaseY= y;
}else if(x > SecondBallX & y > SecondBallY){
SecBaseX = x;
SecBaseY= y;
}else if(x > ThirdBallX & y > ThirdBallY){
ThdBaseX = x;
ThdBaseY= y;
} else {
// Do Nothing
}
break;
case MotionEvent.ACTION_MOVE:
PrimeyX = x;
PrimeryY = y;
break;
case MotionEvent.ACTION_UP:
if (x > FirstBallX & y > FirstBallY){
int FstFnlResX = FstBaseX + PrimeyX;
int FstFnlResY = FstBaseY + PrimeryY;
FstBall.scrollTo(FstFnlResX, FstFnlResY);
}else if(x > SecondBallX & y > SecondBallY){
int SecFnlResX = SecBaseX + PrimeyX;
int SecFnlResY = SecBaseY + PrimeryY;
SecBall.scrollTo(SecFnlResX, SecFnlResY);
}else if(x > ThirdBallX & y > ThirdBallY){
int ThdFnlResX = ThdBaseX + PrimeyX;
int ThdFnlResY = ThdBaseY + PrimeryY;
ThdBall.scrollTo(ThdFnlResX, ThdFnlResY);
}
break;
}
return true;
}

【问题讨论】:

    标签: android


    【解决方案1】:

    你不应该尝试使用 setX 和 setY 然后调用 View.invalidate 并查看它是否刷新?我不认为在这里使用 scrollTo 是一个好主意。您可以先尝试调用 View.invalidate 。如果它不起作用,请尝试使用 setX 和 setY。

    不过,请阅读此内容.. 它可能会有所帮助 Where does Android View.scrollTo(x, y) scroll to?

    祝你好运。

    【讨论】:

      猜你喜欢
      • 2013-09-22
      • 2019-08-02
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多