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

标签 android overlay imagebutton

是否可以使用 ImageButton 实现触摸和突出显示状态,而不需要在 Android 上多 2 个图像资源(多 6 个,考虑到 h/m/ldpi)?我基本上是在寻找类似于 iOS 的行为,其中操作系统可以在按钮的触摸状态上放置一个半字母覆盖。

我尝试在 onTouch 监听器中使用 setColorFilter(0xFF000000, Mode.MULTIPLY),结果非常接近我想要的 - 但我不确定最好的方法实现此目的的状态处理:

  1. touchDown 事件 -> 改变颜色覆盖。
  2. touchUp 事件 -> 移除颜色叠加,并执行按钮操作。

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

我不想使用单独的图像有几个原因 - 这是一个 iPhone 端口,我还没有合适的 Assets ,考虑到我有低/中/,需要更多的设计师时间需要考虑的高创意。

谢谢!

最佳答案

如果您对 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;
        }
    }
}

然后是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>

关于Android - 自动覆盖触摸/突出显示的图像按钮状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5526915/

相关文章:

android - 多个屏幕的图像按钮大小

android - 我想为以下 Android 游戏使用哪些容器/类?

java - 从不同来源访问相同数据的设计模式

css - 在图像上叠加带有颜色的文本

html - 带过渡的粘性叠加层

ios - 如何确保 iOS MapKit 中叠加层的显示

android - 在 imageButton 中添加图像

android - 每个页面都需要一个 Activity 类吗?

java - Android:创建表时抛出错误

android - 带有关闭按钮的自定义对话框