android图片gif动画效果,android中类似于gif 实现图片的动画效果

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

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

案例:在android中实现gif动态图片的效果:08be868a9077a389a4301c76ee8932ed.png

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() {

@Override

public 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()

*/

@Override

protected void onResume() {

super.onResume();

(new Timer(false)).schedule(new AnimationTimer(earthButtonAnimation), 100);

}

/* (non-Javadoc)

* @see android.app.Activity#onPause()

*/

@Override

protected void onPause() {

super.onPause();

earthButtonAnimation.stop();

}

private static class AnimationTimer extends TimerTask {

AnimationDrawable animation;

public AnimationTimer(AnimationDrawable animation) {

this.animation = animation;

}

@Override

public void run() {

animation.start();

this.cancel();

}

}

}

main.xml

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" android:id="@+id/frame_layout"

android:background="@drawable/space_background">

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" >

earth_animation_rev.xml

android:oneshot="false" android:visible="true">

earth_animation.xml

android:oneshot="false" android:visible="true">

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值