2011-04-03 76 views
11

使用的ImageButton有向下触摸和高亮状态是否有可能的,而不在Android需要2个图像资源(6以上,考虑到H/M/LDPI)?我基本上寻找类似于iOS的行为,其中操作系统可以在按钮的触摸状态下放置半alpha叠加层。的Android - 自动叠加降落/突出显示的ImageButton指出

我在onTouch监听使用setColorFilter(0xFF000000, Mode.MULTIPLY)试过,结果是相当接近我是后 - 但我不知道该状态的最佳方式处理,以实现这一目标:

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

有没有更好的方法......还是有人可以帮助填补空白?

我不希望使用单独的图像的一对夫妇的原因 - 它是我没有合适的资产,但iPhone的端口,就需要更多的设计师考虑的时间我已经得到了低/中/高创意要考虑。

谢谢!

回答

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; 
     } 
    } 
} 

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

不要使用触摸事件来试试在这样的事情上显示适当的状态。你将不可避免地让它部分错误。该框架做了大量的工作来检测用户意图(例如:用户接触下来,但然后滑下他的手指)考虑到像坡道等事情

对于ImageButton,您可以使用src属性来显示图标然后使用适当的StateListDrawable作为背景。

我创建了一个快速的项目给你看on Github

注意,触摸状态的背景下,没有前台显示。关于Android框架工程师为什么认为触摸反馈应该落后而不在前面的讨论on Google+。如果您仍然希望drawable在前面,请参阅我的文章on my blog