前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[android] 切换按钮-自定义控件

[android] 切换按钮-自定义控件

作者头像
唯一Chat
发布2019-09-10 15:31:05
1.6K0
发布2019-09-10 15:31:05
举报
文章被收录于专栏:陶士涵的菜地陶士涵的菜地

准备两张图片,按钮背景,上面的小开关

创建一个类MyToggleBtn,继承View

实现三个构造方法,传递上下文,

实现构造方法,传递Context对象,在java代码中实例化时主要使用这个

实现构造方法,传递Context对象,AttributeSet对象,在布局文件中主要使用

View对象显示在屏幕上,有几个重要步骤

1.构造方法创建对象

2.测量view的大小 onSeasure(int,int)

3.确定view的位置,view自身有一些建议权,决定权在父view手中 onLayout()

4.绘制view的内容 onDraw(Canvas)

构造方法,初始化view,

调用BitmapFactory.decodeResurce()方法,把图片资源转成Bitmap对象,参数:Resource对象(getResources()),资源id

重写onMesaure()方法,

不要调用父类

调用setMeasuredDimension()方法,参数:宽度,高度;调用背景Bitmap对象的getWidth()和getHeight()

重写onDraw()方法,传递进来Canvas对象

调用Canvas对象的drawBitmap()方法,参数:Bitmap对象,左边点(0),上边点(0),Paint对象

获取Paint对象,new出来

调用Paint对象的setAntiAlias(),设置抗锯齿,参数:布尔值

滑动按钮

滑动按钮目前的位置,0,0,状态是 关

canvas.drawBitmap(bitmapBtn, 0, 0, paint);

滑动按钮的位置在,背景图的宽度-滑动按钮的宽度,0,状态是 开

canvas.drawBitmap(bitmapBtn, 背景图的宽度-滑动按钮的宽度, 0, paint);

定义成员变量currentState存储当前状态,值:布尔值

调用setOnClickListener()方法,设置点击事件,参数:this

当前类实现obClickListener接口,实现onClick()方法

切换当前状态currentState=!currentState

判断当前状态

如果为真,滑动按钮的左边是背景图的宽度-滑动按钮的宽度

如果为假,滑动按钮的左边是0

调用invalidate()方法,刷新当前视图

MyToggleBtn.java

代码语言:javascript
复制
package com.tsh.myswitchbtn;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;

public class MyToggleBtn extends View implements OnClickListener {
    //背景图片
    private Bitmap bitmapBackground;
    //按钮图片
    private Bitmap bitmapBtn;
    private Paint paint;
    /**
     * 布局文件中使用
     * @param context
     * @param attrs
     */
    public MyToggleBtn(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }
    /**
     * 初始化view
     */
    private void initView() {
        bitmapBackground=BitmapFactory.decodeResource(getResources(), R.drawable.switch_background);
        bitmapBtn=BitmapFactory.decodeResource(getResources(), R.drawable.slide_button);
        paint=new Paint();
        paint.setAntiAlias(true);
        //点击事件
        setOnClickListener(this);
    }
    /**
     * 计算大小
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(bitmapBackground.getWidth(), bitmapBackground.getHeight());
    }
    //当前状态
    private boolean currentState=false;
    //滑动按钮的当前left
    private float slideBtnLeft=0;
    /**
     * 绘制view
     */
    @Override
    protected void onDraw(Canvas canvas) {
        //绘制背景
        canvas.drawBitmap(bitmapBackground, 0, 0, paint);
        //绘制滑动按钮
        canvas.drawBitmap(bitmapBtn, slideBtnLeft, 0, paint);
    }
    /**
     * 点击事件
     */
    @Override
    public void onClick(View v) {
        currentState=!currentState;
        if(currentState==true){
            slideBtnLeft=bitmapBackground.getWidth()-bitmapBtn.getWidth();
        }else{
            slideBtnLeft=0;
        }
        invalidate();
    }

}

布局:

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <com.tsh.myswitchbtn.MyToggleBtn
        android:layout_centerInParent="true"
        android:id="@+id/my_toggle_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</RelativeLayout>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-05-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档