如何在Android中制作动画时更改视图大小?

时间:2018-04-04 05:41:06

标签: java android android-studio views drawable

我在android studio中的片段中设置了以下内容: enter image description here

这会从100%动画到收到的值。

圆圈是一个ProgressBarHorizo​​ntal,带有这个drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:useLevel="true"
    android:innerRadiusRatio="2.3"
    android:shape="ring"
    android:thickness="8sp" >
    <solid android:color="@color/colorPrimary" />
</shape>

文字,TextView。

这两个视图都在此函数中设置动画:

public void capacityRecieved(int value){

        valueAnimator = ValueAnimator.ofInt(100,value);
        animation = ObjectAnimator.ofInt (progressBar, "progress", 500, value*500/100);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                String displayText = valueAnimator.getAnimatedValue().toString() + "%";
                capacityTextView.setText(displayText);
            }
        });
        valueAnimator.setDuration(5000);
        animation.setDuration(5000); //in milliseconds
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        animation.setInterpolator(new DecelerateInterpolator());
        valueAnimator.start();
        animation.start();
        mSwipeRefreshLayout.setRefreshing(false);
    }
}

当我旋转设备时,我会使用以下行调整尺寸:

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    final int pHeight = progressBar.getHeight();
    ViewTreeObserver observer = rootView.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            ViewGroup.LayoutParams params = progressBar.getLayoutParams();
            params.width = progressBar.getHeight();
            int newHeight = progressBar.getHeight();
            float newSize = ((float)newHeight/(float)pHeight)*70;
            progressBar.setLayoutParams(params);
            capacityTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, newSize);
            rootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
}

这很有效,除非我在动画时旋转设备,在这种情况下,新配置未应用(我必须重新旋转手机然后才能正常工作)

如何在制作动画时更改尺寸?

0 个答案:

没有答案