博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android笔记--一个图片+文字的自定义按钮
阅读量:7106 次
发布时间:2019-06-28

本文共 7524 字,大约阅读时间需要 25 分钟。

hot3.png

样图:

174934_DBbh_1462828.png

功能说明,大概意思就是,没按的时候是描边,按下之后是填充,可以自定义设置颜色和文本文字。

代码:

package com.imxiaoyu.common.widget;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.RectF;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.MotionEvent;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.TextView;import com.imxiaoyu.common.R;import com.imxiaoyu.common.utils.ImageUtils;import java.util.Date;/** * 通用的图片+ 文字按钮 * Created by 庞光渝 on 2017/2/18. */public class ImgAndTvButton extends RelativeLayout {    /**     * UI     */    private RelativeLayout rlyIcon;    private ImageView ivBottom;    private ImageView ivIcon;    private TextView tvTitle;    /**     * 变量     */    private boolean isClick = true;//是否可以点击,不可点击状态的话自动设置透明度为半透明    private long touchTime;//按下时间,抬起的时候判定一下,超过300毫秒算点击    private int colorNormal = 0xffcccccc;//正常icon描边颜色    private int colorPress = 0xff999999;//文字按下之后的颜色    /**     * 接口     */    private OnClickListener onClickListener;    public ImgAndTvButton(Context context) {        super(context);        initView(context);    }    public ImgAndTvButton(Context context, AttributeSet attrs) {        super(context, attrs);        initView(context);    }    public ImgAndTvButton(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        initView(context);    }    private void initView(Context context) {        // 加载布局        LayoutInflater.from(context).inflate(R.layout.layout_image_and_txt_buttom, this);        rlyIcon = (RelativeLayout) findViewById(R.id.rly_icon);        ivBottom = (ImageView) findViewById(R.id.iv_bottom);        ivIcon = (ImageView) findViewById(R.id.iv_icon);        tvTitle = (TextView) findViewById(R.id.tv_title);        rlyIcon.post(new Runnable() {            @Override            public void run() {                //保持rlyIcon是正方形                LayoutParams para = (LayoutParams) rlyIcon.getLayoutParams();                para.width = rlyIcon.getHeight();                rlyIcon.setLayoutParams(para);                LayoutParams para1 = (LayoutParams) ivBottom.getLayoutParams();                para.width = rlyIcon.getHeight();                para.height = rlyIcon.getHeight();                ivBottom.setLayoutParams(para1);                setRoundStroke(ivBottom, colorNormal);                if (isClick) {                    ivIcon.setAlpha((float) 1);                    ivBottom.setAlpha((float) 1);                    tvTitle.setAlpha((float) 1);                } else {                    ivIcon.setAlpha((float) 0.5);                    ivBottom.setAlpha((float) 0.5);                    tvTitle.setAlpha((float) 0.5);                }                tvTitle.setTextColor(colorNormal);            }        });    }    /**     * 设置图标     *     * @param icon     */    public void setButtonIcon(int icon) {        ivIcon.setImageResource(icon);    }    /**     * 设置图标     *     * @param bitmap     */    public void setButtonIcon(Bitmap bitmap) {        ivIcon.setImageBitmap(bitmap);    }    /**     * 设置点击时间     *     * @param onButtomClickListener     */    public void setOnButtonClickListener(OnClickListener onButtomClickListener) {        onClickListener = onButtomClickListener;    }    /**     * 设置是否可以点击     *     * @param bln     */    public void setIsClick(boolean bln) {        isClick = bln;        if (isClick) {            ivIcon.setAlpha((float) 1);            ivBottom.setAlpha((float) 1);            tvTitle.setAlpha((float) 1);        } else {            ivIcon.setAlpha((float) 0.5);            ivBottom.setAlpha((float) 0.5);            tvTitle.setAlpha((float) 0.5);        }    }    /**     * 返回TextView对象,需要怎么设置样式自己设置     *     * @return     */    public TextView getTvTitle() {        return tvTitle;    }    /**     * 设置文字     *     * @param str     */    public void setTitle(String str) {        if (str == null) {            str = "";        }        tvTitle.setText(str);    }    /**     * 设置图标背景颜色样式     *     * @param roundColor     */    public void setIconColorStyle(int roundColor) {        this.colorNormal = roundColor;        setRoundStroke(ivBottom, roundColor);    }    /**     * 设置文字颜色样式     *     * @param normalColorTv     * @param pressColorTv     */    public void setTvColorStyle(int normalColorTv, int pressColorTv) {        this.colorPress = pressColorTv;        tvTitle.setTextColor(normalColorTv);    }    /**     * 给一个控件设置一个纯色的圆形背景图片     *     * @param iv     * @param color     */    private void setRoundColor(final ImageView iv, final int color) {        rlyIcon.post(new Runnable() {            @Override            public void run() {                Bitmap bitmap = Bitmap.createBitmap(rlyIcon.getWidth(), rlyIcon.getHeight(),                        Bitmap.Config.ARGB_8888);                bitmap.eraseColor(color);//填充颜色                bitmap = ImageUtils.bitmap2RoundBitmap(bitmap);                iv.setImageBitmap(bitmap);            }        });    }    /**     * 画一个描边     *     * @param iv     * @param color     */    private void setRoundStroke(final ImageView iv, final int color) {        rlyIcon.post(new Runnable() {            @Override            public void run() {                int number = 4;//描边宽度                Bitmap bitmap = Bitmap.createBitmap(rlyIcon.getHeight(), rlyIcon.getHeight(),                        Bitmap.Config.ARGB_8888);                Canvas canvas = new Canvas(bitmap);                RectF rect1 = new RectF(number, number, rlyIcon.getHeight() - number, rlyIcon.getHeight() - number);                Paint paint = new Paint();                paint.setAntiAlias(true);                paint.setStrokeWidth(number);                paint.setStyle(Paint.Style.STROKE);                paint.setColor(color);                canvas.drawArc(rect1, 0, 360, true, paint);                iv.setImageBitmap(bitmap);            }        });    }    /**     * 监听触摸时间,用于更新状态以及响应点击监听     * @param event     * @return     */    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                touchTime = new Date().getTime();                if (isClick) {                    setRoundColor(ivBottom, colorNormal);                    tvTitle.setTextColor(colorPress);                }                break;            case MotionEvent.ACTION_UP:                if (new Date().getTime() - touchTime < 300) {                    //点击回调                    if (onClickListener != null) {                        onClickListener.onClick(ImgAndTvButton.this);                    }                }                if (isClick) {                    setRoundStroke(ivBottom, colorNormal);                    tvTitle.setTextColor(colorNormal);                }                break;        }        return true;    }}

布局:

调用:

itbPower.setButtonIcon(R.drawable.ic_infrared_power);        itbPower.setTitle("电源");        itbPower.setOnButtonClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                ToastUtils.showToast(getActivity(), "电源");            }        });

http:doutugongchang.com

转载于:https://my.oschina.net/u/1462828/blog/856509

你可能感兴趣的文章
EXCEL显示 文件未保存 解决方法
查看>>
编写一个日志轮询归档脚本
查看>>
Mesh Shading
查看>>
测试职业规划和发展----从零开始到合格的测试工程师
查看>>
UIImageView上添加UIButton,button的点击事件无法响应
查看>>
网络编程学习——Unix域协议
查看>>
队列的使用
查看>>
Java Annotation入门
查看>>
java--序列化及其算法透析
查看>>
GenMyModel:拥有代码生成功能的法国创新型UML工具来袭
查看>>
nexus 手册
查看>>
【hadoop】15.HDFS-其他功能
查看>>
照葫芦画瓢-reading files(读文件)
查看>>
HTTP协议详解(转)
查看>>
Android零基础入门第54节:视图切换组件ViewSwitcher
查看>>
083-使用shell和expect一键批量分发SSH密钥脚本
查看>>
线上采购注意了
查看>>
压缩与打包
查看>>
配置Tomcat虚拟主机
查看>>
Vue.JS 开发常见问题集锦
查看>>