这两天学习自定义控件,有点小 进步,根据事件分发机制谢了一个简单滑动按钮,希望大家多多提提意见话不多说上代码了PS 图片用别人的自定义控件的图片-.---
package com.zhl.huadong;
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.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class HuaView extends View {
private float downX, nowX;// 按下时的坐标,和最终的X坐标
private boolean isClose = false;// 是否 关闭 默认为关闭状态
private Bitmap mBitmapOpen;// 滑动按钮底部打开时的背景
private Bitmap mBitmapClose;// 滑动按钮底部关闭时的背景
private Bitmap mBitmapHua;// 滑动按钮的滑块
private boolean isSliping = false;// 是否为滑动状态
private Paint mPaint;// 画笔
private OnChangeLister listener;// 回调接口
public HuaView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public HuaView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
// TODO Auto-generated constructor stub
}
public HuaView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
initView();
}
public void initView() {
mBitmapOpen = BitmapFactory.decodeResource(getResources(),
R.drawable.open);
mBitmapClose = BitmapFactory.decodeResource(getResources(),
R.drawable.close);
mBitmapHua = BitmapFactory.decodeResource(getResources(),
R.drawable.huakuai);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
Matrix matrix = new Matrix();
mPaint = new Paint();
// 设置背景
if (nowX < (mBitmapOpen.getWidth() / 2)) {
canvas.drawBitmap(mBitmapClose, matrix, mPaint);
} else {
canvas.drawBitmap(mBitmapOpen, matrix, mPaint);
}
float x = 0;
// 是否在滑动状态
if (isSliping) {
// 判断X 滑块位置 不能 让滑块 出去
if (nowX >= mBitmapOpen.getWidth()) {
x = mBitmapOpen.getWidth() - mBitmapHua.getWidth() / 2;
} else {
x = nowX - mBitmapHua.getWidth() / 2;
}
} else {
// 如果不在滑动状态
if (isClose) {
x = mBitmapOpen.getWidth() - mBitmapHua.getWidth();
} else {
x = -8;// 可以设置为0但是 当设置为0的时候那个滑块到达不了最左边
}
}
if (x < 0) {
x = -8;
} else if (x > mBitmapOpen.getWidth() - mBitmapHua.getWidth()) {
x = mBitmapOpen.getWidth() - mBitmapHua.getWidth();
}
canvas.drawBitmap(mBitmapHua, x, 0, mPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (event.getX() > mBitmapClose.getWidth()
|| event.getY() > mBitmapClose.getHeight()) {
return false;
} else {
isSliping = true;
downX = event.getX();
nowX = downX;
}
break;
case MotionEvent.ACTION_MOVE:
nowX = event.getX();
break;
case MotionEvent.ACTION_UP:
isSliping = false;
if (event.getX() > (mBitmapOpen.getWidth() / 2)) {
isClose = true;
nowX = mBitmapOpen.getWidth() - (mBitmapHua.getWidth());
} else {
isClose = false;
nowX = 0;
}
break;
}
invalidate();
return true;
}
/**
* 设置滑动开关的初始状态,供外部调用
*
* @param checked
*/
public void setChecked(boolean checked) {
if (checked) {
nowX = mBitmapOpen.getWidth();
} else {
nowX = -8;
}
isClose = checked;
invalidate();
}
public interface OnChangeLister {
public void OnChanged(HuaView wiperSwitch, boolean checkState);
}
/**
* 为huaView设置一个监听,供外部调用的方法
*
* @param listener
*/
public void setOnChangedListener(OnChangeLister listener) {
this.listener = listener;
}
}
布局里面使用
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zhl.huadong.MainActivity" >
<com.zhl.huadong.HuaView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
元旦将至 祝大家元旦快乐