按住按钮修改视图属性

时间:2016-12-03 20:43:07

标签: android android-animation android-view

我有两个视图,v1和v2。我的目标是,当用户按下并按住v1时,v2会使用动画进行扩展。当用户放开v1时,v2停止扩展。

这就是我的onCreate的样子:

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

    root = findViewById(R.id.root);
    shape = findViewById(R.id.shape);

    root.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN: {
                    isExpanding = true;
                    expandView(shape);
                } return true;

                case MotionEvent.ACTION_UP: {
                    isExpanding = false;
                    shrinkView(shape);
                } return true;

                default: return false;
            }

        }
    });
}

对于expandView()和shrinkView(),我尝试添加一个包含isExpanding的while循环并在其中添加动画:

private void shrinkView(final View view) {
    while (!isExpanding) {
        view.animate().scaleXBy(0.1f).scaleYBy(0.1f);
    }
}

我也尝试在循环中更改LayoutParams:

private void expandView(final View view) {
    while (isExpanding) {
        final ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        currWidth = layoutParams.width;
        currHeight = layoutParams.height;
        currWidth = +GROWTH_FACTOR;
        currHeight = +GROWTH_FACTOR;
        layoutParams.width += currWidth;
        layoutParams.height += currHeight;
        view.setLayoutParams(layoutParams);
        view.invalidate();
    }
}

但是这两个都按预期阻止了ui线程。

2 个答案:

答案 0 :(得分:1)

对于这种类型的动画,我会使用ObjectAnimator来缩放X和Y.然后我会向该ObjectAnimator添加一个监听器,并在用户仍在按下时检查onAnimationEnd(isExpanding)。如下面的代码:

    View v2 = findViewById(R.id.v2);//lets say u have v2 and v1
       final ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(v2, "scaleX", 0.5f);
    final ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(v2, "scaleY", 0.5f);
    scaleDownX.setDuration(1000);
    scaleDownY.setDuration(1000);


    final AnimatorSet scaleAnimation = new AnimatorSet();
    scaleAnimation.play(scaleDownX).with(scaleDownY);
    scaleAnimation.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            if (isExpanding) {
                scaleAnimation.start();
            }
        }
    });


           findViewById(R.id.v1).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getAction()) {

                case MotionEvent.ACTION_DOWN: {
                    isExpanding = true;
                    if (!scaleAnimation.isRunning()) {
                        scaleAnimation.start();
                    }

                }
                return true;

                case MotionEvent.ACTION_UP: {
                    isExpanding = false;
                    scaleAnimation.cancel();
                }
                return true;

                default:
                    return false;
            }
        }
    });

希望这会有所帮助。

答案 1 :(得分:0)

你可以使用一个asynctask,它在每个固定的时间间隔内调用它的发布方法,在那个调用中展开v2。这将释放你的ui线程来监听v1的发布。

或者另一种方法是在用户持有v1并使该线程返回post时更新(修改v2)时启动一个线程。此外,您可以在释放v1时终止线程。