我想在我的复选框中添加某种材质外观。我希望可绘制的复选标记使用自定义可绘制对象从一种状态动画到另一种状态。我尝试为 an ImageView
(以查看它是否有效)和 a CheckedTextView
(这是我真正需要的)制作动画。
为了做到这一点,我在布局中创建了两个视图,并android:onClick
分别设置为自定义方法animateImage(View v)
和animateChecked(View v)
. 我的问题是第二个不起作用,尽管两者之间的唯一区别是我如何获取和设置我需要的可绘制对象。
布局.xml
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:src="@drawable/checkbox_off" <!-- static png showing the unchecked mark -->
android:clickable="true"
android:onClick="animateImage"/>
<CheckedTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/impo8"
android:gravity="center_vertical|left"
android:checked="false"
android:clickable="true"
android:CheckMark="@drawable/checkbox_off" <!-- again -->
android:onClick="animateChecked" />
活动
public void animateImage(View v) {
ImageView iv = (ImageView)v;
iv.setBackgroundResource(R.drawable.animation_checkbox_on);
AnimationDrawable adOn = (AnimationDrawable)iv.getBackground();
adOn.setColorFilter(getResources().getColor(R.color.static), PorterDuff.Mode.SRC_IN);
adOn.start();
}
public void animateChecked(View v) {
CheckedTextView c = (CheckedTextView)v;
c.setCheckMarkDrawable(R.drawable.animation_checkbox_on);
AnimationDrawable ad = (AnimationDrawable) c.getCheckMarkDrawable();
ad.setColorFilter(getResources().getColor(R.color.static), PorterDuff.Mode.SRC_IN);
ad.start();
}
当然,在这两种方法中都使用的animation_checkbox_on是具有静态 png 和持续时间的动画列表 xml 资源。我想让它尽可能简单(甚至没有实现“如果检查”语句)。
如前所述:第一种方法有效并且 ImageView 得到正确的动画;第二种方法不起作用,复选标记保持不动。我的代码中是否存在某种概念错误,或者我在某处错了?我怎样才能在CheckedTextView
标记上进行这项工作?我没有收到来自 Android Studio 的任何警告。谢谢你。
/drawable/animation_checkbox_on
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item
android:drawable="@drawable/checkbox_off2" android:duration="50" />
<item
android:drawable="@drawable/checkbox_off3" android:duration="50" />
<item
android:drawable="@drawable/checkbox_on" android:duration="50" />
编辑:
我注意到动画列表中的第一项实际上已加载。此外,如果我删除该行ad.start()
(!),结果是相同的。
它看起来像setCheckMarkDrawable
或者getCheckMarkDrawable
只返回动画列表的第一项,从而使无效ad.start()
,并将动画转换为简单的可绘制开关。如果这是真的,那么问题将是如何为 CheckedTextView 设置动画。在应用下一个可绘制对象之前,我已经阅读了有关手动暂停线程一段时间的信息,但这对我来说似乎不是最好的选择。