我有这种奇怪的行为,我找不到解决方案......这只发生在我的模拟器 API 级别 10 中,在我的智能手机(Android 4.1)上工作正常。
我写了一个动态动画来展示一个滚动的骰子,其中随机选择 10 帧作为动画的帧,持续时间为 50 毫秒。
当我按下按钮时,动画运行,但被挤到零高度......(你仍然可以从动画骰子中看到一些颜色),布局也变得一团糟。这不会发生在我的手机上。
这是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",那么骰子将在动画期间显示,但拉伸成有趣的形状......
我尝试了大多数缩放方法,adjustviewbound 等但没有运气......