私はdrawableフォルダにフレーム(frame1.png ... frame5.png)を持っていて、drawableフォルダの中にbg_animation.xmlファイルを作成しました。Android Studioで背景をアニメーション化する方法は?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<animation-list android:id="@+id/backganimate" android:oneshot="false">
<item android:drawable="@drawable/frame1" android:duration="50" />
<item android:drawable="@drawable/frame2" android:duration="50" />
<item android:drawable="@drawable/frame3" android:duration="50" />
<item android:drawable="@drawable/frame4" android:duration="50" />
<item android:drawable="@drawable/frame5" android:duration="50" />
</animation-list>
</selector>
私はthis codeを使用しようと、それは次のようになります。私は、アプリを起動すると
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/backg" />
</RelativeLayout>
:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img = (ImageView)findViewById(R.id.backg);
img.setBackgroundResource(R.drawable.bg_animation);
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
frameAnimation.start();
}
}
XMLでImageViewのがあり、それはこのようになります私の電話(4.4.2)で、このエラーでクラッシュします:
java.lang.RuntimeException: Unable to start activity ComponentInfo{hu.media.smk.test/hu.media.smk.test.MainActivity}: java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.AnimationDrawable