11

ImageView で AnimationDrawable を使用する方法の Google 提供の例に従っていました。ここで見つけることができます:http://developer.android.com/guide/topics/graphics/drawable-animation.html

imageView.setBackgroundResource(R.drawable.animation);
AnimationDrawable animation = (AnimationDrawable)imageView.getBackground();
animation.start();

実行すると、次のエラーが表示されます。

java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.AnimationDrawable

Google はこれが機能すると考えているようですが、BitmapDrawable を AnimationDrawable にキャストできない場合、これがどのように機能するのかわかりません。

4

6 に答える 6

4

私も同じ問題を抱えていました。このスレッドが数か月前のものであることは知っていますが、誰かが私の経験について何を読むべきか.

理由はわかりませんが、アニメーションに使用している間、Google はピクチャネームで「_」のようなスペースマークを受け入れません。「loading_frame1」などの名前を使用していますが、機能しません。名前を「loadingframe1」のようなものに変更したところ、動作しました....

前:

<?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/loading_frame1" android:duration="100" />
    <item android:drawable="@drawable/loading_frame2" android:duration="100" />
    <item android:drawable="@drawable/loading_frame3" android:duration="100" />
    <item android:drawable="@drawable/loading_frame4" android:duration="100" />
    <item android:drawable="@drawable/loading_frame5" android:duration="100" />
    <item android:drawable="@drawable/loading_frame6" android:duration="100" />
    <item android:drawable="@drawable/loading_frame7" android:duration="100" />
    <item android:drawable="@drawable/loading_frame8" android:duration="100" />
    <item android:drawable="@drawable/loading_frame9" android:duration="100" />
    <item android:drawable="@drawable/loading_frame10" android:duration="100" />
    <item android:drawable="@drawable/loading_frame11" android:duration="100" />
    <item android:drawable="@drawable/loading_frame12" android:duration="100" />
    <item android:drawable="@drawable/loading_frame13" android:duration="100" />
    <item android:drawable="@drawable/loading_frame14" android:duration="100" />
    <item android:drawable="@drawable/loading_frame15" android:duration="100" />
    <item android:drawable="@drawable/loading_frame16" android:duration="100" />
    <item android:drawable="@drawable/loading_frame17" android:duration="100" />
    <item android:drawable="@drawable/loading_frame18" android:duration="100" />
    <item android:drawable="@drawable/loading_frame19" android:duration="100" />
    <item android:drawable="@drawable/loading_frame20" android:duration="100" />

</animation-list>

後:

<?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/loadingframe1" android:duration="100" />
    <item android:drawable="@drawable/loadingframe2" android:duration="100" />
    <item android:drawable="@drawable/loadingframe3" android:duration="100" />
    <item android:drawable="@drawable/loadingframe4" android:duration="100" />
    <item android:drawable="@drawable/loadingframe5" android:duration="100" />
    <item android:drawable="@drawable/loadingframe6" android:duration="100" />
    <item android:drawable="@drawable/loadingframe7" android:duration="100" />
    <item android:drawable="@drawable/loadingframe8" android:duration="100" />
    <item android:drawable="@drawable/loadingframe9" android:duration="100" />
    <item android:drawable="@drawable/loadingframe10" android:duration="100" />
    <item android:drawable="@drawable/loadingframe11" android:duration="100" />
    <item android:drawable="@drawable/loadingframe12" android:duration="100" />
    <item android:drawable="@drawable/loadingframe13" android:duration="100" />
    <item android:drawable="@drawable/loadingframe14" android:duration="100" />
    <item android:drawable="@drawable/loadingframe15" android:duration="100" />
    <item android:drawable="@drawable/loadingframe16" android:duration="100" />
    <item android:drawable="@drawable/loadingframe17" android:duration="100" />
    <item android:drawable="@drawable/loadingframe18" android:duration="100" />
    <item android:drawable="@drawable/loadingframe19" android:duration="100" />
    <item android:drawable="@drawable/loadingframe20" android:duration="100" />

</animation-list>

そして、ここで LoadingAnimation.class リスト

package com.justkidding.animation;

import android.support.v7.app.ActionBarActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class LoadingAnimation extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading_animation);    
    }

    @Override
    public void onWindowFocusChanged (boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        ImageView animation = (ImageView)findViewById(R.id.aniimage);
        animation.setBackgroundResource(R.drawable.loading_animation);
        AnimationDrawable frameAnimation = (AnimationDrawable) animation.getBackground();
        if(hasFocus) {
            frameAnimation.start();
        } else {
            frameAnimation.stop();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.loading_animation, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
于 2015-05-05T08:40:25.177 に答える
2

Google のコードは動作します。私をここに導いた「キャストできない問題」は、注意を払っておらず、animation.xml を res.drawable ではなく res.anim に入れたことが原因でした。

ただし、 setImageDrawable と getDrawable を使用するとうまく機能することに同意します。

于 2016-04-30T10:09:01.217 に答える
0

アニメーションを行うための完全なプロセスは次のとおりです。1. imageView で XML レイアウトを作成し、2. アニメーション用の XML ファイルを作成します。drawable/animation.xml と仮定します。

<?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/twit" android:duration="120"></item>
<item android:duration="120" android:drawable="@drawable/a111"></item>
<item android:duration="120" android:drawable="@drawable/a2"></item>
<item android:duration="120" android:drawable="@drawable/a3"></item>
<item android:duration="120" android:drawable="@drawable/a4"></item>
<item android:duration="120" android:drawable="@drawable/a5"></item>
<item android:duration="120" android:drawable="@drawable/a6"></item>

</animation-list>

3. メイン アクティビティを作成する

次に、このコードを入力します

public class AnimationMe extends AppCompatActivity {
    private ImageView imgView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logoo);
        imgView = (ImageView) findViewById(R.id.imgView);

        // the frame-by-frame animation defined as a xml file within the drawable folder
        /*imgView.setBackgroundResource(R.drawable.animation);*/
        imgView.setImageDrawable(getResources().getDrawable(R.drawable.animation));

     // It's not possible to start the animation during the onCreate.
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        AnimationDrawable animationDrawable = (AnimationDrawable)imgView.getDrawable();
        if(hasFocus)
        {
            animationDrawable.start();
        }
        else
        {
            animationDrawable.stop();
        }
    }
}

*

注 : ImageView は描画可能な背景を持ち、特定の画像ではなく animation.xml という名前を付けてから、AnimationDrawable で imageview.getDrawable を呼び出します。---- onCreate メソッドでアニメーションを実行することはできません。onCreate() で Imageview の drawable プロパティを設定しますが、onCreate() のブロックから AnimationDrawable メソッドを呼び出します。

*

確かにそれはうまくいくでしょう!

于 2018-05-25T08:20:11.640 に答える