动画布局的背景颜色变化

job*_*bin 3 animation android android-animation android-layout android-view

我有一个RelativeLayout我已经设置了背景的drawable. 我能够RelativeLayoutRadioButton检查时将背景更改为另一个背景。但是当它改变时我如何给它一个动画?

代码:activity_main.xml:

<RelativeLayout
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:gravity="center"
    android:background="@drawable/original"
    android:id="@+id/rel1">
    <RadioButton
        android:id="@+id/radio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

原始.xml(可绘制):

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:color="#ffffff">
    </solid>
    <corners
        android:radius="50dp">
    </corners>
</shape>
Run Code Online (Sandbox Code Playgroud)

按下.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:color="#2196F3">
    </solid>
    <corners
        android:radius="50dp">
    </corners>
</shape>
Run Code Online (Sandbox Code Playgroud)

Java类:

radio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if(b){
                    relativeLayout.setBackgroundResource(R.drawable.pressed);
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)

azi*_*ian 6

问题归结为以某种方式剪切角并为布局背景设置动画。因此,理论上,我们可以为布局设置一个前景可绘制对象,并使用ArgbEvaluator.

移动到实践。

看看这个答案,作者分享了一个可绘制的蒙版,它可以方便地解决您的问题:

在此处输入图片说明

将该 drawable 应用为布局的前景:

<RelativeLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:foreground="@drawable/frame"
    android:background="@color/colorAccent" />
Run Code Online (Sandbox Code Playgroud)

在代码中,每当需要执行动画时:



    final int from = ContextCompat.getColor(this, R.color.colorAccent);
    final int to   = ContextCompat.getColor(this, R.color.colorPrimary);

    ValueAnimator anim = new ValueAnimator();
    anim.setIntValues(from, to);
    anim.setEvaluator(new ArgbEvaluator());
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            view.setBackgroundColor((Integer)valueAnimator.getAnimatedValue());
        }
    });

    anim.setDuration(1000);
    anim.start();

以下是您将在输出中得到的内容:

在此处输入图片说明

如果你想改变 frame drawable 的颜色,你可以把它包装成 anxml并应用android:tint.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item >
        <bitmap android:src="@drawable/frame" android:tint="#ccaaff"/>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

现在将此 drawable 设置为布局的前景。