资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

编写可复用的自定义按钮

编写可复用的自定义按钮

创新互联专注于临高企业网站建设,响应式网站建设,商城建设。临高网站建设公司,为临高等地区提供建站服务。全流程定制网站设计,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

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

 编写可复用的自定义按钮

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

这样的好处很多:

· 自由的在不同分辨率屏幕下做适配,不必考虑图形的长宽比;

· 当文字改动后,不必喊上美工一起加班处理;

· 文字的国际化。

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

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

 编写可复用的自定义按钮

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

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

    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将不能生效。

 


网页名称:编写可复用的自定义按钮
标题来源:http://cdkjz.cn/article/ggodjs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220