在Android上绘制比例动画

时间:2019-05-15 11:34:19

标签: android android-animation android-drawable android-selector

我有一个RadioButton设置为drawableTop的{​​{1}}。 LayerList看起来像这样

LayerList

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="@color/image_radio_button_bg_selector" /> <size android:width="@dimen/imageRadioButtonDrawableSize" android:height="@dimen/imageRadioButtonDrawableSize" /> <corners android:radius="@dimen/imageRadioButtonDrawableRadius" /> </shape> </item> <layer-list> 被选中或未选中的情况下,我希望能够对该可绘制对象进行动画处理。选中RadioButton时,Drawable需要从50%缩放到100%,反之亦然。有没有一种方法可以在复合drawable中添加动画?特别是当drawable由RadioButton驱动而不是Color Selector驱动时。 (在我的情况下为Drawable Selector

那个颜色选择器看起来像这样

image_radio_button_bg_selector

1 个答案:

答案 0 :(得分:0)

在res / anim / scale.xml中创建文件

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale
        android:pivotX="50%"     // anchor point in x-axis->
        android:pivotY="50%"     // anchor point in x-axis
        android:fromYScale="0.5" // from size Y axis
        android:fromXScale="0.5" // from size X axis
        android:interpolator="@android:anim/bounce_interpolator"
        android:toYScale="1.0"   // to size Y axis
        android:toXScale="1.0"   // to size X axis
        android:duration="250"/> // time of duration in milliseconds

</set>

活动文件中的用法

RadioGroup rGroup = (RadioGroup)findViewById(R.id.radioGroup);
RadioButton checkedRadioButton =(RadioButton)rGroup.findViewById(rGroup.getCheckedRadioButtonId());
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    public void onCheckedChanged(RadioGroup group, int checkedId) {
       RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
       Animation bulge = AnimationUtils.loadAnimation(getApplication(), R.anim.scale);
                        checkedRadioButton.startAnimation(bulge);
    }
});