0

我有这种奇怪的行为,我找不到解决方案......这只发生在我的模拟器 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",那么骰子将在动画期间显示,但拉伸成有趣的形状......

在 100dp 动画期间

我尝试了大多数缩放方法,adjustviewbound 等但没有运气......

4

2 回答 2

1

不知道你的问题解决了没有。但我想添加这个问题的答案。

你可以试试这个:

首先,将图像视图 layout_width 从 match_parent 设置为 wrap_content。

其次使用下面的代码使骰子完全消失:

frameAnimation.setVisibility(GONE);

于 2017-06-11T20:40:27.363 回答
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,至少当我从网络上抓取许多不同大小的图像时它对我有用。

于 2014-10-10T11:36:36.920 回答