0
以下程序顯示使用loadAnimation()方法自動進行動畫處理的全屏圖像。這是代碼。如何停止圖像動畫?
public class MainActivity extends ActionBarActivity {
/** Called when the activity is first created. */
public int currentimageindex=0;
Timer timer;
TimerTask task;
ImageView slidingimage;
private int[] IMAGE_IDS = {
R.drawable.picture1, R.drawable.picture2, R.drawable.picture3,
R.drawable.picture4
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Handler mHandler = new Handler();
// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
AnimateandSlideShow();
}
};
int delay = 1000; // delay for 1 sec.
int period = 3000; // repeat every 4 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
}, delay, period);
}
/**
* Helper method to start the animation on the splash screen
*/
private void AnimateandSlideShow() {
slidingimage = (ImageView)findViewById(R.id.ImageView3_Left);
slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
currentimageindex++;
Animation leftToRight = AnimationUtils.loadAnimation(this, R.anim.left_to_right);
slidingimage.startAnimation(leftToRight);
}
}
正如您所看到的全尺寸圖片每4秒更換一次。現在我想要做的就是在最後一張圖片出現後停止動畫(本例中爲圖片4)。解決這個問題後,我會嘗試從使用JSON的服務器獲取圖像。
謝謝 Theo。
是。那做了這個工作。非常感謝。我把你的答案標記爲一個:) – Theo 2015-03-31 14:05:20