簡體   English   中英

Android TranslateAnimation奇怪的問題

[英]Android TranslateAnimation strange problems

作為項目的一部分,我需要為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和要放入該單元格的對象。 在方法內部,我用各自的圖片或android.R.color.transparent (如果第二個參數為null )替換各自的ImageView ImageResource。

請向我解釋正在發生的事情以及如何實現期望的行為(我的意思是,在先前的位置上不會出現奇怪的碎片)。

提前致謝

dx和dy可能計算錯誤(如果在不同的行上使用getY)。

並刪除fillBefore()和fillAfter()

我已經用Buttons重新創建了您的情況,它對我有用,這是源代碼

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?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM