ImageView 被 setAlpha() 隐藏后不想再出现

发布于 2024-11-30 09:50:20 字数 1133 浏览 5 评论 0原文

我有一个定义为可绘制的形状:

<?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 都会消失,但只出现动画的 ImageView在第二个。消失后的线条根本不想显示。我想要一个有效的切换方法。

I have a shape defined as a 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>

and I use this shape in an ImageView as source:

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

The problem appears when I want to toggle my 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);
        }
    }

The result is that on first execution of this method both ImageViews disappears but only the animated one appears on the second. The line since disappearing don't want to show at all. I would like to have a working toggle method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初心未许 2024-12-07 09:50:20

This might be better accomplished with View.setVisibility(), use View.INVISIBLE if you still want the ImageView layed-out or View.Gone if you want it to ignore layout.

无声静候 2024-12-07 09:50:20

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

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

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

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

So it was really weird behaviour of ImageView. After I made some layout changes in the layout file, which were for sure unrelated (I've checked) to this issue the code started to work. Anyone knows why this happened?

Anyway, I've discovered this after pasting this code above my blueLine in layout xml. The previous line started to work then. After cutting it up again it still works.

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

Eventually, I decided to stay with this new version of line, abandoning shapes...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文