如何解决如何创建可绘制的右侧动画按钮
我如何创建一个正确的旋转按钮动画,我尝试了下面的代码,但它一直崩溃。
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
解决方法
我为您编写了一个解决方案。 MainActivity.java
类包含 2 个函数,它们将运行动画并停止动画。看看这里:
public class MainActivity extends AppCompatActivity {
public static ImageView ImageIcon;
public static boolean aBoolean = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageIcon = findViewById(R.id.imageView);
Button button = findViewById(R.id.btn);
button.setOnClickListener(v -> {
if(!aBoolean) {
runBtnAnimation();
aBoolean = true;
}
else {
stopBtnAnimation();
aBoolean = false;
}
});
}
private void runBtnAnimation() {
ImageIcon.setImageResource(R.drawable.ic_baseline_flutter_dash_24);
Animation rotation = AnimationUtils
.loadAnimation(getApplicationContext(),R.anim.rotation_animation);
ImageIcon.startAnimation(rotation);
}
private void stopBtnAnimation() {
ImageIcon.clearAnimation();
ImageIcon.setImageResource(R.drawable.ic_baseline_flutter_dash_24);
}
}
之后,您将创建 activity_main.xml
布局。我想指出的是,您需要将具有不同 Button
级别的 ImageView
和 elevation
放在彼此之上,放入 frame
(此处为 FrameLayout
) :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<Button
android:id="@+id/btn"
android:layout_width="200dp"
android:layout_height="100dp"
android:elevation="0dp"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:src="@drawable/ic_baseline_flutter_dash_24"
android:elevation="4dp"/>
</FrameLayout>
</RelativeLayout>
最后一部分是在位于 rotation_animation.xml
文件夹中的 res/anim
中配置动画行为:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<rotate
android:duration="1000"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toDegrees="359"/>
</set>
结果
在本例中,ImageView
将在被点击后旋转 infinite
。动画在再次点击后停止,set
是之前的 ImageResource
。但是您可以随意使用 function
runBtnAnimation()
和 stopBtnAnimation()
。干杯!
您需要进行 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",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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。