TypeArray需要配置的String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SwitchButtonView">
<attr name="checked" format="boolean"></attr>
</declare-styleable>
</resources>
在xml中使用自定义的标签 下面是layout_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:myapp="http://schemas.android.com/apk/res/com.example.pulltorefreshtest"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.example.pulltorefreshtest.SwitchButtonView
android:id="@+id/refreshable_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
myapp:checked="true" >
</com.example.pulltorefreshtest.SwitchButtonView>
</RelativeLayout>
xmlns:myapp=”http://schemas.android.com/apk/res/com.example.pulltorefreshtest”
中的myapp是自定义的,com.example. … 包是自己的工程包名,下面的myapp:checked 属性 也要对应起来 (myapp 要上下对应 )
package com.example.pulltorefreshtest;
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.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
/** * Created by Administrator on 3/22 0022. */
public class SwitchButtonView extends View {
private Paint mPaint;
private Bitmap bitmapBackGround;
private Bitmap bitmapBall;
private Bitmap bitmapBottom;
private Bitmap bitmapBlack;
private PorterDuffXfermode pdf;
private boolean isChecked;
private int touchX;
private int ballX = 0;
private int ballMoveState = LEFT_MOST;
private int saveFlags;
private int switchWidth;
private int switchHeight;
private static final int LEFT_MOST = 0;
private static final int RIGHT_MOST = 1;
private static final int TOUCH_STATE_DOWN = 2;
private static final int TOUCH_STATE_MOVE = 3;
private static final int TOUCH_STATE_UP = 4;
private onSwitchListener mListener;
public SwitchButtonView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.SwitchButtonView);
isChecked = ta.getBoolean(R.styleable.SwitchButtonView_checked, true);
ta.recycle();
init(context);
}
public SwitchButtonView(Context context) {
this(context, null);
init(context);
}
private void init(Context context) {
mPaint = new Paint();
mPaint.setAntiAlias(true);
pdf = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
saveFlags = Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG
| Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
| Canvas.FULL_COLOR_LAYER_SAVE_FLAG
| Canvas.CLIP_TO_LAYER_SAVE_FLAG;
bitmapBackGround = BitmapFactory.decodeResource(getResources(),
R.drawable.switch_bg);
bitmapBall = BitmapFactory.decodeResource(getResources(),
R.drawable.switch_ball);
bitmapBottom = BitmapFactory.decodeResource(getResources(),
R.drawable.switch_bottom);
bitmapBlack = BitmapFactory.decodeResource(getResources(),
R.drawable.switch_black);
switchWidth = bitmapBackGround.getWidth();
switchHeight = bitmapBackGround.getHeight();
if (isChecked) {
ballMoveState = RIGHT_MOST;
ballX = bitmapBackGround.getWidth() - bitmapBall.getWidth();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(switchWidth, switchHeight);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.saveLayer(0, 0, switchWidth, switchHeight, null, saveFlags);
canvas.drawBitmap(bitmapBlack, 0, 0, mPaint);
mPaint.setXfermode(pdf);
if (isChecked) {
canvas.drawBitmap(bitmapBottom,
0 - (switchWidth - bitmapBall.getWidth() - ballX), 0,
mPaint);
} else {
canvas.drawBitmap(bitmapBottom,
-(bitmapBottom.getWidth() / 2 - bitmapBall.getWidth() / 2)
+ ballX, 0, mPaint);
}
mPaint.setXfermode(null);
canvas.restore();
ballMoveState(canvas);
}
/** * 滑动状态绘制 * * @param canvas */
private void ballMoveState(Canvas canvas) {
switch (ballMoveState) {
case TOUCH_STATE_DOWN:
case TOUCH_STATE_MOVE:
if (touchX > 0 && touchX < switchWidth - bitmapBall.getWidth()) {
canvas.drawBitmap(bitmapBall, touchX, 0, mPaint);
} else if (touchX <= 0) {
canvas.drawBitmap(bitmapBall, 0, 0, mPaint);
} else if (touchX >= switchWidth - bitmapBall.getWidth()) {
canvas.drawBitmap(bitmapBall,
switchWidth - bitmapBall.getWidth(), 0, mPaint);
}
break;
case TOUCH_STATE_UP:
case LEFT_MOST:
if (touchX > 0 && touchX < switchWidth / 2) {
canvas.drawBitmap(bitmapBall, 0, 0, mPaint);
} else if (touchX >= switchWidth / 2 && touchX <= switchWidth) {
canvas.drawBitmap(bitmapBall,
switchWidth - bitmapBall.getWidth(), 0, mPaint);
} else if (touchX <= 0) {
canvas.drawBitmap(bitmapBall, 0, 0, mPaint);
} else if (touchX >= switchWidth - bitmapBall.getWidth()) {
canvas.drawBitmap(bitmapBall,
switchWidth - bitmapBall.getWidth(), 0, mPaint);
}
break;
case RIGHT_MOST:
canvas.drawBitmap(bitmapBall, switchWidth - bitmapBall.getWidth(),
0, mPaint);
break;
default:
break;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i("check", event.getX() + "----------------");
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchStateChange((int) event.getX(), TOUCH_STATE_DOWN);
break;
case MotionEvent.ACTION_MOVE:
touchStateChange((int) event.getX(), TOUCH_STATE_MOVE);
break;
case MotionEvent.ACTION_UP:
touchStateChange((int) event.getX(), TOUCH_STATE_UP);
break;
default:
break;
}
return true;
}
/** * 触摸状态改变 touchX 控制在 (0,bg。getwidth) * * @param mTouchX * @param touchState */
private void touchStateChange(int mTouchX, int touchState) {
ballX = touchX = mTouchX;
if (touchX <= 0) {
ballX = 0;
}
if (touchX >= switchWidth - bitmapBall.getWidth()) {
ballX = switchWidth - bitmapBall.getWidth();
}
ballMoveState = touchState;
if (ballMoveState == TOUCH_STATE_UP) {
ballX = 0;
if (touchX >= switchWidth / 2f) {
isChecked = true;
ballX = switchWidth - bitmapBall.getWidth();
} else {
isChecked = false;
}
if (mListener != null) {
mListener.onSwitchChanged(isChecked);
}
}
invalidate();
}
public void setOnSwitchListener(onSwitchListener listener) {
this.mListener = listener;
}
public interface onSwitchListener {
void onSwitchChanged(boolean isCheck);
}
}