自定义开关(不带有滑动,只具有点击切换开关功能)

it2022-05-05  104

类似这样的开关虽然只是安卓众多组件中的一个不起眼的开关按钮,但是不少app中都有他的存在,下面我们就自定义这样的开关按钮

直接上代码了

1 package com.wangy.wiperswitch.Custom; 2 3 import android.content.Context; 4 import android.graphics.Bitmap; 5 import android.graphics.BitmapFactory; 6 import android.graphics.Canvas; 7 import android.graphics.Matrix; 8 import android.graphics.Paint; 9 import android.util.AttributeSet; 10 import android.view.MotionEvent; 11 import android.view.View; 12 13 import com.wangy.wiperswitch.R; 14 15 /** 16 * Created by xhb on 2016/9/13. 17 */ 18 public class WiperSwitch extends View implements View.OnTouchListener{ 19 20 private OnChangedListener listener; 21 private Bitmap btn_on; 22 private Bitmap btn_off; 23 private Bitmap sliperbtn; 24 /** 25 * 按下时的x和当前的x 26 */ 27 private float downx,nowx; 28 /** 29 * 记录用户是否在滑动 30 */ 31 private boolean onSlip = false; 32 /** 33 * 当前的状态 34 */ 35 private boolean nowstate=false; 36 37 38 39 public WiperSwitch(Context context) { 40 super(context); 41 init(); 42 } 43 44 public WiperSwitch(Context context, AttributeSet attrs) { 45 super(context, attrs); 46 init(); 47 } 48 private void init(){ 49 btn_on = BitmapFactory.decodeResource(getResources(), R.drawable.on_btn); 50 btn_off = BitmapFactory.decodeResource(getResources(), R.drawable.off_btn); 51 sliperbtn = BitmapFactory.decodeResource(getResources(), R.drawable.white_btn); 52 setOnTouchListener(this); 53 } 54 55 @Override 56 protected void onDraw(Canvas canvas) { 57 super.onDraw(canvas); 58 Matrix matrix=new Matrix(); 59 Paint paint=new Paint(); 60 float x=0; 61 62 //判断开关的背景 63 if (nowx<=btn_on.getWidth()/2){ 64 canvas.drawBitmap(btn_off,matrix,paint);//显示关闭时的背景 65 }else { 66 canvas.drawBitmap(btn_on,matrix,paint);//显示打开时的背景 67 } 68 69 //判断绘制滑块的位置 70 if (nowstate){ 71 x=btn_on.getWidth()-sliperbtn.getWidth()+8;//8是你根据滑块的显示位置可能不正,做出的稍微的一点调整 72 }else { 73 x=8; 74 } 75 //对滑块滑动进行异常处理,不能让滑块出界 76 if (x<0){ 77 x=0; 78 }else if (x>btn_on.getWidth()-sliperbtn.getWidth()){ 79 x=btn_on.getWidth()-sliperbtn.getWidth(); 80 } 81 82 canvas.drawBitmap(sliperbtn,x,8,paint); 83 } 84 85 @Override 86 public boolean onTouch(View v, MotionEvent event) { 87 switch (event.getAction()){ 88 case MotionEvent.ACTION_DOWN:{ 89 break; 90 } 91 case MotionEvent.ACTION_MOVE:{ 92 break; 93 } 94 case MotionEvent.ACTION_UP:{ 95 nowstate=!nowstate; 96 if (listener!=null){ 97 listener.onchanged(this,nowstate); 98 } 99 break; 100 } 101 102 } 103 //刷新界面 104 invalidate(); 105 return true; 106 } 107 /** * 设置滑动开关的初始状态,供外部调用 * @param checked */ 108 public void setchecked(boolean checked){ 109 if (checked){ 110 nowx=btn_off.getWidth(); 111 }else { 112 nowx=0; 113 } 114 nowstate=checked; 115 } 116 /** * WiperSwitch设置一个监听,供外部调用的方法 * @param listener */ 117 public void setonchangedlistener(OnChangedListener listener){ 118 this.listener=listener; 119 } 120 //回调的接口 121 public interface OnChangedListener{ 122 public void onchanged(WiperSwitch wiperSwitch,boolean checked); 123 } 124 125 126 127 }

后面就是应用了,这是xml文件:

1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 > 6 7 8 <com.wangy.wiperswitch.Custom.WiperSwitch 9 android:id="@+id/wiperswitch" 10 android:layout_width="110dp" 11 android:layout_height="70dp" 12 android:layout_centerInParent="true" 13 /> 14 15 </RelativeLayout>

然后是主代码了:

1 package com.wangy.wiperswitch; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.Window; 6 import android.widget.Toast; 7 8 import com.wangy.wiperswitch.Custom.WiperSwitch; 9 10 public class MainActivity extends AppCompatActivity { 11 12 private WiperSwitch wiperSwitch; 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 19 init(); 20 } 21 private void init(){ 22 wiperSwitch = (WiperSwitch)findViewById(R.id.wiperswitch); 23 wiperSwitch.setonchangedlistener(new WiperSwitch.OnChangedListener() { 24 @Override 25 public void onchanged(WiperSwitch wiperSwitch, boolean checked) { 26 if (checked){ 27 wiperSwitch.setchecked(false); 28 Toast.makeText(MainActivity.this,"打开",Toast.LENGTH_SHORT).show(); 29 open(); 30 }else { 31 wiperSwitch.setchecked(true); 32 Toast.makeText(MainActivity.this,"关闭",Toast.LENGTH_SHORT).show(); 33 close(); 34 } 35 } 36 }); 37 } 38 39 private void open(){ 40 wiperSwitch.setchecked(true); 41 } 42 43 private void close(){ 44 wiperSwitch.setchecked(false); 45 } 46 }

把图片也奉上吧!

是不是很简单,我自定义控件的能力很差,没有怎么写过,应该从见到的开始练练了,以后碰到这样的功能就可以直接复制代码了,提高了工作效率了,哈哈哈,希望对大家有帮助!

转载于:https://www.cnblogs.com/wangying222/p/5870769.html

相关资源:Android自定义view仿IOS开关效果

最新回复(0)