带有ImageView的Android动画

时间:2012-09-09 15:12:16

标签: android android-layout animation android-animation android-2.2-froyo

我有一个相框,应该用其中的图片进行动画处理。我将使用imageView,我的背景将是框架,我的图片将是前景图像。我必须以一种方式显示它,其中一个相框从顶部向下落到屏幕中间,并在其顶部附加一条色带。在将其放入中心后,只有带图像的框架执行toFro运动,而色带保持刚性且不移动。我该怎么做这样的动画?

由于

2 个答案:

答案 0 :(得分:1)

在您的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);
}

答案 1 :(得分:0)

你需要像卡通一样画画。首先尝试视频以获得所需的移动,然后创建PNG序列(使用Adobe After Effects),将所需的帧作为指南,然后在Illustrator或Photoshop中重绘它们。

然后使用animation.drawable创建动画,只要没有太多帧就显示帧。