Creating a frame animation in android

2019-05-29 20:27发布

问题:

Hi im trying to set up a frame animation with a series of images as a background for my splash page in an android application, I would if possible like the code to run on start up. I have been writing the code but get an error that says "frame animation cannot be resolved".

The java code I have implemented in my main activity is as follows:

@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageView frameanimation = (ImageView) findViewById(R.id.frame_animation);
        AnimationDrawable frame_animation = (AnimationDrawable) frameanimation.getBackground();

        frame_animation.setVisible(true, true);
        frame_animation.start();
    }

I then have this code implementing an image view within my XML Layout file:

<ImageView
        android:id="@+id/frame_animation"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/frame_animation" />

and finally I have my animation drawable saved as 'frame_animation.xml' within my drawable folder.

<?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/homebckgrnda1" android:duration="200" />
    <item android:drawable="@drawable/homebckgrnda2" android:duration="50" />
    <item android:drawable="@drawable/homebckgrnda3" android:duration="50" />
    <item android:drawable="@drawable/homebckgrnda4" android:duration="50" />
    <item android:drawable="@drawable/homebckgrnda5" android:duration="50" />
    <item android:drawable="@drawable/homebckgrnda6" android:duration="50" />
    <item android:drawable="@drawable/homebckgrnda7" android:duration="50" />
    <item android:drawable="@drawable/homebckgrnda8" android:duration="50" />
    <item android:drawable="@drawable/homebckgrnda9" android:duration="50" />
    <item android:drawable="@drawable/homebckgrnda10" android:duration="50" />
    <item android:drawable="@drawable/homebckgrnda11" android:duration="50" />
    <item android:drawable="@drawable/homebckgrnda12" android:duration="50" />
    <item android:drawable="@drawable/homebckgrnda13" android:duration="50" />
    <item android:drawable="@drawable/homebckgrnda14" android:duration="50" />
    <item android:drawable="@drawable/homebckgrnda15" android:duration="50" />
    <item android:drawable="@drawable/homebckgrnda16" android:duration="50" />
    <item android:drawable="@drawable/homebckgrnda17" android:duration="50" />
</animation-list>

Any help is much appreciated, I just cannot figure out what is going wrong.

Thanks

回答1:

Source of ImageView (android:src) and background of ImageView (android:background) are different things. You are setting the source in XML but trying to get the background image in the code (frameanimation.getBackground()).