案例:实现gif动画效果,连续播放图片

由于是转载的,也就没必要多说,直接上代码

案例:在android中实现gif动态图片的效果:android中类似于gif 实现图片的动画效果_第1张图片

EarthAnimationActivity.java

package com.codtech.android.samples.earthanimation;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.view.View;import android.view.Window;import android.view.View.OnClickListener;import android.widget.Button;public class EarthAnimationActivity extends Activity {    private static final String TAG = "EarthAnimationActivity";protected static final int FORWARD = 0;protected static final int REVERSE = 1;private Button earthButton;private AnimationDrawable earthButtonAnimation;protected int direction;/** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.main);        earthButton = (Button) findViewById(R.id.earth_button);        earthButtonAnimation = (AnimationDrawable) earthButton.getBackground();        direction = FORWARD;                earthButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if ( ! earthButtonAnimation.isRunning() ) {earthButtonAnimation.start();earthButton.setText(R.string.click_me_to_stop);}else {earthButtonAnimation.stop();int resId = R.anim.earth_animation_rev;if ( direction == FORWARD ) {direction = REVERSE;}else {resId = R.anim.earth_animation;direction = FORWARD;}earthButton.setBackgroundResource(resId);earthButtonAnimation = (AnimationDrawable) earthButton.getBackground();earthButton.setText(R.string.click_me_to_start);}}                });    }/* (non-Javadoc) * @see android.app.Activity#onResume() */@Overrideprotected void onResume() {super.onResume();(new Timer(false)).schedule(new AnimationTimer(earthButtonAnimation), 100);}/* (non-Javadoc) * @see android.app.Activity#onPause() */@Overrideprotected void onPause() {super.onPause();earthButtonAnimation.stop();}private static class AnimationTimer extends TimerTask {AnimationDrawable animation;public AnimationTimer(AnimationDrawable animation) {this.animation = animation;}@Overridepublic void run() {animation.start();this.cancel();}}    }


main.xml

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent" android:id="@+id/frame_layout"android:background="@drawable/space_background"><Button android:id="@+id/earth_button"android:text="@string/click_me_to_stop" android:layout_gravity="center"android:textStyle="bold" android:textColor="#ff9900"android:textSize="24dip"android:layout_marginBottom="12dip" android:layout_marginRight="12dip"android:layout_marginLeft="-30dip"android:layout_marginTop="12dip" android:maxHeight="296dip"android:maxWidth="296dip" android:layout_height="wrap_content"android:layout_width="wrap_content" android:width="296dip"android:height="296dip" ></Button></FrameLayout>


earth_animation_rev.xml

<?xml version="1.0" encoding="UTF-8"?><!-- Animation frames for earth --><!--  android:id="@+id/earth_animation_rev" --><animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false" android:visible="true"><item android:drawable="@drawable/earth9" android:duration="150" /><item android:drawable="@drawable/earth8" android:duration="150" /><item android:drawable="@drawable/earth7" android:duration="150" /><item android:drawable="@drawable/earth6" android:duration="150" /><item android:drawable="@drawable/earth5" android:duration="150" /><item android:drawable="@drawable/earth4" android:duration="150" /><item android:drawable="@drawable/earth3" android:duration="150" /><item android:drawable="@drawable/earth2" android:duration="150" /><item android:drawable="@drawable/earth1" android:duration="150" /><item android:drawable="@drawable/earth0" android:duration="150" /></animation-list>


earth_animation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Animation frames for earth -->
<!-- android:id="@+id/earth_animation" -->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" android:visible="true">
<item android:drawable="@drawable/earth0" android:duration="150" />
<item android:drawable="@drawable/earth1" android:duration="150" />
<item android:drawable="@drawable/earth2" android:duration="150" />
<item android:drawable="@drawable/earth3" android:duration="150" />
<item android:drawable="@drawable/earth4" android:duration="150" />
<item android:drawable="@drawable/earth5" android:duration="150" />
<item android:drawable="@drawable/earth6" android:duration="150" />
<item android:drawable="@drawable/earth7" android:duration="150" />
<item android:drawable="@drawable/earth8" android:duration="150" />
<item android:drawable="@drawable/earth9" android:duration="150" />
</animation-list>

更多相关文章

  1. 刚开始安卓,记录一个刚做的图片缩放程序
  2. Android基础小技术点:Android ListView设置背景图片及分割线、周
  3. Android -- 加载大图片的方法
  4. Android WebView 支持H5图片上传
  5. Android上传图片(PHP服务器)
  6. android沉浸式状态栏底部背景用图片代替
  7. 浅入浅出Android(015):使用ImageView显示网络图片
  8. Android 实现ListView 3D效果 - 1
  9. Android 水波纹点击效果

随机推荐

  1. Android Compile Error Do not Accept Li
  2. Android Studio实现人民币与美元的转换
  3. Android(安卓)SDK作用和结构
  4. Android7.0中文文档(API)-- AdapterView.On
  5. Android Button的背景图片拉伸变形解决方
  6. 好久米有碎碎念啦 android how to click
  7. 2020-08-07
  8. 关于Android的Activity的onCreate多次调
  9. 【Android】_MyContentProvider_外部访问
  10. Android日记之三