我想为Android创建不规则形状的图像按钮,基于按钮图像的透明区域。
基本上,如果您单击按钮的透明区域,而不是处理事件,它应该将其“向下”传播到自身下面的视图。因此,如果将其中两个按钮重叠,则实际上可以单击'bottom'按钮(由'top'重叠),如果单击'top'的透明区域,但单击'bottom'的非透明区域。
基于iOS相同原理的一种工作解决方案是OBShapedButton http://www.cocoacontrols.com/platforms/ios/controls/obshapedbutton
我的第一个想法是创建一个ImageButton子类(CustomShapedButton)来实现这个功能。考虑到实际的可绘制状态和MotionEvent,下面的代码可以用于确定是否单击透明的是。
public class CustomShapedButton extends ImageButton {
[...]
if (e.getAction() == MotionEvent.ACTION_UP)
{
// click coordinates of (MotionEvent e)
float x = e.getX();
float y = e.getY();
// get current state of drawable and the color under the click
StateListDrawable s = (StateListDrawable)this.getBackground();
int color = ((BitmapDrawable)s.getCurrent()).getBitmap().getPixel((int)x, (int)y);
Log.d(TAG, "custom shaped button clicked at x: "+x+" y: "+y+" with color: "+color);
if (color != 0)
{
// normal region clicked, should count as click
// ...
}
else
{
// transparent region clicked, should NOT count as click
// ...
}
}
[...]
}为了获得正确的功能,我应该把上面的代码放在哪里?我试图重写public boolean onTouchEvent(MotionEvent e)和public boolean dispatchTouchEvent(MotionEvent e)方法,但没有得到任何结果(可能只是没有为每种情况找到正确的返回值)。
基于Android文档,public boolean interceptTouchEvent(MotionEvent e)看起来是这类行为的可能解决方案,但它是在ViewGroup类中定义的,因此不能用于ImageView子类。
如果您有处理/发布触摸事件的经验,请回答,并知道我问题的答案!
发布于 2013-05-16 23:25:07
我在控制触觉方面也遇到过类似的问题。我的解决方案是扩展一个WebView,以便它能够处理自己的操作。我也用一个大TextView做过同样的事情。您可以在这个the文件中找到第一个源:
http://this-voice.org/dnlds/MoDaBrowserGNU.zip
至于纽扣,我不知道这会不会有帮助。但是,我在普通按钮上放置图像的结果与ImageButton不同。你可能会在那里找到一些能满足你需要的东西。另外,您可能已经尝试过了,但是您可以使用XML中的边距将一个按钮放在另一个按钮上。至于将按压向下传递,您必须在类似于我的WebView解决方案的情况下拦截新闻坐标,并“模拟”重叠。我用引号是因为所有这些东西都是模拟的,对吧?让用户觉得他在做你想让他体验的事情。引擎盖下面有什么并不重要。
https://stackoverflow.com/questions/14561782
复制相似问题