2012-12-13 32 views
6

我試圖給ImageView設置動畫,所以它從左到右緩慢地顯示。 當I asked this before我有麻煩解釋一下我想要的東西,所以這一次我創建使用HTML/JS預期的效果:Animate ImageView width without scaling

http://jsfiddle.net/E2uDE/

什麼將得到Android的這種效果的最佳方法是什麼?

我試圖改變scaleType,然後直接應用ScaleAnimation到ImageView的:

佈局:

<ImageView 
    android:id="@+id/graphImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:scaleType="centerCrop" 
    android:background="@android:color/transparent" 
    android:contentDescription="@string/stroom_grafiek" 
    /> 

的Java:

scale = new ScaleAnimation((float)0, 
     (float)1, (float)1, (float)1, 
     Animation.RELATIVE_TO_SELF, (float)0, 
     Animation.RELATIVE_TO_SELF, (float)1); 
scale.setDuration(1000); 

graphImage.startAnimation(scale); 

但這STIL縮放圖片。

我也試過在一個FrameLayout裏包裹ImageView的,希望我可以動畫的FrameLayout:

<FrameLayout 
    android:layout_width="70dp" 
    android:layout_height="fill_parent" 
    android:clipChildren="true""> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|left|clip_horizontal" /> 
</FrameLayout> 

這仍然會嘗試擴大我的ImageView以適合的FrameLayout裏面。

+0

也許使用Android動畫會幫助你? –

回答

9

有幾種方法可以做到這一點 - 一種方法是簡單地更改繪製圖像的畫布上的剪輯(即允許繪製視圖的區域)的ImageView。緩慢增加繪製邊界的右邊將產生與示例鏈接中相同的效果。

我寫了一些example code說明了這種技術。如果你下載/構建/運行項目,點擊圖像將運行揭示動畫。

看一看在CoveredImageView類的onDraw方法,其具有以下基本結構:

@Override 
protected void onDraw(Canvas canvas) { 
    ... 
    canvas.getClipBounds(mRect); 
    mRect.right = ...; 
    canvas.clipRect(mRect); 
    ... 
    super.onDraw(canvas); 
    ... 
    invalidate(); 
} 

夾子被調整,然後無效()被調用,從而引起的onDraw()來再次調用。

我也寫了一些代碼來插入從經過的時間,這不依賴於任何特定的Android版本的權利剪輯值。但是,從Android 3.0開始,有一個新的動畫框架和一個可以幫助完成這些任務的類。

+0

@antonyt你給我的Github鏈接沒有gen文件夾以及它支持哪個版本它沒有闡明我如何使用該項目來查看動畫,根據我Gen文件夾是自動生成的,但它不是這樣做 –

+0

我隨意選擇API級別7(v2.1)作爲最低限度,但它可能會降低 - 這是在清單中指定的。但是,該項目使用API​​ 17(4.2)進行編譯。Gen在編譯時會自動創建 - 也許您在構建項目時遇到問題 - 請查看Eclipse中的「Problems」窗口以獲取更多詳細信息。 – antonyt

0

你可以重疊你的imageview與另一個,例如一個純白色drawable。然後,您可以動畫重疊的圖像視圖,而不必擔心縮放,直到其寬度爲0並將其刪除。爲了這個工作,你必須把你的imageView放在一個RelativeLayout ofc中。