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

7

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

enter image description here

现在在按下状态下,我想减少这个图像的透明度。 要做到这一点,我知道的选项是        1.创建具有所需透明度的其他图像,并使用选择器获得效果        2.在选择器中为两种状态使用颜色代码; 但是在这里,我已经有一个图像状态作为图像,对于下一个状态,我只想在这个图像中减少透明度。

7个回答

8
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;
}

你好,我看到了你的博客,非常棒。你有一个切换按钮的帖子,能否查看一下这个问题:http://stackoverflow.com/questions/16986875/how-to-move-toogle-button-label-move-to-right-side - Bora
我认为 v.setAlpha(.5f); 现在接受 int,所以正确的方式是 v.setAlpha(128);,而 v.setAlpha(1f); 将会是 v.setAlpha(255); - Blue Bot

2
创建一个自定义的ImageView类,即AlphaImageView,它继承自ImageView,并重写setPressed()方法,如下所示:
@Override
public void setPressed(boolean pressed) {
    super.setPressed(pressed);
    setAlpha(pressed ? 0.5f : 1.0f);
}

2
您可以使用标志来存储状态,并调用此函数来更改不透明度:
button.getBackground().setAlpha(60);

我知道这个,但是当按下状态被移除时,它应该自动恢复到正常状态,如何使用选择器实现这一点。 - Bora
那么您可以使用复选框,而不是按钮。它支持点击监听器,在其中您可以检查当前状态。 - Michael Cheremuhin
如果您需要在一个用户交互期间更改不透明度,请使用@selva的解决方案。如果您需要保持按钮按下一段时间(例如,直到异步调用完成),则复选框将很有帮助。 - Michael Cheremuhin

1
您还可以使用Android选择器(它不需要您创建自定义控件或逻辑)。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
      <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:alpha="0.3" android:src="@drawable/your_cancel_button_image" />
    </item>
    //other selector states 
</selector>

这对我完美地起作用了,它也适用于drawableLeft等等。有人知道它是否存在缺陷或怪异之处吗? - now

1

我不得不稍微修改上面的正确答案以使其按照我的需求工作:当您取消触摸时,只有不透明度应该改变,而不应该闪烁。

ImageButton imageButton = (ImageButton) findViewById(viewId);
imageButton.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;
}

0

我的类用于在按下时更改ImageView的透明度

public class PressableImageView extends AppCompatImageView {
    private static final float DEFAULT_ALPHA_WHEN_PRESS = 0.5f;
    private static final float DEFAULT_ALPHA = 1f;

    public PressableImageView(Context context) {
        super(context);
    }

    public PressableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PressableImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private void refresh() {
        if (isPressed()) {
            setAlpha(DEFAULT_ALPHA_WHEN_PRESS);
            invalidate();
            return;
        }
        setAlpha(DEFAULT_ALPHA);
        invalidate();
    }

    @Override
    public void setPressed(boolean pressed) {
        super.setPressed(pressed);
        refresh();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
            setPressed(true);
        } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
            setPressed(false);
            float x = event.getX();
            float y = event.getY();
            boolean isInside = (x > 0 && x < getWidth() && y > 0 && y < getHeight());
            if(isInside){
                performClick();
            }
        }
        return true;
    }
}

使用

<....PressableImageView
    android:id="@+id/imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"
    />

0
ImageButton imageButton = (ImageButton) findViewById(viewId);     
imageButton.setOnTouchListener(this);

@Override
public boolean onTouch(View v, MotionEvent event)
{
     if(event.getAction() == MotionEvent.ACTION_DOWN)
    {
         v.setAlpha(.5f);
    } 
    else if (event.getAction() == MotionEvent.ACTION_UP)
    {
        v.setAlpha(1f);
    }
    return true;    //make shure to return true
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接