【问题标题】:Android rotate drawable in buttonAndroid旋转可绘制按钮
【发布时间】:2020-07-22 12:40:55
【问题描述】:

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

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的解决方案

你有什么想法吗?

感谢您的帮助

【问题讨论】:

标签: java android android-studio drawable


【解决方案1】:

您可以创建一个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 代码中,尝试像下面这样开始动画:

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

结果:

【讨论】:

  • 谢谢它的工作,但动画在每一轮都停止 可以改变这个吗?
  • 绝对可以,只需添加anim.setInterpolator(new LinearInterpolator()); 就像上面的更新一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-16
  • 2015-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多