效果图:

定义一个类,取名为MySwitch.java,此类去继承View,为何是继承View而不是去继承ViewGroup呢,是因为自定义开关没有子控件,之需要操作自身绘制即可

package custom.view.upgrade.my_switch;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View; import custom.view.R; public class MySwitch extends View implements View.OnClickListener { private static String TAG = MySwitch.class.getSimpleName(); private Paint mPaint; /**
* 让布局中来指定实例化,得到属性集合AttributeSet
* @param context
* @param attrs
*/
public MySwitch(Context context, @Nullable AttributeSet attrs) {
super(context, attrs); initView(context, attrs);
initListener();
} // 定义按钮背景图片
private Bitmap bmSwitchBackground; // 定义按钮拖动的图片
private Bitmap bmSwitchDrag; // 定义开关的状态 true || false , 默认是关闭状态
private boolean switchStatus; // 定义开关的临时记录状态
private boolean tempSwitchStatus; // 定义按钮拖动距离左边的距离
private int dragLife = -1; /**
* 初始化工作
*/
private void initView(Context context, AttributeSet attrs) {
mPaint = new Paint();
mPaint.setAntiAlias(true); // 抗锯齿 // 获取属性
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MySwitch);
bmSwitchBackground = ((BitmapDrawable) typedArray.getDrawable(R.styleable.MySwitch_switch_background)).getBitmap();
bmSwitchDrag = ((BitmapDrawable) typedArray.getDrawable(R.styleable.MySwitch_switch_drag)).getBitmap();
switchStatus = typedArray.getBoolean(R.styleable.MySwitch_switch_status, false);
} public void setBmSwitchBackground(int switchBackground) {
this.bmSwitchBackground = BitmapFactory.decodeResource(getResources() ,switchBackground);
} public void setBmSwitchDrag(int switchDrag) {
this.bmSwitchDrag = BitmapFactory.decodeResource(getResources(), switchDrag);
} public void setSwitChStatus(boolean switchStatus) {
this.switchStatus = switchStatus;
dragLife = -1;
} /**
* 初始化事件
*/
private void initListener() {
setOnClickListener(this);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 宽度是:按钮背景的宽度
// 高度是:按钮背景的高度
// 测量自身View
setMeasuredDimension(bmSwitchBackground.getWidth(), bmSwitchBackground.getHeight());
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas); // 绘制按钮背景
canvas.drawBitmap(bmSwitchBackground, 0, 0, mPaint); if (dragLife != -1) {
canvas.drawBitmap(bmSwitchDrag, dragLife, 0 , mPaint);
} else if (dragLife == -1) {
if (switchStatus) {
// 打开状态
// 滑动点向右就是开启状态
int openDragLife = getLifeDragMaxValue();
canvas.drawBitmap(bmSwitchDrag, openDragLife, 0, mPaint);
moveEndX = openDragLife;
} else {
// 关闭状态
canvas.drawBitmap(bmSwitchDrag, 0, 0, mPaint);
moveEndX = 0;
} // 当开关的状态发生变化后,回调方法告诉用户,开关改变了
if (null != onSwitchChangeListener && switchStatusChange) {
if (tempSwitchStatus != switchStatus) {
onSwitchChangeListener.onSwitchChange(switchStatus);
}
}
}
} private float downX;
private int moveEndX; private float clickDown;
private float clickMove; // 开关状态是否发送了改变
private boolean switchStatusChange; @Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event); // 必须要调用此方法,onClick点击事件方法才会生效
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getX();
isClick = true;
clickDown = event.getX();
switchStatusChange = false;
tempSwitchStatus = switchStatus;
break;
case MotionEvent.ACTION_MOVE:
// Log.d(TAG, ">>>>>不加等于:" + (int) (event.getX() - downX));
moveEndX += (int) (event.getX() - downX);
Log.d(TAG,">>>>>>加等于:" + moveEndX); if (moveEndX > getLifeDragMaxValue()) {
moveEndX = getLifeDragMaxValue();
} else if (moveEndX < 0){
moveEndX = 0;
} dragLife = moveEndX;
invalidate(); downX = event.getX(); clickMove = downX;
if (Math.abs(clickMove - clickDown) > 5) {
isClick = false;
} break;
case MotionEvent.ACTION_UP:
if (dragLife > (getLifeDragMaxValue() / 2)) {
dragLife = -1;
switchStatus = true;
switchStatusChange = true;
} else if (dragLife >= 0){
dragLife = -1;
switchStatus = false;
switchStatusChange = true;
} else {
switchStatusChange = false;
}
invalidate();
// upX = (int) event.getX();
break;
default:
break;
}
return true;
} private int getLifeDragMaxValue() {
return bmSwitchBackground.getWidth() - bmSwitchDrag.getWidth();
} /*@Override
protected void onFinishInflate() {
super.onFinishInflate(); if (switchStatus) {
moveEndX = getLifeDragMaxValue();
Log.d(TAG, ">>>>>>>>>>>>>>>>>onFinishInflate()............ getLifeDragMaxValue():" + getLifeDragMaxValue());
}
}*/ /**
* 定义点击事件状态
*/
private boolean isClick = true; @Override
public void onClick(View v) {
Log.d(TAG, "onClick() isClick:" + isClick);
if (isClick) {
if (switchStatus) {
switchStatus = false;
switchStatusChange = true;
} else {
switchStatus = true;
switchStatusChange = true;
}
// switchStatus = (switchStatus==true?false:true);
dragLife = -1;
invalidate();
}
} private OnSwitchChangeListener onSwitchChangeListener; /**
* 用户设置的 状态监听
* @param onSwitchChangeListener
*/
public void setOnSwitchChangeListener(OnSwitchChangeListener onSwitchChangeListener) {
this.onSwitchChangeListener = onSwitchChangeListener;
}
}

