ImageView 被 setAlpha() 隐藏后不想再出现
我有一个定义为可绘制的形状:
<?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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 View.setVisibility(),如果您仍然想要 ImageView,请使用 View.INVISIBLE布局或 View.Gone 如果你希望它忽略布局。
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.
所以这确实是 ImageView 的奇怪行为。在我在布局文件中进行了一些布局更改(这肯定与此问题无关(我已经检查过))后,代码开始工作。有谁知道为什么会发生这种情况?
无论如何,在将这段代码粘贴到布局 xml 中的 blueLine 上方后,我发现了这一点。之前的线路开始工作。再次切开后仍然有效。
最终,我决定继续使用新版本的线条,放弃形状......
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.
Eventually, I decided to stay with this new version of line, abandoning shapes...