我使用ImageView作为按钮。 在正常状态下,它是一个简单的红色图像,上面有文本,在按下状态下,我想改变图像的透明度。
现在在按下状态下,我想减少这个图像的透明度。 要做到这一点,我知道的选项是 1.创建具有所需透明度的其他图像,并使用选择器获得效果 2.在选择器中为两种状态使用颜色代码; 但是在这里,我已经有一个图像状态作为图像,对于下一个状态,我只想在这个图像中减少透明度。
我使用ImageView作为按钮。 在正常状态下,它是一个简单的红色图像,上面有文本,在按下状态下,我想改变图像的透明度。
现在在按下状态下,我想减少这个图像的透明度。 要做到这一点,我知道的选项是 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;
}
@Override
public void setPressed(boolean pressed) {
super.setPressed(pressed);
setAlpha(pressed ? 0.5f : 1.0f);
}
button.getBackground().setAlpha(60);
<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>
我不得不稍微修改上面的正确答案以使其按照我的需求工作:当您取消触摸时,只有不透明度应该改变,而不应该闪烁。
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;
}
我的类用于在按下时更改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"
/>
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
}
v.setAlpha(.5f);
现在接受int
,所以正确的方式是v.setAlpha(128);
,而v.setAlpha(1f);
将会是v.setAlpha(255);
。 - Blue Bot