自定义View button

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

声明的方式编写可复用按钮

编写可复用的自定义按钮中,通过增加背景图的方式编写了按钮。这种方式一般应用于按钮是很特殊的图形的时候。

如果仅仅是编写可复用的自定义按钮中的按钮,可以看出,有边界、背景有渐进色,那么可以通过声明的方式获得更好的可配置特性。

这是生成的按钮:

这是按下后的按钮:

按钮的编写次序是,先在ui界面中实现最简单的按钮。样子类似是这样:

布局文件类似这样:

<Button

    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:focusable=“true”
    android:text=“登录” 
    />

这样,可以快速开始编写ui和ue,直到项目的后期,再对具体的按钮做定制处理。这类似html和css的关系。具体做法是,使用Android布局的Style,为按钮增加风格。 

 

<Button
    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:focusable=“true”
    android:text=“登录” 
    style=“@style/ButtonText”/>

在按钮标签上增加style属性。style属性指向的是res/values目录下的文件,可以任意合法命名的xml文件,

<Button
    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:focusable=“true”
    android:text=“登录” 
    style=“@style/ButtonText”/>

文件中类似这样:

 

<?xml version=“1.0″ encoding=“utf-8″?>
<resources>
    <style name=“ButtonText”>
        <item name=“android:layout_width”>fill_parent</item>
        <item name=“android:layout_height”>wrap_content</item>
        <item name=“android:textColor”>#ffffff</item>
        <item name=“android:gravity”>center</item>
        <item name=“android:textStyle”>bold</item>
        <item name=“android:shadowColor”>#000000</item>
        <item name=“android:shadowDx”>1</item>
        <item name=“android:shadowDy”>1</item>
        <item name=“android:shadowRadius”>2</item>
        <itemname=“android:background”>@drawable/customer_button_selector</item>
    </style>
</resources>

这里的背景色,使用的还是配置文件,在res/drawable目录下,创建名为customer_button_selector.xml文件:

<?xml version=“1.0″ encoding=“utf-8″?>
<selectorxmlns:android=“http://schemas.android.com/apk/res/android”>
    <item android:state_pressed=“true”>
        <shape >
            <solid android:color=“#993434″/>
            <stroke android:width=“1dp” android:color=“#171717″/>
            <corners android:radius=“3dp”/>
            <padding android:left=“30dp” android:right=“30dp”android:top=“10dp” android:bottom=“10dp”/>
        </shape>
    </item>
<item >
    <shape >
        <gradient android:startColor=“#343434″android:endColor=“#171717″ android:angle=“270″/>
        <stroke android:width=“1dp” android:color=“#171717″/>
        <corners android:radius=“3dp”/>
        <padding android:left=“30dp” android:right=“30dp”android:top=“10dp” android:bottom=“10dp”/>
    </shape>
</item>
</selector>

这个背景色配置文件,配置了按钮的形状,默认矩形,渐进色,按钮边界,以及点击后的变色等。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴为您解答这个问题。自定义View下载按钮的实现可以通过以下步骤完成: 1. 创建一个自定义View,并在其中添加一个按钮。 2. 在按钮的点击事件中,使用Android的DownloadManager类来下载文件。 3. 在下载完成后,使用Android的Intent类来打开下载的文件。 以下是一个简单的示例代码: ``` public class DownloadButtonView extends View { private Button mDownloadButton; public DownloadButtonView(Context context) { super(context); init(); } public DownloadButtonView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public DownloadButtonView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mDownloadButton = new Button(getContext()); mDownloadButton.setText("Download"); mDownloadButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { downloadFile(); } }); addView(mDownloadButton); } private void downloadFile() { DownloadManager downloadManager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE); Uri uri = Uri.parse("http://example.com/file.pdf"); DownloadManager.Request request = new DownloadManager.Request(uri); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); downloadManager.enqueue(request); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { mDownloadButton.layout(left, top, right, bottom); } } ``` 当用户点击按钮时,downloadFile()方法将被调用,该方法使用DownloadManager类来下载文件。在下载完成后,用户可以通过点击通知栏中的下载完成通知来打开下载的文件。 希望这个示例代码能够帮助您实现自定义View下载按钮。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值