zoukankan      html  css  js  c++  java
  • [android] 切换按钮-自定义控件

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

    创建一个类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(),设置抗锯齿,参数:布尔值

    滑动按钮

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

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

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

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

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

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

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

    切换当前状态currentState=!currentState

    判断当前状态

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

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

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

    MyToggleBtn.java

    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();
        }
    
    }

    布局:

    <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>
  • 相关阅读:
    基于Antlr4编写DSL
    【整理】ANTLR应用案例 | 在路上
    【整理】ANTLR应用案例 | 在路上
    The ANTLR Parser Generator
    ANTLR4权威参考手册
    ANTLR Examples
    ANTLRWorks: The ANTLR GUI Development Environment
    http://www.cnblogs.com/vowei/archive/2012/08/24/2654287.html
    写一个编译器
    写一个编译器
  • 原文地址:https://www.cnblogs.com/taoshihan/p/5456725.html
Copyright © 2011-2022 走看看