Android 图像视图不移动

Android 图像视图不移动,android,view,Android,View,我试图让一个Imageview(一个球)在布局中移动,并在按下按钮停止之前反弹多次。问题是,尽管logcat说它正在发生,但我看不到它在移动 给你 public class BallPhisics { int x =400; int y = 0; boolean bounceX = false; boolean bounceY= false; int counter =0; ImageView object; public BallPhisics(ImageView i){ object

我试图让一个Imageview(一个球)在布局中移动,并在按下按钮停止之前反弹多次。问题是,尽管logcat说它正在发生,但我看不到它在移动

给你

public class BallPhisics {
int x =400;
int y = 0;
boolean bounceX = false;
boolean bounceY= false;
int counter =0;
ImageView object;
public BallPhisics(ImageView i){
    object=i;
}

public void applyMovement() {
    while (true) {
        object.setLeft((int) object.getX()+x); //i know i shouldnt use pixels
        Log.d("EVENT", "X moved");        Log.d("Ended",Integer.toString(object.getLeft()));

        object.setBottom((int)(object.getY() + y));
        Log.d("EVENT", "Y moved");
        try {
            Thread.sleep(1000);
            Log.d("EVENT", "Time 1 used");
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }

        if (object.getX()<=50||(object.getRight()<=50)){
            bounceX =true;
            break;
        }
        if (object.getY()<=50||object.getTop()<=50){
            bounceY=true;
            break;
        }
    }
    this.bouncing();

}
public void bouncing(){
    Log.d("EVENT", "Bouncing!!");
    if (bounceX&&bounceY){
        x=-x;
        y=-y;
    }
    else if (bounceX){
        x=-x;
        y=(int)(Math.random()*100- 50 +y);
    }
    else if (bounceY) {
        x = (int) (Math.random() * 100 - 50 + x);
        y = -y;
    }
    counter++;
    if(counter==5){return;}
    this.applyMovement();
}
对不起,如果这是很多阅读。顺便问一下,有人知道移动视图的正确方法吗


首先要感谢您移动视图,使用
setLeft(int-left)
setBottom(int-bottom)
可能不是一个好主意,因为此方法是由布局系统调用的,通常不应以其他方式调用

要动态移动视图,您应该查看
LayoutParams
setX(float x)
setY(float Y)
,如果您可以将支持限制在蜂巢(API级别11)

无论你使用哪一种,你可能会发现很难实现平稳的运动。因此,我建议您使用视图动画系统来执行ImageView的T形动画

下面是一个链动画示例,该动画使用任意y坐标从左向右移动ImageView。每次从一个x边框转换到下一个x边框后,将调用
onAnimationEnd
,并在另一个方向上启动动画

public class MainActivity extends Activity {

  //the ImageView you want to animate
  private ImageView imageview; 

  private Animation mAnimation;

  //The coordinates the view moved to in the last animation 
  private float lastX=0;
  private float lastY=0;
  private float secondlastY;
 
  //Listener that implements a translate animation chain
  AnimationListener animationListener=new AnimationListener() {
        @Override
        public void onAnimationEnd(Animation animation) {
            
            float newY=(float)(Math.random()*0.75);

            //this prevents that we move back to the position we came from
            // to get a more natural bounce animation
            while(newY<=secondlastY+0.15 && newY>=secondlastY-0.15){
                newY=(float)(Math.random()*0.75);
            }
            
            if(lastX==0.75f){
              //test if we are on the right border of the parent
                mAnimation=newAnimation(lastX,lastY,0f,newY);
                mAnimation.setAnimationListener(animationListener);
                lastX=0f;
                
            }else if(lastX==0.0f){
                //test if we are on the left border of the parent
                mAnimation=newAnimation(lastX,lastY,0.75f,newY);
                mAnimation.setAnimationListener(animationListener);
                lastX=0.75f;
            }
            
            secondlastY=lastY;
            lastY=newY;
            imageview.startAnimation(mAnimation);
        }
  };
    
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    imageview=(ImageView) findViewById(R.id.imageView);

    //the coordinates the first animation should move to
    lastY=(float)(Math.random()*0.75);
    lastX=0.75f;

    mAnimation=newAnimation(0f,0f,lastX,lastY); 
    mAnimation.setAnimationListener(animationListener);
    imageview.startAnimation(mAnimation);
 }

  //Method that returns a new animation with given start and end coordinates
  private Animation newAnimation(float startX, float startY, float endX, float endY){
     Animation mAnimation = new TranslateAnimation(
              TranslateAnimation.RELATIVE_TO_PARENT, startX,
              TranslateAnimation.RELATIVE_TO_PARENT, endX,
              TranslateAnimation.RELATIVE_TO_PARENT, startY,
              TranslateAnimation.RELATIVE_TO_PARENT, endY );
     mAnimation.setDuration(2500);
     mAnimation.setRepeatCount(0);
     mAnimation.setRepeatMode(Animation.REVERSE);
     mAnimation.setFillAfter(true);
          
     return mAnimation;
 }
公共类MainActivity扩展活动{
//要设置动画的图像视图
私人影像视图;
私人动画制作;
//视图在上一个动画中移动到的坐标
私有浮动lastX=0;
私人浮动弹性=0;
二次成形术;
//实现平移动画链的侦听器
AnimationListener AnimationListener=新建AnimationListener(){
@凌驾
onAnimationEnd上的公共无效(动画){
float newY=(float)(Math.random()*0.75);
//这可以防止我们回到原来的位置
//以获得更自然的反弹动画
while(newY=secondlastY-0.15){
newY=(float)(Math.random()*0.75);
}
如果(lastX==0.75f){
//测试我们是否在父对象的右边框上
mAnimation=newAnimation(lastX,lastY,0f,newY);
mAnimation.setAnimationListener(animationListener);
lastX=0f;
}else if(lastX==0.0f){
//测试我们是否在父对象的左边框上
mAnimation=newAnimation(lastX,lastY,0.75f,newY);
mAnimation.setAnimationListener(animationListener);
lastX=0.75f;
}
第二次整形=整形;
lastY=newY;
imageview.startAnimation(管理);
}
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageview=(imageview)findViewById(R.id.imageview);
//第一个动画应该移动到的坐标
lastY=(float)(Math.random()*0.75);
lastX=0.75f;
mAnimation=newAnimation(0f,0f,lastX,lastY);
mAnimation.setAnimationListener(animationListener);
imageview.startAnimation(管理);
}
//方法返回具有给定开始和结束坐标的新动画
私有动画新建动画(浮点startX、浮点startY、浮点endX、浮点endY){
动画模拟=新的平移模拟(
TranslateAnimation.RELATIVE\u到\u父项,startX,
TranslateAnimation.RELATIVE_到_父对象,endX,
TranslateAnimation.RELATIVE\u TO\u PARENT,startY,
TranslateAnimation.RELATIVE_TO_PARENT,endY);
设置持续时间(2500);
mAnimation.setRepeatCount(0);
mAnimation.setRepeatMode(动画.反转);
mAnimation.setFillAfter(true);
返回操作;
}
}

注意:
平移动画与父对象的宽度和高度相关。如果将视图一直移动到x=1.0或y=1.0,则会将部分视图移出父布局。因为对于这个例子来说已经足够了,所以我选择在两个方向上将最大位置设置为0.75。但是您可能应该根据ImageView和ParentLayout的宽度和高度动态设置此选项。

while(true)和Thread.sleep on the ui线程sono a bad idea是的,但这只是一个实验:)为什么?我不确定。logcat看起来很棒!顺便问一下,加速度是大量的平移图像还是有一个特定的方法或类?有一些插值类,你可以在这里找到。例如,您可以使用它们让变化的速率从缓慢开始,然后加速。只需在newAnimation函数中添加如下内容:
mAnimation.setInterpolator(新的AccelerateInterpolator())。如果要整体加速移动,请缩短动画的持续时间。
public class MainActivity extends Activity {

  //the ImageView you want to animate
  private ImageView imageview; 

  private Animation mAnimation;

  //The coordinates the view moved to in the last animation 
  private float lastX=0;
  private float lastY=0;
  private float secondlastY;
 
  //Listener that implements a translate animation chain
  AnimationListener animationListener=new AnimationListener() {
        @Override
        public void onAnimationEnd(Animation animation) {
            
            float newY=(float)(Math.random()*0.75);

            //this prevents that we move back to the position we came from
            // to get a more natural bounce animation
            while(newY<=secondlastY+0.15 && newY>=secondlastY-0.15){
                newY=(float)(Math.random()*0.75);
            }
            
            if(lastX==0.75f){
              //test if we are on the right border of the parent
                mAnimation=newAnimation(lastX,lastY,0f,newY);
                mAnimation.setAnimationListener(animationListener);
                lastX=0f;
                
            }else if(lastX==0.0f){
                //test if we are on the left border of the parent
                mAnimation=newAnimation(lastX,lastY,0.75f,newY);
                mAnimation.setAnimationListener(animationListener);
                lastX=0.75f;
            }
            
            secondlastY=lastY;
            lastY=newY;
            imageview.startAnimation(mAnimation);
        }
  };
    
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    imageview=(ImageView) findViewById(R.id.imageView);

    //the coordinates the first animation should move to
    lastY=(float)(Math.random()*0.75);
    lastX=0.75f;

    mAnimation=newAnimation(0f,0f,lastX,lastY); 
    mAnimation.setAnimationListener(animationListener);
    imageview.startAnimation(mAnimation);
 }

  //Method that returns a new animation with given start and end coordinates
  private Animation newAnimation(float startX, float startY, float endX, float endY){
     Animation mAnimation = new TranslateAnimation(
              TranslateAnimation.RELATIVE_TO_PARENT, startX,
              TranslateAnimation.RELATIVE_TO_PARENT, endX,
              TranslateAnimation.RELATIVE_TO_PARENT, startY,
              TranslateAnimation.RELATIVE_TO_PARENT, endY );
     mAnimation.setDuration(2500);
     mAnimation.setRepeatCount(0);
     mAnimation.setRepeatMode(Animation.REVERSE);
     mAnimation.setFillAfter(true);
          
     return mAnimation;
 }