首页 > 解决方案 > 如何创建可绘制的右动画按钮

问题描述

如何创建正确的旋转按钮动画,我尝试了下面的代码,但它一直在崩溃。

button_spin_animation.xml

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

用法

 @Override
    public void onStart() {
        super.onStart();

        account_login.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(getApplicationContext(), R.drawable.button_spin_animation), null);
        Drawable[] sb = account_login.getCompoundDrawables();
        AnimationDrawable animDrawable = (AnimationDrawable) sb[0];
        animDrawable.start();
    }

错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.android.buyer/com.app.android.buyer.Login}: android.content.res.Resources$NotFoundException: Drawable 
     Caused by: android.content.res.Resources$NotFoundException: Drawable com.app.android.buyer:drawable/button_spin_animation with resource ID #0x7f080146
     Caused by: android.content.res.Resources$NotFoundException: File res/drawable/button_spin_animation.xml from drawable resource ID #0x7f080146
        at 
     Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #8: <rotate> tag requires a 'drawable' attribute or child tag defining a drawable
        at android.graphics.drawable.RotateDrawable.verifyRequiredAttributes(RotateDrawable.java:106)
        at android.graphics.drawable.RotateDrawable.inflate(RotateDrawable.java:76)
        at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:145)
        at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1295)
        at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1254)
        at 

标签: androidandroid-animationdrawable

解决方案


您需要进行 3 处更改:

  1. 将drawable添加到您的button_spin_animation
  2. 如果您想获得正确的可绘制对象,请使用第二个索引。
  3. 用于ObjectAnimator为您的drawable设置动画。

以下是您的用例示例:

Java file:

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv = findViewById(R.id.tv);

        tv.setCompoundDrawablesWithIntrinsicBounds(
                null,
                null,
                ContextCompat.getDrawable(getApplicationContext(), R.drawable.button_spin_animation),
                null
        );

        int MAX_LEVEL = 10000;

        Drawable[] sb = tv.getCompoundDrawables();

        ObjectAnimator anim = ObjectAnimator.ofInt(sb[2], "level", 0, MAX_LEVEL);
        anim.start();

    }

}

button_spin_animation.xml:

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

替换ic_add为您的可绘制对象:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

推荐阅读