0

我对在 Android 上构建应用程序有点陌生。我确实有一些Java开发经验,但我现在做的项目首先是在Android上。

我正在构建一个基本的纸牌游戏,我希望卡片移动到我触摸的特定位置。我做到了,卡片移动到接触点,但是一旦动画结束,它就会弹回起始位置。

我添加了一个图像重新定位代码,但现在它从那个点开始动画。任何想法,有人吗?

我做的代码是:

    public class FinallyActivity extends Activity {
        /** Called when the activity is first created. */

        EditText DBG;
        ImageView iv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        DBG = (EditText)  findViewById(R.id.editText1);
        iv =  (ImageView) findViewById(R.id.imV1);      
    }

    private void RunAnimations(MotionEvent event) {
    //  Animation b = AnimationUtils.loadAnimation(this,R.anim.cardtrans);
        int xStart, yStart, dx, dy;

        xStart = iv.getLeft();
        yStart = iv.getTop();
        Animation b = new TranslateAnimation(Animation.ABSOLUTE,0,Animation.ABSOLUTE,event.getX()-50,
                Animation.ABSOLUTE,0,Animation.ABSOLUTE,event.getY()- 105);
        b.setDuration(3000);
        iv.clearAnimation();
        iv.startAnimation(b);   

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            RunAnimations(event);
        }

                iv.layout((int)event.getX()-25,(int) event.getY()-105,
                (int)event.getX()+25,(int) event.getY()-35);
        return true;

    }

}

谢谢大家!!

4

2 回答 2

1

b.fillAfter(true)

http://developer.android.com/reference/android/view/animation/Animation.html#setFillAfter(boolean)

于 2012-04-19T18:09:50.080 回答
0
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = findViewById(R.id.subview);
    imageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    xDown = event.getX();
                    yDown = event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    float moveX,moveY;
                    moveX = event.getX();
                    moveY = event.getY();

                    float distanceX = moveX -xDown;
                    float distanceY  = moveY -yDown;
                    imageView.setX(imageView.getX()+distanceX);
                    imageView.setY(imageView.getY()+distanceY);
                    break;
            }
            return true;
        }
    });
}
于 2020-12-09T17:54:58.010 回答