ImageView不希望在被setAlpha()隐藏后出现

时间:2011-08-19 22:27:56

标签: android transparency imageview shape

我有一个定义为drawable的形状:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
       android:shape="rectangle">
    <solid android:color="#0075b5"/>
    <size android:height="2dp"/>
</shape>

我在ImageView中将此形状用作源:

<ImageView android:layout_height="2dp"
           android:layout_width="fill_parent"
           android:src="@drawable/shape_blue_line"
           android:id="@+id/ptt_blueLineImageView"/>

当我想切换我的ImageView时会出现问题:

private void toggleAnimatedLogo() {
        if (viewFlipper.getDisplayedChild() == 2) {
            animatedLogo.setAlpha(ALHPA_TRANSPARENT);
            blueLine.setAlpha(ALHPA_TRANSPARENT);
        } else {
            animatedLogo.setAlpha(ALHPA_VISIBLE);
            blueLine.setAlpha(ALHPA_VISIBLE);
        }
    }

结果是,在第一次执行此方法时,两个ImageView都会消失,但只有动画的一个出现在第二个上。消失后的线路根本不想显示。我想要一个有效的切换方法。

2 个答案:

答案 0 :(得分:1)

使用View.setVisibility()可能会更好,如果您仍想要ImageView布局,请使用View.INVISIBLE,如果您希望忽略布局,请使用View.Gone

答案 1 :(得分:0)

所以这是ImageView的奇怪行为。在布局文件中进行了一些布局更改后,这个问题肯定与此问题无关(我已经检查过),代码开始工作。谁知道为什么会这样?

无论如何,我在布局xml中将此代码粘贴到blueLine上方之后发现了这一点。之前的一行开始起作用了。在再次切割后它仍然有效。

<View android:layout_height="2dp"
      android:layout_width="fill_parent"
      android:id="@+id/ptt_blueLineView"
      android:background="#0075b5"/>

最终,我决定继续使用这个新版本的线条,放弃形状......