【问题标题】:how to change the alpha value of imageview如何改变imageview的alpha值
【发布时间】:2013-03-16 01:50:44
【问题描述】:

在我的应用程序中,我想如下更改 ImageView。 我只想更改身体上的图像。

我已经按照以下链接使用类似的代码更改 alpha 值

imageView.setAlpha(255);

但它不起作用。

How to Set Opacity (Alpha) for View in Android。 请任何人建议我如何做到这一点。

【问题讨论】:

  • 所以发布你的代码你在做什么
  • 在 0 到 1 之间传递 alpha 值
  • 感谢您的回复

标签: android image alpha


【解决方案1】:

也许你可以这样尝试,我在正常方式下做起来有困难,但使用“动画”就可以了。

AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F); // change values as you want
alpha.setDuration(0); // Make animation instant
alpha.setFillAfter(true); // Tell it to persist after the animation ends
// And then on your imageview
yourImageView.startAnimation(alpha);

【讨论】:

  • 谢谢你它工作正常。我的要求是我想要不同的变化。我怎样才能改变那个阿尔法值。请给我建议
  • 在AlphaAnimation中(第一个参数是起点,所以完整的alpha是1.0,第二个参数是终点,简单的0是透明的);
【解决方案2】:

或者,不使用动画,您可以在 ImageView 上使用 setAlpha(float alpha)

【讨论】:

  • setalpha 适用于 api 11+!
  • 每当我尝试通过这种方法放置 alpha 时,我都看不到任何变化! iv.setAlpha(50f) 或 iv.setImageAlpha(50) 没有任何反应?为什么
  • 如 android developer doc 中所写:。这是一个从 0 到 1 的值,其中 0 表示视图完全透明,1 表示视图完全不透明。您应该尝试使用 setAlpha(0.2f)
【解决方案3】:

这可能是因为setAlpha(float Alpha) 在 API 级别 16 中已被setImageAlpha(float Alpha) 弃用

【讨论】:

  • setAlpha(float) 没有被弃用,只有setAlpha(int)
【解决方案4】:

不透明度介于 0 到 1 之间,您已将值设为 255,
imageView.setAlpha(255)

所以给出0到1之间的值,比如当

你想要 50% 的不透明度,给 0.5。 imageView.setAlpha(0.5f)

您也可以在 XML 文件中执行如下操作:

android:layout_width="100dip"
android:layout_height="100dip"
android:id="@+id/imageViewIcon"
android:src="@drawable/icon"
android:alpha=".5"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-05
    • 2016-05-04
    • 2011-07-10
    • 2022-04-13
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多