2014-01-20 43 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(消失);