Android 滑动开关控件
程序员文章站
2022-04-20 08:28:44
...
初始时是关闭的状态:
[img]http://dl.iteye.com/upload/attachment/0073/3337/e6c0e502-d9ac-3613-96a9-ad30a22a341a.jpg[/img]
向右滑动打开:
[img]http://dl.iteye.com/upload/attachment/0073/3342/7bc74a8e-aba4-30e0-b56a-e1a368229a38.jpg[/img]
向左滑动关闭:
[img]http://dl.iteye.com/upload/attachment/0073/3344/f3476dc2-d043-3cef-8e2c-d724f267219f.jpg[/img]
滑动控件所用图片:
[img]http://dl.iteye.com/upload/attachment/0073/3412/14afbba0-d7ca-3b1e-b237-570512fb213f.png[/img] sild_bg_on
[img]http://dl.iteye.com/upload/attachment/0073/3414/b5fa0ff9-233e-34e7-ba12-fb29dde82ff3.png[/img] sild_bg_off
[img]http://dl.iteye.com/upload/attachment/0073/3416/f6849f28-5f6f-36eb-bfa0-ba5fba62a7a8.png[/img] sild_bg_btn
Activity:
自定义滑动控件:
main.xml
最后AndroidManifest.xml,一定要注意加上蓝牙的权限
[img]http://dl.iteye.com/upload/attachment/0073/3337/e6c0e502-d9ac-3613-96a9-ad30a22a341a.jpg[/img]
向右滑动打开:
[img]http://dl.iteye.com/upload/attachment/0073/3342/7bc74a8e-aba4-30e0-b56a-e1a368229a38.jpg[/img]
向左滑动关闭:
[img]http://dl.iteye.com/upload/attachment/0073/3344/f3476dc2-d043-3cef-8e2c-d724f267219f.jpg[/img]
滑动控件所用图片:
[img]http://dl.iteye.com/upload/attachment/0073/3412/14afbba0-d7ca-3b1e-b237-570512fb213f.png[/img] sild_bg_on
[img]http://dl.iteye.com/upload/attachment/0073/3414/b5fa0ff9-233e-34e7-ba12-fb29dde82ff3.png[/img] sild_bg_off
[img]http://dl.iteye.com/upload/attachment/0073/3416/f6849f28-5f6f-36eb-bfa0-ba5fba62a7a8.png[/img] sild_bg_btn
Activity:
package com.amaker.Activity;
import com.amaker.Activity.SlidButton.OnChangedListener;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.widget.Toast;
public class Android_SlidButtonActivity extends Activity implements OnChangedListener {
/** Called when the activity is first created. */
private BluetoothAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SlidButton myBtn = (SlidButton)findViewById(R.id.slipBtn);
myBtn.SetOnChangedListener(this);
adapter = BluetoothAdapter.getDefaultAdapter();
}
@Override
public void OnChanged(boolean checkState) {
// TODO Auto-generated method stub
if (checkState){
//打开蓝牙
adapter.enable();
Toast.makeText(this, "蓝牙打开了。。。", Toast.LENGTH_SHORT).show();
}else{
//关闭蓝牙
adapter.disable();
Toast.makeText(this, "蓝牙关闭了。。。", Toast.LENGTH_SHORT).show();
}
}
}
自定义滑动控件:
package com.amaker.Activity;
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 SlidButton extends View implements OnTouchListener {
private boolean nowChoose = false;// 记录当前按钮是否打开,true为打开,false为关闭
private boolean onSlip = false;// 记录用户是否在滑动
private float downX, nowX; // 按下时的x,当前的x
private Rect btn_on, btn_off;// 打开和关闭状态下,游标的Rect
private boolean isChgLsnOn = false;//是否设置监听
private OnChangedListener changedLis;
private Bitmap bg_on, bg_off, slip_btn;
public SlidButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SlidButton(Context context) {
super(context);
init();
}
private void init() {
// 载入图片资源
bg_on = BitmapFactory.decodeResource(getResources(),
R.drawable.sild_bg_on);
bg_off = BitmapFactory.decodeResource(getResources(),
R.drawable.sild_bg_off);
slip_btn = BitmapFactory.decodeResource(getResources(),
R.drawable.sild_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);
}
@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) {
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))//如果设置了监听器,就调用其方法.
changedLis.OnChanged(nowChoose);
break;
default:
break;
}
invalidate();
return true;
}
public void SetOnChangedListener(OnChangedListener l){//设置监听器,当状态修改的时候
isChgLsnOn = true;
changedLis = l;
}
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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFFFFF">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="30dp">
<TextView
android:text="蓝牙"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20sp"/>
<com.amaker.Activity.SlidButton
android:id="@+id/slipBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"/>
</LinearLayout>
</LinearLayout>
最后AndroidManifest.xml,一定要注意加上蓝牙的权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amaker.Activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Android_SlidButtonActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
上一篇: 随笔 mysql 定时任务
下一篇: UI设计之Dialog
推荐阅读