Background animated image

2019-03-20 18:24发布

问题:

I am wondering how I can do the following. I want my app background to be animated (let's say birds flying sort of what live wall papers look like). Is this usually done using GIF background or do I need to code animation or use paint/draw functionality to draw animations (I hope not!)

Any pointer is appreciated as I am not sure how to do that

Thank you

回答1:

You can do it by using GIF image also, but first convert that GIF to frames

in xml

<ImageView 
    android:layout_width="match_parent"
android:layout_height="match_parent" 
android:id="@+id/img1"
/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/img1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="156dp"
    android:text="submit"
     />

in java

public class MainActivity extends Activity {

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

    ImageView i = (ImageView)findViewById(R.id.img1);
    i.setBackgroundResource(R.drawable.gif);

    AnimationDrawable pro = (AnimationDrawable)i.getBackground();
    pro.start();
}   
}

in drawable create animation file

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/selected" android:oneshot="false">  
  <item android:drawable="@drawable/frame0" android:duration="50" />
  <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" />
  <item android:drawable="@drawable/frame6" android:duration="50" />
  <item android:drawable="@drawable/frame7" android:duration="50" />
  <item android:drawable="@drawable/frame8" android:duration="50" />
  <item android:drawable="@drawable/frame8" android:duration="50" />
  <item android:drawable="@drawable/frame9" android:duration="50" />
  <item android:drawable="@drawable/frame10" android:duration="50" />

</animation-list>

Hope this might help you