是否可以使用 ImageButton 实现触摸和突出显示状态,而不需要在 Android 上多 2 个图像资源(多 6 个,考虑到 h/m/ldpi)?我基本上是在寻找类似于 iOS 的行为,其中操作系统可以在按钮的触摸状态上放置一个半字母覆盖。
我尝试在 onTouch 监听器中使用 setColorFilter(0xFF000000, Mode.MULTIPLY)
,结果非常接近我想要的 - 但我不确定最好的方法实现此目的的状态处理:
即
- touchDown 事件 -> 改变颜色覆盖。
- 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/