gpt4 book ai didi

android - 现有 ImageView 上的 AnimationDrawable 不会设置动画

转载 作者:行者123 更新时间:2023-11-29 16:03:07 25 4
gpt4 key购买 nike

我有以下代码,用于在应用另一个动画后向 ImageView 添加动画。

动画永远不会开始。相关代码如下:

// I'm Using Jake Wharton's ButterKnife,a view "injection" library.
@InjectView(R.id.record_button)
public ImageView recordButtonView;

protected void onCreate(Bundle savedInstance) {
...
ButterKnife.inject(this);
pulsate = AnimationUtils.loadAnimation(this.parent, R.anim.pulsate);
recordButtonView.startAnimation(pulsate)
...
}

@OnClick(R.id.record_button)
protected void onButtonClick( ) {
...
// Clear the old animation
recordButtonView.clearAnimation();

// Start the new animation
recordButtonView.setBackgroundResource(R.drawable.record_button_animation);
AnimationDrawable recordButtonAnimation = (AnimationDrawable) recordButtonView.getBackground();
recordButtonAnimation.setCallback(recordButtonView);
recordButtonAnimation.setVisible(true, true);
recordButtonAnimation.start();

// At this point the animation should be looping. Nothing happens
...
}

这里是record_button_animation.xml(保存在res/drawable)

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/sing_btn_record" android:duration="500"/>
<item android:drawable="@drawable/sing_btn_record_activated" android:duration="500"/>
</animation-list>

这里是对应的布局文件

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

<!-- all sibling elements are removed for clarity -->

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/stem">

<!-- all sibling elements are removed for clarity -->

<ImageView
android:id="@+id/record_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sing_btn_record"
android:scaleType="center"
android:layout_centerInParent="true"/>
</RelativeLayout>
</RelativeLayout>

最佳答案

试试这个

ImageView recordButton = (ImageView) findViewById(R.id.imageViewPulse);
recordButton.setImageBitmap(null);
recordButton.setBackgroundResource( R.anim.pulse );
final AnimationDrawable mailAnimation = (AnimationDrawable) recordButton.getBackground();
recordButton.post(new Runnable() {
public void run() {
if ( mailAnimation != null ) mailAnimation.start();
}
});

关于android - 现有 ImageView 上的 AnimationDrawable 不会设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22031935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com