我已经在Android中编写了类似国际象棋的益智游戏,基本上已经完成了。我现在想做的就是添加一些图形效果,从改善棋子的运动开始,以改善游戏的感觉和外观。

我将主游戏板国际象棋棋盘构造为GridLayoutTextViews,以便将国际象棋棋子用国际象棋字体表示为字母(通过调用setBackgroundResource将正方形涂成黑色或白色)在TextView上)。每个TextView都有一个onClickListener,因此,当用户单击适当的正方形时,所讨论的TextViews上的字母会根据国际象棋规则进行更改。这意味着图形感觉有些不自然:移动的棋子从其原始正方形消失,并立即重新出现在其移动的正方形上。我想通过让国际象棋棋子从其原始正方形滑到其新正方形来改变这种情况。在保持GridLayout-of-TextViews逻辑的同时,有什么方法可以执行此操作?还是需要显着更改代码的结构?无论如何,如果有人知道如何入门,我很想听听!

最佳答案

试试这个

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtOne = (TextView)findViewById(R.id.textone);
    txtTwo = (TextView)findViewById(R.id.text_two);
    relative = (RelativeLayout)findViewById(R.id.relative);


    txtTwo.setOnClickListener(new View.OnClickListener() {

        @SuppressLint("NewApi")
        @Override
        public void onClick(View v) {



            float to_x = txtOne.getX();
            float to_y = txtOne.getY();


            float x_from = txtTwo.getX();
            float y_from = txtTwo.getY();
            //txtTwo.getLocationInWindow(fromLoc);
            txtOne.getLocationOnScreen(toLoc);
            Animations anim = new Animations();

            Animation a = anim.fromAtoB(x_from, y_from, -to_x, -to_y, animL,1000);
            txtTwo.startAnimation(a);

        }
    });
}



public class Animations {
    public Animation fromAtoB(float fromX, float fromY, float toX, float toY, AnimationListener l, int speed){


        Animation fromAtoB = new TranslateAnimation(

                fromX,

                toX,

                fromY,

                toY
                );

        fromAtoB.setDuration(speed);
        //fromAtoB.setInterpolator(new );


        if(l != null)
            fromAtoB.setAnimationListener(l);
        return fromAtoB;
    }
}



AnimationListener animL = new AnimationListener() {

    @Override
    public void onAnimationStart(Animation animation) {
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        //this is just a method to delete the ImageView and hide the animation Layout until we need it again.
        //clearAnimation();
    }
};

10-07 12:23