繁体   English   中英

Android 旋转可绘制按钮

[英]Android rotate drawable in button

您好,我在按钮中添加了一个可绘制对象:

Drawable image = null;
image = getActivity().getDrawable(R.drawable.ic_spinner);
int h = image.getIntrinsicHeight();
int w = image.getIntrinsicWidth();
image.setBounds( 0, 0, w, h );
button.setCompoundDrawables( null, null, image,  null);

我想无限旋转它

我在 res/anim/rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite" />

我没有找到调用它并在我的按钮中分配给drawable的解决方案

你有想法吗?

谢谢你的帮助

您可以创建一个包含 animation 和可绘制对象的layer-list

ic_refresh_rotate.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <rotate
            android:drawable="@drawable/ic_refresh"
            android:fromDegrees="0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="360" />
    </item>
</layer-list>

现在将可绘制对象设置为ButtonTextView的复合可绘制对象。

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:textSize="16sp"
    app:drawableLeftCompat="@drawable/ic_refresh_rotate" />

最后,在您的 java 代码中,尝试启动 animation,如下所示:

Drawable[] compoundDrawables = textView.getCompoundDrawables();
for (Drawable drawable : compoundDrawables) {
    if (drawable == null) continue;
    ObjectAnimator anim = ObjectAnimator.ofInt(drawable, "level", 0, 10000);
    anim.setDuration(1000);
    anim.setRepeatCount(Animation.INFINITE);
    anim.setInterpolator(new LinearInterpolator());
    anim.start();
}

结果:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM