【问题标题】:Scale Animation on Compound Drawables Android在 Compound Drawables Android 上缩放动画
【发布时间】:2019-10-02 13:00:58
【问题描述】:

我有一个RadioButton,其drawableTop 设置为LayerListLayerList 看起来像这样

<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 设置动画。当 RadioButton 被选中时,Drawable 需要从 50% 缩放到 100%,反之亦然。有没有办法在复合绘图中添加动画?特别是当 drawable 由 Color Selector 而不是 Drawable Selector 驱动时。 (在我的情况下为image_radio_button_bg_selector

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

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?attr/colorAccent" android:state_checked="true" />
    <item android:color="@android:color/white" android:state_checked="false" />
    <item android:color="@color/white" />
</selector>

【问题讨论】:

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


    【解决方案1】:

    在 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>
    

    在activity文件中的使用

    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);
        }
    });
    

    【讨论】:

    • 您好,感谢您的努力,但这将为RadioButton 设置动画,而不仅仅是drawable。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 2017-03-09
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多