Android编程实现滑动按钮功能详解
作者:罗瑜 时间:2022-04-14 04:58:39
本文实例讲述了Android编程实现滑动按钮功能。分享给大家供大家参考,具体如下:
首先效果图:
然后是分别建立三个文件,第一个是main.class,第二个是SlipButton.class,第三个是 onchangeListener.class
main.class
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class Main extends Activity implements OnChangedListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SlipButton myBtn =(SlipButton) findViewById(R.id.slipBtn);//获得指定控件
myBtn.SetOnChangedListener(this);//为控件设置 *
}
@Override
public void OnChanged(boolean CheckState) {//当按钮状态被改变时
// TODO Auto-generated method stub
if(CheckState)
Toast.makeText(this,"打开了..." , Toast.LENGTH_SHORT).show();
else
Toast.makeText(this,"关闭了..." , Toast.LENGTH_SHORT).show();
}
}
SlipButton.class
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class SlipButton extends View implements OnTouchListener{
private boolean NowChoose = false;//记录当前按钮是否打开,true为打开,flase为关闭
private boolean OnSlip = false;//记录用户是否在滑动的变量
private float DownX,NowX;//按下时的x,当前的x,
private Rect Btn_On,Btn_Off;//打开和关闭状态下,游标的Rect
private boolean isChgLsnOn = false;
private OnChangedListener ChgLsn;
private Bitmap bg_on,bg_off,slip_btn;
public SlipButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
init();
}
public SlipButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init();
}
private void init(){//初始化
//载入图片资源
bg_on = BitmapFactory.decodeResource(getResources(), R.drawable.slip_bg_on);
bg_off = BitmapFactory.decodeResource(getResources(), R.drawable.slip_bg_off);
slip_btn = BitmapFactory.decodeResource(getResources(), R.drawable.slip_btn);
//获得需要的Rect数据
Btn_On = new Rect(0,0,slip_btn.getWidth(),slip_btn.getHeight());
Btn_Off = new Rect(
bg_off.getWidth()-slip_btn.getWidth(),
0,
bg_off.getWidth(),
slip_btn.getHeight());
setOnTouchListener(this);//设置 * ,也可以直接复写OnTouchEvent
}
@Override
protected void onDraw(Canvas canvas) {//绘图函数
// TODO Auto-generated method stub
super.onDraw(canvas);
Matrix matrix = new Matrix();
Paint paint = new Paint();
float x;
{
if(NowX<(bg_on.getWidth()/2))//滑动到前半段与后半段的背景不同,在此做判断
canvas.drawBitmap(bg_off,matrix, paint);//画出关闭时的背景
else
canvas.drawBitmap(bg_on,matrix, paint);//画出打开时的背景
if(OnSlip)//是否是在滑动状态,
{
if(NowX >= bg_on.getWidth())//是否划出指定范围,不能让游标跑到外头,必须做这个判断
x = bg_on.getWidth()-slip_btn.getWidth()/2;//减去游标1/2的长度...
else
x = NowX - slip_btn.getWidth()/2;
}else{//非滑动状态
if(NowChoose)//根据现在的开关状态设置画游标的位置
x = Btn_Off.left;
else
x = Btn_On.left;
}
if(x<0)//对游标位置进行异常判断...
x = 0;
else if(x>bg_on.getWidth()-slip_btn.getWidth())
x = bg_on.getWidth()-slip_btn.getWidth();
canvas.drawBitmap(slip_btn,x, 0, paint);//画出游标.
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction())//根据动作来执行代码
{
case MotionEvent.ACTION_MOVE://滑动
NowX = event.getX();
break;
case MotionEvent.ACTION_DOWN://按下
if(event.getX()>bg_on.getWidth()||event.getY()>bg_on.getHeight())
return false;
OnSlip = true;
DownX = event.getX();
NowX = DownX;
break;
case MotionEvent.ACTION_UP://松开
OnSlip = false;
boolean LastChoose = NowChoose;
if(event.getX()>=(bg_on.getWidth()/2))
NowChoose = true;
else
NowChoose = false;
if(isChgLsnOn&&(LastChoose!=NowChoose))//如果设置了 * ,就调用其方法..
ChgLsn.OnChanged(NowChoose);
break;
default:
}
invalidate();//重画控件
return true;
}
public void SetOnChangedListener(OnChangedListener l){//设置 * ,当状态修改的时候
isChgLsnOn = true;
ChgLsn = l;
}
}
onchangeListener.class
package CMD100.demo.slipButton;
public interface OnChangedListener {
abstract void OnChanged(boolean CheckState);
}
main.xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<LinearLayout
android:orientation = "horizontal"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:background = "#ff0000"
>
<TextView
android:text = "测试:"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
/>
<CMD100.demo.slipButton.SlipButton
android:id = "@+id/slipBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft = "10sp"
/>
</LinearLayout>
</LinearLayout>
注意:在xml里头要放置的位置
<[包名].SlipButton
android:id = "@+id/slipBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
然后可以像其他控件一样使用了...
SlipButton myBtn =(SlipButton) findViewById(R.id.slipBtn);
myBtn.SetOnChangedListener(...);
代码到这里就全部完成了。
希望本文所述对大家Android程序设计有所帮助。
标签:Android,滑动按钮
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#串口通信实现方法
2023-04-15 06:31:11
Unity实现注册登录模块
2023-11-24 04:55:22
![](https://img.aspxhome.com/file/2023/8/92948_0s.gif)
C语言实现双向链表
2023-05-30 08:02:24
![](https://img.aspxhome.com/file/2023/2/90192_0s.png)
IntelliJ Plugin 开发之添加第三方jar的示例代码
2021-11-27 13:35:52
![](https://img.aspxhome.com/file/2023/5/77105_0s.png)
idea输入sout无法自动补全System.out.println()的问题
2023-11-28 21:34:03
![](https://img.aspxhome.com/file/2023/3/60603_0s.jpg)
java实现zip,gzip,7z,zlib格式的压缩打包
2023-08-13 08:56:59
Spring Boot Admin 进行项目监控管理的方法
2021-09-01 23:39:19
![](https://img.aspxhome.com/file/2023/1/67001_0s.png)
超简单C#获取带汉字的字符串真实长度(单个英文长度为1,单个中文长度为2)
2021-11-13 20:37:55
![](https://img.aspxhome.com/file/2023/0/104290_0s.png)
关于C#委托三种调用的分享使用
2022-10-29 03:24:35
![](https://img.aspxhome.com/file/2023/5/80075_0s.png)
Java获取时间打印到控制台代码实例
2022-07-20 22:43:48
![](https://img.aspxhome.com/file/2023/7/96087_0s.png)
Java的运算符和程序逻辑控制你了解吗
2023-01-19 10:01:59
Java实现简单QQ聊天工具
2023-11-25 05:54:52
![](https://img.aspxhome.com/file/2023/0/60130_0s.jpg)
Java SimpleDateFormat与System类使用示例详解
2021-10-25 23:15:55
![](https://img.aspxhome.com/file/2023/4/78014_0s.png)
Kotlin与Java的区别详解
2023-06-22 16:25:58
![](https://img.aspxhome.com/file/2023/6/75696_0s.jpg)
Java BoxLayout(盒子布局)布局管理器解析
2022-07-19 05:26:09
![](https://img.aspxhome.com/file/2023/3/63783_0s.png)
Java经典面试题最全汇总208道(三)
2023-11-15 23:30:42
![](https://img.aspxhome.com/file/2023/6/59066_0s.jpg)
java读取ftp中TXT文件的案例
2022-07-07 20:10:11
![](https://img.aspxhome.com/file/2023/1/101771_0s.jpg)
Spring Boot2深入分析解决java.lang.ArrayStoreException异常
2021-10-20 12:10:24
![](https://img.aspxhome.com/file/2023/6/67436_0s.jpg)
解决Android Studio安装后运行出错dose not...和Internal error...
2023-04-24 11:54:42
![](https://img.aspxhome.com/file/2023/9/64919_0s.png)
SpringBoot自定义注解之实现AOP切面日志详解
2022-12-18 17:58:06
![](https://img.aspxhome.com/file/2023/1/99721_0s.jpg)