2012-08-26 38 views

回答

19

我對android動畫並不是很熟悉,但其中一個(有點駭人聽聞的)方法是將圖像包裝在ClipDrawable中,並對其level值進行動畫處理。例如:

<ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:src="@drawable/clip_source" /> 

clip_source是繪製:

<?xml version="1.0" encoding="utf-8"?> 
<clip xmlns:android="http://schemas.android.com/apk/res/android" 
    android:clipOrientation="vertical" 
    android:drawable="@drawable/your_own_drawable" 
    android:gravity="bottom" /> 

然後在代碼中你會:

// a field in your class 
private int mLevel = 0; 

ImageView img = (ImageView) findViewById(R.id.imageView1); 
mImageDrawable = (ClipDrawable) img.getDrawable(); 
mImageDrawable.setLevel(0); 
mHandler.post(animateImage); 

animateImageRunnable對象:

private Runnable animateImage = new Runnable() { 

     @Override 
     public void run() { 
      doTheAnimation(); 
     } 
    }; 

doTheAnimation方法:

private void doTheAnimation() { 
    mLevel += 1000; 
    mImageDrawable.setLevel(mLevel); 
    if (mLevel <= 10000) { 
     mHandler.postDelayed(animateImage, 50); 
    } else { 
     mHandler.removeCallbacks(animateImage); 
    } 
} 
+0

有趣!在這種情況下,mHandler是什麼? – ibiza

+0

@ibiza它是'Handler'實例,'Handler mHandler = new Handler();'。 – Luksprog

+0

我似乎無法使這種方法的工作,我總是看到完整的圖像 – ibiza