如何停止图像动画?
How to stop images animation?
以下程序使用 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.
从服务器获取图像
谢谢,
西奥.
1)您必须加载动画,直到 IMAGE_IDS 数组中只有图像
2) 然后也停止你的计时器
在 AnimateandSlideShow() 下面的 currentimageindex++
添加以下行
if(currentimageindex <= IMAGE_IDS.length)
{
Animation leftToRight = AnimationUtils.loadAnimation(this, R.anim.letf_anim);
slidingimage.startAnimation(leftToRight);
}
else
{
timer.cancel();
}
同时替换以下行
Timer timer = new Timer();
到
timer = new Timer();
以下程序使用 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.
从服务器获取图像谢谢, 西奥.
1)您必须加载动画,直到 IMAGE_IDS 数组中只有图像
2) 然后也停止你的计时器
在 AnimateandSlideShow() 下面的 currentimageindex++
添加以下行if(currentimageindex <= IMAGE_IDS.length)
{
Animation leftToRight = AnimationUtils.loadAnimation(this, R.anim.letf_anim);
slidingimage.startAnimation(leftToRight);
}
else
{
timer.cancel();
}
同时替换以下行
Timer timer = new Timer();
到
timer = new Timer();