动画 ImageView 可绘制颜色变化
Animate ImageView drawable colorchange
我正在尝试使用动画更改可绘制形状的颜色。可绘制对象显示在 ImageView 中,颜色通常使用 setColorFilter() 设置。
我尝试过使用 TransitionDrawable 和 ObjectAnimator,但无论我怎么尝试都没有任何反应。
有谁知道该怎么做,或者如果我做错了什么?
这是我要更改颜色的形状:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="@color/circle"/>
<size
android:width="70dp"
android:height="70dp"/>
</shape>
以及我上次尝试使用 TransistionDrawable 的代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
out = new TransitionDrawable(new Drawable[]{getResources().getDrawable(R.drawable.white_circle, getTheme()), getResources().getDrawable(R.drawable.circle, getTheme())});
} else {
out = new TransitionDrawable(new Drawable[]{getResources().getDrawable(R.drawable.white_circle), getResources().getDrawable(R.drawable.circle)});
}
out.setCrossFadeEnabled(true);
out.startTransition(1000);
其中 white_circle 与圆形完全相同,但颜色为白色
不幸的是,我没有找到用动画更改 Android 中形状颜色的方法,我使用了另一种解决方案。
我创建了两次对象,显示在完全相同的位置,一次使用起始颜色,一次使用结束颜色。当需要过渡时,我只是淡出旧图像并淡入新图像。
如果有人仍然想办法在不使用第二个对象的情况下为其设置动画,请随时回答这个问题。
我正在尝试使用动画更改可绘制形状的颜色。可绘制对象显示在 ImageView 中,颜色通常使用 setColorFilter() 设置。
我尝试过使用 TransitionDrawable 和 ObjectAnimator,但无论我怎么尝试都没有任何反应。
有谁知道该怎么做,或者如果我做错了什么?
这是我要更改颜色的形状:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="@color/circle"/>
<size
android:width="70dp"
android:height="70dp"/>
</shape>
以及我上次尝试使用 TransistionDrawable 的代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
out = new TransitionDrawable(new Drawable[]{getResources().getDrawable(R.drawable.white_circle, getTheme()), getResources().getDrawable(R.drawable.circle, getTheme())});
} else {
out = new TransitionDrawable(new Drawable[]{getResources().getDrawable(R.drawable.white_circle), getResources().getDrawable(R.drawable.circle)});
}
out.setCrossFadeEnabled(true);
out.startTransition(1000);
其中 white_circle 与圆形完全相同,但颜色为白色
不幸的是,我没有找到用动画更改 Android 中形状颜色的方法,我使用了另一种解决方案。
我创建了两次对象,显示在完全相同的位置,一次使用起始颜色,一次使用结束颜色。当需要过渡时,我只是淡出旧图像并淡入新图像。
如果有人仍然想办法在不使用第二个对象的情况下为其设置动画,请随时回答这个问题。