【问题标题】:Android TranslateAnimation strange problemsAndroid TranslateAnimation 奇怪的问题
【发布时间】:2015-05-02 10:13:27
【问题描述】:

作为我项目的一部分,我需要为 Android 设备创建棋盘(可以移动棋子)。在我决定为片段动作添加动画之前,一切都运行良好。现在问题如下:如果代码看起来像您可能在下面看到的代码,则行为如下:

  • 首先,X 棋子按我的预期移动;
  • 但是当我开始为下一个移动设置动画时,X 块的图像会出现(尽管相应的 ImageView 保持为空)在上一个移动之前它所在的单元格中。我的意思是,它的副本出现在之前的位置,同时它还停留在当前单元格中;
final ImageView movingPiece = (ImageView) findViewById(prevCellId);
TranslateAnimation animation = new TranslateAnimation(0, dx, 0, dy);
animation.setDuration(500);
animation.setFillBefore(true);
animation.setFillAfter(true);
animation.setAnimationListener(new Animation.AnimationListener() {
    public void onAnimationEnd(Animation animation) {
        setPiece(prevCellId, null);
        setPiece(newCellId, chosenPiece);
        movingPiece.clearAnimation();
    }

    public void onAnimationRepeat(Animation animation) {
    }

    public void onAnimationStart(Animation animation) {
    }
});
movingPiece.startAnimation(animation);

为了使有关代码的一些要点清楚:setPiece 获取要放入一些内容的单元格的 id 和要放入单元格的对象。在方法内部,我将相应的 ImageView ImageResource 替换为相应的图片或 android.R.color.transparent(如果第二个参数是 null)。

请向我解释发生了什么以及如何实现所需的行为(我的意思是,没有奇怪地重新出现在其先前位置上的片段)。

提前致谢

【问题讨论】:

    标签: android android-animation translate-animation


    【解决方案1】:

    dx 和 dy 可能计算错误 (如果您在不同的行上使用 getY)。

    并删除fillBefore()和fillAfter()

    我用按钮重新创建了你的情况,它对我有用,这是源代码

    import android.app.*;
    import android.os.*;
    import android.widget.*;
    import java.util.*;
    import android.view.*;
    import android.view.animation.*;
    import android.graphics.*;
    
    public class MainActivity extends Activity {
    
        private int id, d, x, y;
        private Button selected;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
    
            d = getResources().getDisplayMetrics().widthPixels/10;
    
            LinearLayout grid = new LinearLayout(this);
            grid.setClipChildren(false);
            grid.setOrientation(LinearLayout.VERTICAL);
            for(int c=0; c<10; c++) addRow(grid);
            setContentView(grid);
    
        }
    
        private void addRow(LinearLayout grid) {
            x=0;
            LinearLayout row = new LinearLayout(this);
            for(int c=0; c<10; c++) addCell(row);
            row.setClipChildren(false);
            row.setClipToOutline(false);
            grid.addView(row);
            y+=d;
        }
    
        private void addCell(LinearLayout row) {
            Button cell = new Button(this) {
    
                int _x = x;
                int _y = y;
    
                @Override
                public float getX() {
                    return _x;
                }
                @Override
                public float getY() {
                    return _y;
                }
            };
            cell.setText(""+(id++));
            cell.setTextSize(10);
            cell.setClipToOutline(false);
            cell.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
    
    
                        Button bt = (Button)v;
    
                        //select source
                        if(selected==null) {
                            selected=bt;
                            return;
                        }
    
                        //deselect pressing again
                        if(selected==bt) {
                            selected=null;
                            return;
                        }
    
                        //selected source and target
                        moveTo(bt);
    
    
    
                    }
                });
            row.addView(cell, d, d);
    
            x+=d;
        }
    
        private void moveTo(final Button target) {
    
            final Button movingPiece = selected;
    
            float dx = target.getX() - movingPiece.getX();
            float dy = target.getY() - movingPiece.getY();
    
            TranslateAnimation animation = new TranslateAnimation(0, dx, 0, dy);
            animation.setDuration(500);
            //animation.setFillBefore(true);
            //animation.setFillAfter(true);
            animation.setAnimationListener(new Animation.AnimationListener() {
                    public void onAnimationEnd(Animation animation) {
                        setPiece(target, selected.getText().toString());
                        setPiece(selected, null);
    
                        //movingPiece.clearAnimation();
    
                        selected = null;
                    }
    
                    public void onAnimationRepeat(Animation animation) {
                    }
    
                    public void onAnimationStart(Animation animation) {
                    }
                });
            movingPiece.startAnimation(animation);
        }
    
        private void setPiece(Button bt, String txt) {
            if(txt==null) bt.setText("");
            bt.setText(txt);
        }
    
    }
    

    我希望你会发现这很有用。

    你如何计算 dx 和 dy ?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 2012-12-20
      • 2011-02-18
      相关资源
      最近更新 更多