android - 更改图像按下状态下的不透明度

标签 android android-layout

我使用 ImageView 作为按钮。 对于正常状态,它是简单的红色图像,上面有文字,在按下状态下,我想改变图像的不透明度。

enter image description here

现在处于新闻状态,我想降低此图像的不透明度。 为此,我知道的选项是 1.创建另一个具有所需不透明度的图像并使用选择器获得效果 2. 在选择器中为两种状态使用颜色代码; 但是这里我已经有一个图像状态作为图像,对于下一个状态我只想降低此图像中的不透明度。

最佳答案

button.setOnTouchListener(this);
@Override
public boolean onTouch(View v, MotionEvent event) {
  if (v == button) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
      v.setAlpha(0.5f);
    } else {
      v.setAlpha(1f);
    }
  }
  return false;
}

关于android - 更改图像按下状态下的不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16986402/

相关文章:

安卓 : stroke in a shape create a margin of the stroke width

android - 在 fragment 中动态添加 TextView

android - Spinner 和 WheelView 在一起

android - 无法在 ubuntu 上的 android studio 1.4 中解析 : junit:junit:4. 12

android - 悬停查看

Android - 避免布局重复

android - 解码未知类型代码

android - 反序列化包含数组的 Firestore 文档的推荐方法是什么?

android - 右下角的背景图片?

java - Android:无法从原始查询填充 ListView