布局文件中去引用写好的自定义开关类

并设置自定义属性:

     myswitch:switch_background="@mipmap/switch_background"
myswitch:switch_drag="@mipmap/switch_drag"
myswitch:switch_status="true"
<!-- 自定义开关升级版 -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myswitch="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".upgrade.MainActivity"> <!-- 使用wrap_content,是因为不知道按钮的背景有多大,更加按钮图片的改变而变化 -->
<custom.view.upgrade.my_switch.MySwitch
android:id="@+id/custom_myswitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
myswitch:switch_background="@mipmap/switch_background"
myswitch:switch_drag="@mipmap/switch_drag"
myswitch:switch_status="true"
/> </RelativeLayout>

自定义规则arrts.xml文件声明:

<?xml version="1.0" encoding="utf-8"?>
<resources> <declare-styleable name="MySwitch"> <attr name="switch_status" format="boolean" /> <attr name="switch_background" format="reference" /> <attr name="switch_drag" format="reference" /> </declare-styleable> </resources>

模拟用户来使用:

MySwitch mySwitch = findViewById(R.id.custom_myswitch);

        // 设置开关的背景图片
mySwitch.setBmSwitchBackground(R.mipmap.switch_background); // 设置开关拖动的图片
mySwitch.setBmSwitchDrag(R.mipmap.switch_drag); // 设置开关的状态,打开、关闭
mySwitch.setSwitChStatus(false); mySwitch.setOnSwitchChangeListener(new OnSwitchChangeListener() {
@Override
public void onSwitchChange(boolean switchChangeStatus) {
String result;
if (switchChangeStatus) {
result = "打开";
} else {
result = "关闭";
}
Toast.makeText(MainActivity.this, "开关已" + result, Toast.LENGTH_SHORT).show();
}
});

Android-自定义开关(升级版)的更多相关文章

  1. Android 自定义 View 绘制

    在 Android 自定义View 里面,介绍了自定义的View的基本概念.同时在 Android 控件架构及View.ViewGroup的测量 里面介绍了 Android 的坐标系 View.Vie ...

  2. android 自定义动画

    android自定义动画注意是继承Animation,重写里面的initialize和applyTransformation,在initialize方法做一些初始化的工作,在applyTransfor ...

  3. Android自定义View 画弧形,文字,并增加动画效果

    一个简单的Android自定义View的demo,画弧形,文字,开启一个多线程更新ui界面,在子线程更新ui是不允许的,但是View提供了方法,让我们来了解下吧. 1.封装一个抽象的View类   B ...

  4. Android自定义View4——统计图View

    1.介绍 周末在逛慕课网的时候,看到了一张学习计划报告图,详细记录了自己一周的学习情况,天天都是0节课啊!正好在学习Android自定义View,于是就想着自己去写了一个,这里先给出一张慕课网的图,和 ...

  5. (转)[原] Android 自定义View 密码框 例子

    遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...

  6. Android 自定义View合集

    自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...

  7. Android 自定义View (五)——实践

    前言: 前面已经介绍了<Android 自定义 view(四)-- onMeasure 方法理解>,那么这次我们就来小实践下吧 任务: 公司现有两个任务需要我完成 (1)监测液化天然气液压 ...

  8. Android 自定义 view(四)—— onMeasure 方法理解

    前言: 前面我们已经学过<Android 自定义 view(三)-- onDraw 方法理解>,那么接下我们还需要继续去理解自定义view里面的onMeasure 方法 推荐文章: htt ...

  9. Android 自定义 view(三)—— onDraw 方法理解

    前言: 上一篇已经介绍了用自己定义的属性怎么简单定义一个view<Android 自定义view(二) -- attr 使用>,那么接下来我们继续深究自定义view,下一步将要去简单理解自 ...

随机推荐

  1. Windows 10 修改系统环境变量后,CMD生效,Powershell未生效

    Windows 10 修改系统环境变量后 1.CMD重新打开,新环境变量已经生效: 2.Shift+右键Powershell重新打开,新环境变量没有生效: 3.貌似服务(Apache24)中使用的环境 ...

  2. Java之POI的excel导入导出

    一.Apache POI是一种流行的API,它允许程序员使用Java程序创建,修改和显示MS Office文件.这由Apache软件基金会开发使用Java分布式设计或修改Microsoft Offic ...

  3. Web Api HelpPage

    为了方面APP开发人员,服务端的接口都应当提供详尽的API说明.但每次有修改,既要维护代码,又要维护文档,一旦开发进度紧张,很容易导致代码与文档不一致. Web API有一个Help Page插件,可 ...

  4. Oracle数据库物理结构

    Oracle数据库物理结构 oracle的数据,实际上是以文件的形式来保存的,文件中出了保存用户的数据之外,还需要保存管理数据和日志数据等等.作为一个DBA,必须需要知道自己的数据分别保存在什么位置上 ...

  5. MPI n 体问题

    ▶ <并行程序设计导论>第六章中讨论了 n 体问题,分别使用了 MPI,Pthreads,OpenMP 来进行实现,这里是 MPI 的代码,分为基本算法和简化算法(引力计算量为基本算法的一 ...

  6. Vue项目中将table组件导出Excel表格以及打印页面内容

    体验更优排版请移步原文:http://blog.kwin.wang/programming/vue-table-export-excel-and-print.html 页面中显示的table表格,经常 ...

  7. 跟我学算法-图像识别之图像分类(上)(基础神经网络, 卷积神经网络(CNN), AlexNet,NIN, VGG)

    1.基础神经网络: 输入向量x,权重向量w, 偏置标量b, 激活函数sigmoid(增加非线性度) 优化手段: 梯度下降优化, BP向后传播(链式规则) 梯度下降优化: 1. 使用得目标函数是交叉熵  ...

  8. 5-青蛙的约会(ex_gcd)

    青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:122411   Accepted: 25980 Descripti ...

  9. Makefiles in Linux

    http://www.codeproject.com/Articles/31488/Makefiles-in-Linux-An-Overview

  10. __next__和__iter__实现迭代器协议

    ---恢复内容开始--- #_*_coding:utf-8_*_ __author__ = 'Linhaifeng' class Foo: def __init__(self,x): self.x=x ...