在您的 Activity 类中声明这些变量
private ImageView myFrameAnimation;
private int iFrameCount;
private ImageView myPhotoLayer;
使用 onStart 或按钮点击事件启动动画
@Override
protected void onStart() {
super.onStart();
Thread thr1 = new Thread(r1);
thr1.start();
}
然后创建一个新线程来做动画
Runnable r1 = new Runnable() {
public void run() {
while (iFrameCount< 747) {
runOnUiThread(new Runnable() {
public void run() {
iFrameCount++;
switch (iFrameCount) {
//you can use a separate procedure here to move the ImageView with your picture in time with your animation
//the x,y cordinates l depend on your screen/View size measured from top left corner.
case 310: animate_object(330,215,310,215,1);
case 315: animate_object(150,380,150,420,80);
case 320: animate_object(80,150,80,120,100);
}
//store your animation images in the drawables folder with the name like img1.png, img2.png etc.
String image="img" + iFrameCount;
resID = getResources().getIdentifier(image, "drawable", getPackageName());
if (resID != 0){
//Use these to time how long it takes to update the imageview/ Set your thread.sleep to around this time.
//startTime = System.currentTimeMillis();
myFrameAnimation.setImageResource(resID);
//endTime = System.currentTimeMillis();
//Log.d("Frame: " +iFrameCount + " setBackground took: " + (endTime - startTime) + " milliseconds");
} else {
//we can skip frames if they are duplicates of the frame before to save space and memory and increase speed
//Log.d(TAG, "File skipped: " + iFrameCount);
}
}
});
try {
Thread.sleep(43); //43 is milliseconds to give around 23 frames per second speed will depend on your image size that you are animating.
} catch (InterruptedException iex) {}
}
//Log.d(TAG, "Finished all frames");
finish();
}
};
最后有一个程序可以将照片与相框一起制作动画
public void animate_object(int startx, int starty, int endx, int endy, int duration)
{
//photoLayer is the Id of your imageView holding the photo. You would need to have previously loaded your photo into the imageview using
//something like this myPhotoLayer.setImageResource(resID); before the animation is started.
myPhotoLayer = (ImageView) findViewById(R.id.photoLayer);
TranslateAnimation move = new TranslateAnimation(startx, endx, starty, endy);
move.setDuration(duration);
move.setFillAfter(true);
myPhotoLayer.startAnimation(move);
}