Note:代码总共140行许,实现按压高亮,可自由拖动效果
1.准备素材:四张图片
switch_bar_light.png
switch_bg_left.png
switch_bar.png
switch_bg_right.png
2.效果图
3.代码
package com.carsyso.radiosurface.view;
import com.carsyso.radiosurface.R;
import com.carsyso.utils.LogUtil;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
@SuppressLint("ClickableViewAccessibility")
public class SwitchButton extends View {
private Bitmap bitmapLeft, bitmapRight, bitmapBar, bitmapBarunPress, bitmapBarPress;
private int x = 0;
private boolean selected;
private OnSelectedChangeListener listener;
public SwitchButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
init();
}
public SwitchButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init();
}
public SwitchButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (selected) {
x = getMeasuredWidth() - bitmapBar.getWidth();
} else {
x = 0;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = (int) event.getX();
bitmapBar = bitmapBarPress;
if (x > getWidth() - bitmapBar.getWidth()) {
x = getWidth() - bitmapBar.getWidth();
} else if (x < 0) {
x = 0;
}
invalidate();
return true;
case MotionEvent.ACTION_MOVE:
bitmapBar = bitmapBarPress;
x = (int) event.getX();
if (x > getWidth() - bitmapBar.getWidth()) {
x = getWidth() - bitmapBar.getWidth();
} else if (x < 0) {
x = 0;
}
invalidate();
break;
case MotionEvent.ACTION_UP:
bitmapBar = bitmapBarunPress;
if (x > getWidth() / 2) {
x = getWidth() - bitmapBar.getWidth();
if (!selected) {
selected = true;
if (listener != null) {
listener.onSelectedChange(selected);
}
}
} else {
x = 0;
if (selected) {
selected = false;
if (listener != null) {
listener.onSelectedChange(selected);
}
}
}
invalidate();
break;
default:
break;
}
return super.onTouchEvent(event);
}
private void init() {
bitmapLeft = BitmapFactory.decodeResource(getResources(), R.drawable.switch_bg_left);
bitmapRight = BitmapFactory.decodeResource(getResources(), R.drawable.switch_bg_right);
bitmapBarunPress = BitmapFactory.decodeResource(getResources(), R.drawable.switch_bar);
bitmapBarPress = BitmapFactory.decodeResource(getResources(), R.drawable.switch_bar_light);
bitmapBar = bitmapBarunPress;
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(bitmapRight, null, new Rect(0, 0, getWidth(), getHeight()), null);
canvas.drawBitmap(bitmapLeft, null, new Rect(x, 0, getWidth(), getHeight()), null);
canvas.drawBitmap(bitmapBar, null, new Rect(x - 2, 0, x + bitmapBarunPress.getWidth() + 2, getHeight()), null);
}
public interface OnSelectedChangeListener {
void onSelectedChange(boolean selected);
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public OnSelectedChangeListener getListener() {
return listener;
}
public void setListener(OnSelectedChangeListener listener) {
this.listener = listener;
}
}
4.使用
<com.carsyso.radiosurface.view.SwitchButton
android:id="@+id/switch_button_dab_dab"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_centerInParent="true" />
资源下载:https://download.youkuaiyun.com/download/sclgxt/10573885