2014-01-20 39 views
0

我有這種奇怪的行爲,我無法找到解決方案... 這隻發生在我的模擬器API級別10,在我的智能手機(Android 4.1)上工作正常。Android:AnimationDrawable擠壓我的形象

我寫了一個動態動畫來展示一個滾動的骰子,其中10個隨機幀被選爲動畫的幀,持續時間爲50ms。

當我按下按鈕,動畫運行,但擠壓到零高度.....(你仍然可以看到動畫骰子的一些顏色),佈局也會搞砸。 雖然這不會發生在我的手機上。

Before animation During animation

這裏是Java代碼的所述部分:

public void RollDice(View view) { 
    int id=-1; 
     AnimationDrawable diceAnimation = new AnimationDrawable(); 

     //create 10 random frames from dice1.jpg to dice6.jpg into diceAnimation 
     for(int j=0;j<10;j++){ 
      id=getResources().getIdentifier("dice"+String.valueOf(rng.nextInt(6)+1), "drawable", getPackageName()); 
      diceAnimation.addFrame(getResources().getDrawable(id), 50); 
     } 

     //assigning and starting animation 
     diceAnimation.setOneShot(true); 
     dice.setImageDrawable(diceAnimation); 
     diceAnimation.start(); 
} 

和XML的部分:

<ImageView 
android:id="@+id/Dice1" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:contentDescription="@string/diceimage" 
android:src="@drawable/dice1" /> 

按鈕的onclick調用RollDice()。

至於最後再說一句,如果我的ImageView硬編碼,說的android:layout_height = 「100dp」,那麼骰子動畫中顯示,但streched成有趣的形狀......

During animation with 100dp

我試着大多數縮放方法,adjustviewbound等沒有運氣...

+0

有幫助嗎? :( – Ivan

回答

0

可能有一個答案給你,謹慎試用,因爲我是一個初學者自己。

ViewGroup.MarginLayoutParams mParams = new ViewGroup.MarginLayoutParams(view.getLayoutParams()); 

ImageView view = (ImageView)findViewById(R.id.image_dice); 
layoutParams = new RelativeLayout.LayoutParams(mParams); 
layoutParams.width=120; 
layoutParams.height=120; 
view.setLayoutParams(layoutParams); 

應用,爲您的圖像將調整對飛行的.xml,至少它爲我工作,當我從網上抓取不同大小的圖像的很多。

1

我不知道你的問題是否解決了。 但我想添加這個問題的答案。首先

,設置你的圖像視圖layout_width從match_parent爲wrap_content:

你可以試試這個。

其次利用這個下面的代碼,從而使骰子完全消失:

frameAnimation.setVisibility(消失);