android - CardView 上具有相对/线性布局的动画图像
问题描述
我想要一个我想在CardView
. 我ImageView
在那个视图中放置了一个LinearLayout
,它显示它在那里,但动画根本没有播放。我检查了很多其他类似问题的帖子并尝试实施修复,但它不会这样做。我检查了我的图像是否都相同,它们不是,并且一次又一次地命名事物。我觉得我错过了一些超级简单但无法解决的问题。
这是我的 activity_lock_reminder.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/trans_black"
android:gravity="center"
tools:context="com.antbicycle.project.antbicycle.LockReminderActivity">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="10dp"
app:cardBackgroundColor="@color/greenTintWhite"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:elevation="6dp"
android:layout_alignParentLeft="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Remember to Lock Your Bike"
android:textAlignment="center"
android:textStyle="bold"
android:textSize="25dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
<ImageView
android:id="@+id/lockAnimation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:src="@drawable/animation"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:text="@string/reminder_advisory"
android:textSize="18dp"
android:textAlignment="center"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="@string/reminder_note"
android:textColor="@color/grayOut"
android:textAlignment="center" />
<Button
android:id="@+id/btn_dismissReminder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:text="dismiss"
android:background="@drawable/button_green_round"
android:textColor="@color/ln_colorWhite" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
这是我的 LockReminder.java
import android.app.Activity;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.AnimationDrawable;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class LockReminderActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lock_reminder);
}
@Override
public void onWindowFocusChanged(boolean hasFocus){
if(hasFocus){
final ImageView anm = (ImageView) findViewById(R.id.lockAnimation);
((Animatable) anm.getDrawable()).start();
}
}
}
还有我的animation.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true"
android:visible="true">
<item android:drawable="@drawable/lock_0" android:duration="200"/>
<item android:drawable="@drawable/lock_1" android:duration="200"/>
<item android:drawable="@drawable/lock_2" android:duration="200"/>
<item android:drawable="@drawable/lock_3" android:duration="200"/>
<item android:drawable="@drawable/lock_4" android:duration="200"/>
<item android:drawable="@drawable/lock_5" android:duration="200"/>
<item android:drawable="@drawable/lock_6" android:duration="200"/>
<item android:drawable="@drawable/lock_7" android:duration="200"/>
<item android:drawable="@drawable/lock_8" android:duration="200"/>
<item android:drawable="@drawable/lock_9" android:duration="200"/>
<item android:drawable="@drawable/lock_10" android:duration="200"/>
<item android:drawable="@drawable/lock_11" android:duration="200"/>
<item android:drawable="@drawable/lock_12" android:duration="200"/>
<item android:drawable="@drawable/lock_13" android:duration="200"/>
<item android:drawable="@drawable/lock_14" android:duration="200"/>
<item android:drawable="@drawable/lock_15" android:duration="200"/>
<item android:drawable="@drawable/lock_16" android:duration="200"/>
<item android:drawable="@drawable/lock_17" android:duration="200"/>
<item android:drawable="@drawable/lock_18" android:duration="200"/>
<item android:drawable="@drawable/lock_19" android:duration="200"/>
</animation-list>
这是显示后的卡片视图:
锁只是一个静止图像,而不是通过动画列表。
解决方案
根据文档:https ://developer.android.com/reference/android/graphics/drawable/AnimationDrawable
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView img = (ImageView)findViewById(R.id.lockAnimation);
img.setBackgroundResource(R.drawable.animation);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
// Start the animation (looped playback by default).
frameAnimation.start();
推荐阅读
- android - Android,如何从同一个 ViewModel 中观察 LiveData
- mongodb - 当您知道键但不知道值时,访问 mongo 对象内的值
- react-native - 如何在 React-Native for Web 上更改 React-Native-Elements 库的 Tab 组件上的选项卡颜色?
- php - Laravel如何在虚拟主机上加载上传的图片
- json - 将json响应添加到flutter中的列表
- javascript - 有没有比 setTimeOut(fn, 0) 更好的选择?
- python - 解决生成位置参数冲突类型错误的函数调用
- javascript - 未捕获的类型错误:无法使用 JS 读取 null 的属性“addEventListener”以获得平滑滚动效果
- android - android studio 不显示布局预览新版本(Android Studio 北极狐 | 2020.3.1)
- node.js - 如何使用 React/TypeScript 阅读 MSK 集群的主题?