2 how old how_old 于 2016.05.10 09:08 提问

android 关于onTouch跟动画的结合

图片说明我想让控件在屏幕上滑动,当手指放开屏幕的时候控件回到他原本的位置,我应该要怎么改,求大神指导!

6个回答

lxk_1993
lxk_1993   Rxr 2016.05.10 09:15
已采纳

你可以先记录原来的位置 再在Action_UP时 用动画移动到 原来的位置

lxk_1993
lxk_1993 回复how_old: 看下你的代码
一年多之前 回复
how_old
how_old 滑动一次后,再按其他按钮,可以滑动的按钮的就不见。。。该怎么弄
一年多之前 回复
how_old
how_old 弄好了,谢谢
一年多之前 回复
lxk_1993
lxk_1993 回复how_old: 参考http://blog.csdn.net/lxk_1993/article/details/50931607
一年多之前 回复
how_old
how_old 我是继承Fragment的,没有onWindowFocusChanged这个方法
一年多之前 回复
lxk_1993
lxk_1993 回复how_old: 不过actionmove 里面需要改
一年多之前 回复
lxk_1993
lxk_1993 回复how_old: 写好了 在下面
一年多之前 回复
how_old
how_old 回复103style: 那要怎么用呢
一年多之前 回复
lxk_1993
lxk_1993 回复how_old: 写了一个 这里动画不能这么用 你写的这种动画并没有改变实际的位置
一年多之前 回复
how_old
how_old 回复103style: 好的,谢谢
一年多之前 回复
lxk_1993
lxk_1993 回复how_old:我试试 写好给你
一年多之前 回复
how_old
how_old 有记录原来的位置,但是第一次点击的时候没有动画,并且以后点击的时候用translateAnimation动画时效果也不是回到右上角,而是回到鼠标放开时候的位置
一年多之前 回复
q610098308
q610098308   2016.05.10 09:24

1.其实你需要改变控件的位置,
见demo
http://download.csdn.net/detail/q610098308/9511644

how_old
how_old 回复恋恋西风: 好的,不过东西有点多,只能慢慢的往Android Studio那边靠
一年多之前 回复
q610098308
q610098308 如果你做Android 开发,建议用 Android Studio
一年多之前 回复
how_old
how_old 用的是eclipse,好心酸。。
一年多之前 回复
lxk_1993
lxk_1993   Rxr 2016.05.10 10:00
 public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private int tvLeft, tvRight, tvTop, tvBottom;
    private boolean first = true;
    private int startX,startY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.test);

        textView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        textView.layout(
                                (int) (tvLeft + event.getRawX() - startX),
                                (int) (tvTop + event.getRawY() - startY),
                                (int) (tvRight + event.getRawX() - startX),
                                (int) (tvBottom + event.getRawY() - startY));
                        break;
                    case MotionEvent.ACTION_UP:
//                        TranslateAnimation translateAnimation = new TranslateAnimation(tvLeft, tvTop, tvRight, tvBottom);
//                        translateAnimation.setDuration(1000);
//                        textView.setAnimation(translateAnimation);
                        textView.layout(tvLeft, tvTop, tvRight, tvBottom);
                        break;
                }
                return true;
            }
        });

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && first) {
            tvLeft = textView.getLeft();
            tvTop = textView.getTop();
            tvRight = textView.getRight();
            tvBottom = textView.getBottom();
            first = false;
            textView.setText(tvLeft + "," + tvTop + "," + tvRight + "," + tvBottom);
        }
    }
}
lxk_1993
lxk_1993   Rxr 2016.05.10 10:22
public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private int tvLeft, tvRight, tvTop, tvBottom;
    private boolean first = true;
    private int startX, startY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.test);

        textView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        textView.layout(
                                (int) (tvLeft + event.getRawX() - startX),
                                (int) (tvTop + event.getRawY() - startY),
                                (int) (tvRight + event.getRawX() - startX),
                                (int) (tvBottom + event.getRawY() - startY));
                        break;
                    case MotionEvent.ACTION_UP:
                        AnimatorSet set = new AnimatorSet();
                        set.playTogether(
                                ObjectAnimator.ofFloat(textView, "translationX", tvLeft - textView.getLeft()),
                                ObjectAnimator.ofFloat(textView, "translationY", tvTop - textView.getTop())
                        );
                        set.setDuration(2000).start();
                        break;
                }
                return true;
            }
        });

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && first) {
            tvLeft = textView.getLeft();
            tvTop = textView.getTop();
            tvRight = textView.getRight();
            tvBottom = textView.getBottom();
            first = false;
            textView.setText(tvLeft + "," + tvTop + "," + tvRight + "," + tvBottom);
        }
    }
}

lxk_1993
lxk_1993   Rxr 2016.05.10 10:34

加在 textView = (TextView) findViewById(R.id.test); 下面

        if (first){
            textView.post(new Runnable() {
                @Override
                public void run() {
                    tvLeft = textView.getLeft();
                    tvTop = textView.getTop();
                    tvRight = textView.getRight();
                    tvBottom = textView.getBottom();
                    first = false;
                    textView.setText(tvLeft + "," + tvTop + "," + tvRight + "," + tvBottom);
                }
            });
        }

lxk_1993
lxk_1993   Rxr 2016.05.10 10:41

这样基本实现了 你再优化下吧

 public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private int tvLeft, tvRight, tvTop, tvBottom;
    private int Left, Right, Top, Bottom;
    private boolean first = true;
    private int startX, startY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.test);


        if (first){
            textView.post(new Runnable() {
                @Override
                public void run() {
                    tvLeft = textView.getLeft();
                    tvTop = textView.getTop();
                    tvRight = textView.getRight();
                    tvBottom = textView.getBottom();
                    first = false;
                    textView.setText(tvLeft + "," + tvTop + "," + tvRight + "," + tvBottom);
                }
            });
        }


        textView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        Left = textView.getLeft();
                        Top = textView.getTop();
                        Right = textView.getRight();
                        Bottom = textView.getBottom();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        textView.layout(
                                (int) (Left + event.getRawX() - startX),
                                (int) (Top + event.getRawY() - startY),
                                (int) (Right + event.getRawX() - startX),
                                (int) (Bottom + event.getRawY() - startY));
                        break;
                    case MotionEvent.ACTION_UP:
                        AnimatorSet set = new AnimatorSet();
                        set.playTogether(
                                ObjectAnimator.ofFloat(textView, "translationX", tvLeft - textView.getLeft()),
                                ObjectAnimator.ofFloat(textView, "translationY", tvTop - textView.getTop())
                        );
                        set.setDuration(2000).start();

                        textView.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                textView.layout(textView.getLeft(), textView.getTop(), textView.getRight(), textView.getBottom());
                            }
                        }, 2000);

                        break;
                }
                return true;
            }
        });

    }
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!