android - 如何创建可绘制的右动画按钮
问题描述
如何创建正确的旋转按钮动画,我尝试了下面的代码,但它一直在崩溃。
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
解决方案
您需要进行 3 处更改:
- 将drawable添加到您的
button_spin_animation
- 如果您想获得正确的可绘制对象,请使用第二个索引。
- 用于
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>
推荐阅读
- javascript - 如何使用 javascript 从 html 保存/写入文件?
- svg - Parcel - 添加 svg 精灵
- verilog - 注册文件不读取任何数据
- c# - 我们如何在 dotnetnuke 中不使用密码登录
- winforms - Winforms WebBrowser 控件没有 IE 弹出窗口不出现
- audio - 创建我自己的个人音频格式需要哪些步骤?
- javascript - 将简单的 ReactJS FORM 发布到 Express JS 服务器时不断出错
- binary-tree - 为 Leetcode 112 寻找正确的递归算法
- powerbi - PBI 如何将日期字段映射到另一个动态表
- reactjs - 在 React JSX 中单击按钮时滚动到组件