0

我有一個圖片框應該與其中的圖片動畫。我將使用imageView,其中我的背景將是框架,我的圖片將成爲前景圖像。我必須以一種相框從頂部到屏幕中間落下的方式顯示它,頂部帶有一個條帶。將其放入中央後,只有帶圖像的框架才能進行平移運動,而色帶保持剛性且不會移動。我該如何製作這樣的動畫?Android動畫與ImageView

謝謝

回答

0

您需要畫卡通畫。嘗試先錄製視頻以獲得所需的移動,然後創建PNG序列(使用Adobe After Effects)將所需的幀作爲指南,然後在Illustrator或Photoshop中重新繪製它們。

然後使用animation.drawable創建一個動畫來顯示幀,只要它沒有太多的幀。

+0

謝謝,但我需要在運行時集成一張圖片,即從照片庫或相機拍攝照片,然後將其添加到動畫中。所以這可能不適合我。 – Ahmed

+0

嗨布拉特,你想選擇或拍照,然後讓它出現在屏幕上已經在相框? 如果是這種情況,您可以像我上面提到的那樣創建動畫。首先拍下它來獲得運動。然後在動畫程序中繪製它(如Flash或After Effects)。創建一個幀的PNG圖像序列(如卡通)。儘可能縮小尺寸(不是全屏)。那麼... – birdman

+0

要麼使用animation.drawable(如果它足夠小),要麼一次一個循環地切換PNG(這就是我所做的),然後讓您的圖片在圖片框架頂部的不同Imageview中生成動畫動畫層,然後逐幀順序移動頂部圖像視圖,使其停留在框架內部並隨之擺動。這應該做到這一點。 – birdman

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(); 
    } 
}; 

終於有了一個程序來動畫照片以及你的照片ture frame

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); 
} 
+0

我編輯了一些我使用的現有代碼,所以希望我在正確的位置爲您提供了所有的括號。 – birdman