`
810364804
  • 浏览: 782777 次
文章分类
社区版块
存档分类
最新评论

编写可复用的自定义按钮

 
阅读更多

转载:http://marshal.easymorse.com/archives/4606

Android的布局,要比iOS复杂的多。如果想写出和iOS类似的交互体验,付出的代价往往要增加一个数量级。

现在有个正在开发的Android项目,里面已经有了一些不合理的UI实现方式。比如按钮是一张图:

可以看出,应该用编程的方式来实现这个按钮,比如xml声明drawable,一个矩形框,四个边是圆角,要有个很细的边框,黑色的,背景色使用渐进色效果。登录使用文字而不是在图形里。

这样的好处很多:

  • 自由的在不同分辨率屏幕下做适配,不必考虑图形的长宽比;
  • 当文字改动后,不必喊上美工一起加班处理;
  • 文字的国际化。

不过,本文只想对原项目做稍微的改动,而不想推倒重来,我打算另写一篇文章来说明上述方案的实现。

本文方案的基本思路是,还是用这个图,但是增加复用性,开发者只需在布局中使用自定义按钮,就可以让已经存在的这种布局具备点击后高亮的效果,而不必准备多张图,写冗长的xml文件做selector。

实现后的效果,在手指触碰到该按钮的时候:

抬起或者移动到按钮外区域恢复原来的样子。

这里布局还是在xml中,类似这样:

<com.witmob.CustomerButton
    android:id=”@+id/login”
    android:layout_width=”wrap_content”
    android:layout_height=”wrap_content”
    android:layout_alignParentLeft=”true”
    android:layout_centerVertical=”true”
    android:layout_marginLeft=”26dp”
    android:background=”@drawable/login_login_but”/>

实现的按钮代码:

package com.witmob;

import android.content.Context;
import android.graphics.LightingColorFilter;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class CustomerButton extends Button {
   
    public CustomerButton(Context context) {
        super(context);
        this.init();
    }

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

    public CustomerButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.init();
    }
   
    private void init(){
        this.setOnTouchListener(new OnTouchListener() {
           
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action=event.getActionMasked();
                switch (action) {
                case MotionEvent.ACTION_DOWN:
                    getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x000000FF));
                    getBackground().invalidateSelf();
                    break;
                case MotionEvent.ACTION_UP:
                    getBackground().clearColorFilter();
                    getBackground().invalidateSelf();
                    break;
                case MotionEvent.ACTION_MOVE:
                    Rect rect=new Rect();
                    v.getDrawingRect(rect);
                   
                    if(!rect.contains((int)event.getX(),(int)event.getY())){
                        getBackground().clearColorFilter();
                        getBackground().invalidateSelf();
                    }
                    break;
                default:
                    break;
                }
                return false;
            }
        });
    }

}

代码要点:

  • 需要使用OnTouchListener,处理手指按下,抬起和移动到区域外的处理;
  • 使用ColorFilter,获取背景色的Drawable对象,增加颜色过滤;
  • 操作Rect,结合手指坐标,判断是否在区域内部;
  • 另外,需要返回false,在OnTouchListener,否则按钮的OnClickListener将不能生效。
备注:这确实是很好的方法,这样就不需要做很多按下和抬起的效果了,哎毕竟不是每个公司都有美工的,而且代码是可以复用的
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics