Android学习笔记-自定义view

来源:互联网 发布:python exe 编辑:程序博客网 时间:2024/05/16 04:00

效果
在android开发中有时会找不到想要的控件样式,这时就需要自定义view了。
首先看一下自定义view的进本步骤:

  1. 继承view,重写onMeasure(),onDraw()方法
  2. 建立atts.xml文件

自定义view

public class ToggleButton extends View {    Bitmap background;    Bitmap toggle;    Paint bgOpenPaint;    Paint bgClosePaint;    Paint tgPaint;    int width;    int height;    float current;    private boolean status=true;    boolean isMove=false;    public ToggleButton(Context context) {        super(context);    }    public ToggleButton(Context context, AttributeSet attrs) {        super(context, attrs);       // ToggleButton(context,attrs,0);        TypedArray a = context.obtainStyledAttributes(attrs,                R.styleable.ToggleButton);        int bgOpenColor=a.getColor(R.styleable.ToggleButton_bgOpenColor,0x0000ff);        int bgCloseColor=a.getColor(R.styleable.ToggleButton_bgCloseColor,0xcccccc);        int tgColor=a.getColor(R.styleable.ToggleButton_tgColor,0xffffff);        bgOpenPaint=new Paint();        bgOpenPaint.setColor(bgOpenColor);        bgClosePaint=new Paint();        bgClosePaint.setColor(bgCloseColor);        tgPaint=new Paint();        tgPaint.setColor(tgColor);    }    public ToggleButton(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onDraw(Canvas canvas) {        if (status) {            canvas.drawCircle(height / 2, height / 2, height / 2, bgOpenPaint);            canvas.drawRect(height / 2, 0, width - height, height, bgOpenPaint);            canvas.drawCircle(width - height, height / 2, height / 2, bgOpenPaint);        } else {            canvas.drawCircle(height / 2, height / 2, height / 2, bgClosePaint);            canvas.drawRect(height / 2, 0, width - height, height, bgClosePaint);            canvas.drawCircle(width - height, height / 2, height / 2, bgClosePaint);        }        if(isMove){            float x;            if(current<height/2){                x=height/2;            }            else if(current>width-height){                x=width-height;            }            else{                x=current;            }            canvas.drawCircle(x,height/2,height/2,tgPaint);        }        else{            if(status){                canvas.drawCircle(width - height, height / 2, height / 2, tgPaint);            }            else{                canvas.drawCircle(height/2, height / 2, height / 2, tgPaint);            }        }       // canvas.drawCircle();    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {      //  super.onMeasure(widthMeasureSpec, heightMeasureSpec);        width=MeasureSpec.getSize(widthMeasureSpec);        height=MeasureSpec.getSize(heightMeasureSpec);        setMeasuredDimension(width,height);    }    /**     * 根据手势重绘view     * @param event     * @return     */    @Override    public boolean onTouchEvent(MotionEvent event) {        current=event.getX();        if(current>width/2)            status=true;        else            status=false;        switch (event.getAction()){            case MotionEvent.ACTION_DOWN:                isMove=true;                break;            case MotionEvent.ACTION_UP:                isMove=false;                break;            case MotionEvent.ACTION_MOVE:                break;        }        invalidate();        return true;    }}

attrs.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="ToggleButton">        <attr name="bgOpenColor" format="color"/>        <attr name="bgCloseColor" format="color"/>        <attr name="tgColor" format="color"/>    </declare-styleable></resources>

在上面的代码中只实现了基本的功能,更多功能可以根据需求自己添加

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 在南京加入嘀嘀代驾怎么办 苹果盗刷支付宝怎么办 街电支付宝掉了怎么办 转转上号器没有苹果版的怎么办 安卓6不支持的应用怎么办 银行卡被存款机吞了怎么办 去银行卡存款机器故障怎么办 银行卡存款没有存进去怎么办 被黑网站黑钱了怎么办 饿了吗账户异常怎么办 单位里有人整你怎么办 乌鲁木齐信息采集结束后怎么办 头发很长时间不长怎么办啊 打嘟噜时间不长怎么办 阴茎勃起的时间不长怎么办 睡觉的时间不长怎么办 每次锻炼时间不长怎么办 微信好友不知道是谁怎么办 就一个软件闪退怎么办 葫芦侠修改闪退怎么办 戴尔游匣磁盘占用100怎么办 步步高家教机扫描笔不亮怎么办 社保卡手机号换了怎么办 香港公司进出口没有申报怎么办 智能电视右上角网络未连接怎么办 电子秤按键板不好用怎么办失灵 桑塔纳车钥匙锁车里了怎么办 舌头上有锯齿印怎么办 夏天有脚气脚臭怎么办 xp分辨率太高黑屏怎么办 农村淘宝标识类目没了怎么办 被投诉到食品监督局怎么办 被客户315投诉了怎么办 给人打不接受调解怎么办 失业证年审忘了怎么办 工商年检过期4天怎么办 个体营业执照年审过期了怎么办 企业年报密码忘了怎么办 税务年报报错了怎么办 工商证过期5年怎么办 车年检标志丢了怎么办