Android - 自动覆盖触摸/突出显示图像按钮状态

Cod*_*odz 11 android overlay imagebutton

是否可以使用ImageButton来触摸和突出显示状态,而不需要在Android上再增加2个图像资源(另外6个,考虑到h/m/ldpi)?我基本上都在寻找类似于iOS的行为,其中操作系统可以在按钮的触摸状态下放置半alpha覆盖.

我已经尝试setColorFilter(0xFF000000, Mode.MULTIPLY)在onTouch监听器中使用,结果与我之后的结果非常接近 - 但我不确定状态处理的最佳方式来实现这一点:

  1. touchDown事件 - >更改颜色叠加.
  2. touchUp event - >删除颜色叠加,并执行按钮操作.

有更好的方法......或者有人可以帮助填补空白吗?

我不想使用单独的图像有几个原因 - 它是一个iPhone端口,我还没有适当的资产,并且需要更多的设计师时间考虑我有低/中/高创意到考虑.

谢谢!

Cor*_*ott 1

如果您对此感到满意,setColorFilter(0xFF000000, Mode.MULTIPLY)那么让自己成为一个扩展 ImageButton 的自定义组件怎么样?

就像是:

(抱歉我没有机会测试)

package com.example;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;

public class IosImageButton extends ImageButton implements OnTouchListener
{

    public IosImageButton(final Context context, final AttributeSet attrs, final int defStyle)
    {
        super(context, attrs, defStyle);
        this.init();
    }

    public IosImageButton(final Context context, final AttributeSet attrs)
    {
        super(context, attrs);
        this.init();
    }

    public IosImageButton(final Context context)
    {
        super(context);
        this.init();
    }

    private void init()
    {
        super.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(final View view, final MotionEvent event)
    {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                // touchDown event -> Change color overlay. goes here
                // e.g. this.setColorFilter(0xFF000000, Mode.MULTIPLY);
                return true;

            case MotionEvent.ACTION_UP:
                // touchUp event -> remove color overlay, and perform button action.
                // e.g. this.setColorFilter(0xFF000000, Mode.MULTIPLY);
                return true;

            default:
                return false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后是view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.example.IosImageButton 
        <!-- add standard ImageButton setting here -->
        />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)