在按钮中旋转Android可绘制对象

huangapple go评论87阅读模式
英文:

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" />

我找不到调用它并将其分配给按钮可绘制对象的解决方案。

你有什么想法吗?

感谢你的帮助。

英文:

Hello I have add a drawable in a 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);

I Want to infinite rotate it

I create this in res/anim/rotate.xml

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

I don't found solution for call it and assign to drawable in my button

Do you have an idea ?

Thanks for your help

答案1

得分: 3

以下是您要翻译的内容:

您可以创建一个包含动画的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();
}

结果:

在按钮中旋转Android可绘制对象

英文:

You can create a layer-list which contains animation with a drawable.

ic_refresh_rotate.xml

&lt;layer-list xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
    &lt;item&gt;
        &lt;rotate
            android:drawable=&quot;@drawable/ic_refresh&quot;
            android:fromDegrees=&quot;0&quot;
            android:pivotX=&quot;50%&quot;
            android:pivotY=&quot;50%&quot;
            android:toDegrees=&quot;360&quot; /&gt;
    &lt;/item&gt;
&lt;/layer-list&gt;

Now set the drawable as compound drawable for the Button or TextView.

&lt;androidx.appcompat.widget.AppCompatTextView
    android:id=&quot;@+id/textView&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:text=&quot;Hello World!&quot;
    android:textSize=&quot;16sp&quot;
    app:drawableLeftCompat=&quot;@drawable/ic_refresh_rotate&quot; /&gt;

Finally, in your java code, try to start the animation like the following:

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

Result:

在按钮中旋转Android可绘制对象

huangapple
  • 本文由 发表于 2020年7月22日 20:40:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63034426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定