Background animated image

前端 未结 1 1556

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

相关标签:
1条回答
  • 2021-02-10 09:26

    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

    0 讨论(0)
提交回复
热议问题