2013-12-17 35 views
34

我有動畫的Android動畫阿爾法

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/linear_interpolator"> 
    <alpha 
     android:fromAlpha="0.2" 
     android:toAlpha="1.0" 
     android:duration="500"/> 
</set> 

和ImageView的

<ImageView 
    android:id="@+id/listViewIcon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/settings" 
    android:alpha="0.2"/> 

和代碼:

final Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha); 
final ImageView iv = (ImageView) findViewById(R.id.listViewIcon); 
anim .setFillAfter(true); 
iv.startAnimation(anim); 

所以一開始我有阿爾法0.2,並在ImageView的結束我想要AlphaView的ImageView。但它不這樣工作 - 當動畫開始更多的Alpha添加和動畫finis h with alpha 0.2

我必須改變以使圖像從0.2-> 1變爲動畫?

我檢查了不同的設置 - 我設置了android:alpha =「1.0」,從alpa =「1.0」toAlpha =「0.2」它像我期望的那樣工作 - 從alpha 1到0.2。它看起來像從ImageView的阿爾法是從動畫阿爾法乘...

+0

爲防止視圖在最後跳回到0.2 alpha,請使用'fillAfter'屬性:http://developer.android.com/reference/android/view/animation/Animation.html#attr_android:fillAfter – Jave

+0

事實並非如此。阿爾法不會去1.當我從1-> 0.2動畫,它工作正常,並保持在0.2(我使用填充後)。當我想從0.2到1進行動畫時,它會消失到接近0並且達到0.2 – wioskamala

+0

您是否已將fillEnabled設置爲true? – Jave

回答

64

試試這個

AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f); 
animation1.setDuration(1000); 
animation1.setStartOffset(5000); 
animation1.setFillAfter(true); 
iv.startAnimation(animation1) 
+1

同樣的結果就像從企業的性質imageview的在我的例子... – wioskamala

+0

reoveα和uninsatll應用從您的設備。清理,構建並重新安裝。因爲它在我的身邊 –

+0

我不想刪除阿爾法 - 圖像應該與阿爾法0.2褪色。 Aplication做一些東西,並已準備好時,我想有我影像動畫阿爾法1 – wioskamala

1
<ImageView 
    android:id="@+id/listViewIcon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/settings"/> 

從XML-> ImageView的刪除android:apha=0.2

1

唔...

的事情是錯誤的,並可能在Android API在動畫的正常運轉。

的事實是,當你在0.2F的代碼中的α值設定是基於Android的這意味着在XML文件中的設置是:

0.2f = 0.2f of 0.2f (20% from 100%) ie from 0.2f/5 = 0.04f 
1f = 0.2f 

所以你其實動畫作品0.04 F到0.2F

標誌setFillAfter(true)肯定的作品,但你要明白,你的動畫ImageView年底將有Alpha值0.2F而不是一個,因爲你指定0.2F爲輕微ACC動畫中的eptable值(一種最大的alpha通道)。

所以,如果你想有所需的結果應該把所有的邏輯運載到你的代碼中,並在代碼中操作動畫而不是在xml中確定。

你應該明白,你的動畫直接取決於兩件事情:

的動畫視圖
  • 的LayoutParams
  • 動畫參數。

動畫參數操縱你的LayoutParams在setFillAfter \ setFillBefore方法。

+0

是順利完美。你是對的。當我在我的代碼中更改開始佈局參數時,它就像一個魅力! – 2015-03-07 14:09:04

9

開始動畫之前設置阿爾法1工作對我來說:

AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f); 
animation1.setDuration(500); 
iv.setAlpha(1f); 
iv.startAnimation(animation1); 

至少在我的測試中,沒有因爲開始動畫之前設置阿爾法閃爍。它工作正常。

+0

這也適用於我(編輯:我的錯誤,我有XML干擾:android:alpha =「0」)。會稍微玩一下,但我確實需要它從0開始,所以現在這個工作。謝謝。 –