zoukankan      html  css  js  c++  java
  • android自定义控件

    package com.example.app.sw.kg;
    
    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;
    import android.widget.Toast;
    
    public class MySwitchBtnView extends View implements OnClickListener{
        
        private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        private boolean flag = false ;
        private Context mContext ;
        
        Bitmap bitmap_open = null ;
        Bitmap bitmap_close = null;
    
        public MySwitchBtnView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mContext = context;
            init();
        }
    
        private void init() {
            setOnClickListener(this);
            bitmap_open = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.btn_open);
            bitmap_close = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.btn_close);
            
        }
    
        
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            setMeasuredDimension(bitmap_open.getWidth(), bitmap_close.getHeight());
        }
        
        public MySwitchBtnView(Context context) {
            super(context);
            mContext = context ;
            init();
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if(flag){
                //绘制开
                canvas.drawBitmap(bitmap_open, 0, 0,paint);
                Toast.makeText(mContext, "被打开了", 0).show();
            }else{
                //绘制关
                canvas.drawBitmap(bitmap_close, 0, 0,paint);
                Toast.makeText(mContext, "被关闭了", 0).show();
            }
        }
    
        @Override
        public void onClick(View v) {
            if(flag){
                flag = false ;
            }else{
                flag = true ;
            }
            invalidate(); //刷新屏幕
        }
    }
  • 相关阅读:
    小程序数据库 用正则查询字符串字段/数组字段
    一键禁用Windows多余?服务
    Switch 10.1.0 无法启动软件请在home菜单中再试一次 解决方法
    算法记录
    LeetCode——面试题 10.01. 合并排序的数组
    LeetCode——98. 验证二叉搜索树
    LeetCode——55. 跳跃游戏
    LeetCode——92. 反转链表 II
    LeetCode——206. 反转链表
    LeetCode——225. 用队列实现栈
  • 原文地址:https://www.cnblogs.com/yoyo198212/p/8325085.html
Copyright © 2011-2022 走看看