更新源码
This commit is contained in:
182
CustomView/Advance/Code/CheckView.java
Normal file
182
CustomView/Advance/Code/CheckView.java
Normal file
@@ -0,0 +1,182 @@
|
||||
package com.sloop.canvas;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
/**
|
||||
* <ul type="disc">
|
||||
* <li>Author: Sloop</li>
|
||||
* <li>Version: v1.0.0</li>
|
||||
* <li>Copyright: Copyright (c) 2015</li>
|
||||
* <li>Date: 2016/2/5</li>
|
||||
* <li><a href="http://www.sloop.icoc.cc" target="_blank">作者网站</a> </li>
|
||||
* <li><a href="http://weibo.com/5459430586" target="_blank">作者微博</a> </li>
|
||||
* <li><a href="https://github.com/GcsSloop" target="_blank">作者GitHub</a> </li>
|
||||
* </ul>
|
||||
*/
|
||||
public class CheckView extends View {
|
||||
|
||||
private static final int ANIM_NULL = 0; //动画状态-没有
|
||||
private static final int ANIM_CHECK = 1; //动画状态-开启
|
||||
private static final int ANIM_UNCHECK = 2; //动画状态-结束
|
||||
|
||||
private Context mContext; // 上下文
|
||||
private int mWidth, mHeight; // 宽高
|
||||
private Handler mHandler; // handler
|
||||
|
||||
private Paint mPaint;
|
||||
private Bitmap okBitmap;
|
||||
|
||||
private int animCurrentPage = -1; // 当前页码
|
||||
private int animMaxPage = 13; // 总页数
|
||||
private int animDuration = 500; // 动画时长
|
||||
private int animState = ANIM_NULL; // 动画状态
|
||||
|
||||
private boolean isCheck = false; // 是否只选中状态
|
||||
|
||||
public CheckView(Context context) {
|
||||
super(context, null);
|
||||
|
||||
}
|
||||
|
||||
public CheckView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @param context
|
||||
*/
|
||||
private void init(Context context) {
|
||||
mContext = context;
|
||||
|
||||
mPaint = new Paint();
|
||||
mPaint.setColor(0xffFF5317);
|
||||
mPaint.setStyle(Paint.Style.FILL);
|
||||
mPaint.setAntiAlias(true);
|
||||
|
||||
okBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.checkres);
|
||||
|
||||
mHandler = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
super.handleMessage(msg);
|
||||
if (animCurrentPage < animMaxPage && animCurrentPage >= 0) {
|
||||
invalidate();
|
||||
if (animState == ANIM_NULL)
|
||||
return;
|
||||
if (animState == ANIM_CHECK) {
|
||||
|
||||
animCurrentPage++;
|
||||
} else if (animState == ANIM_UNCHECK) {
|
||||
animCurrentPage--;
|
||||
}
|
||||
|
||||
this.sendEmptyMessageDelayed(0, animDuration / animMaxPage);
|
||||
Log.e("AAA", "Count=" + animCurrentPage);
|
||||
} else {
|
||||
if (isCheck) {
|
||||
animCurrentPage = animMaxPage - 1;
|
||||
} else {
|
||||
animCurrentPage = -1;
|
||||
}
|
||||
invalidate();
|
||||
animState = ANIM_NULL;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* View大小确定
|
||||
* @param w
|
||||
* @param h
|
||||
* @param oldw
|
||||
* @param oldh
|
||||
*/
|
||||
@Override
|
||||
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
||||
super.onSizeChanged(w, h, oldw, oldh);
|
||||
mWidth = w;
|
||||
mHeight = h;
|
||||
}
|
||||
|
||||
/**
|
||||
* 绘制内容
|
||||
* @param canvas
|
||||
*/
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
|
||||
// 移动坐标系到画布中央
|
||||
canvas.translate(mWidth / 2, mHeight / 2);
|
||||
|
||||
// 绘制背景圆形
|
||||
canvas.drawCircle(0, 0, 240, mPaint);
|
||||
|
||||
// 得出图像边长
|
||||
int sideLength = okBitmap.getHeight();
|
||||
|
||||
// 得到图像选区 和 实际绘制位置
|
||||
Rect src = new Rect(sideLength * animCurrentPage, 0, sideLength * (animCurrentPage + 1), sideLength);
|
||||
Rect dst = new Rect(-200, -200, 200, 200);
|
||||
|
||||
// 绘制
|
||||
canvas.drawBitmap(okBitmap, src, dst, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 选择
|
||||
*/
|
||||
public void check() {
|
||||
if (animState != ANIM_NULL || isCheck)
|
||||
return;
|
||||
animState = ANIM_CHECK;
|
||||
animCurrentPage = 0;
|
||||
mHandler.sendEmptyMessageDelayed(0, animDuration / animMaxPage);
|
||||
isCheck = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消选择
|
||||
*/
|
||||
public void unCheck() {
|
||||
if (animState != ANIM_NULL || (!isCheck))
|
||||
return;
|
||||
animState = ANIM_UNCHECK;
|
||||
animCurrentPage = animMaxPage - 1;
|
||||
mHandler.sendEmptyMessageDelayed(0, animDuration / animMaxPage);
|
||||
isCheck = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置动画时长
|
||||
* @param animDuration
|
||||
*/
|
||||
public void setAnimDuration(int animDuration) {
|
||||
if (animDuration <= 0)
|
||||
return;
|
||||
this.animDuration = animDuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置背景圆形颜色
|
||||
* @param color
|
||||
*/
|
||||
public void setBackgroundColor(int color){
|
||||
mPaint.setColor(color);
|
||||
}
|
||||
}
|
||||
185
CustomView/Advance/Code/CheckView.md
Normal file
185
CustomView/Advance/Code/CheckView.md
Normal file
@@ -0,0 +1,185 @@
|
||||
``` java
|
||||
package com.sloop.canvas;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
/**
|
||||
* <ul type="disc">
|
||||
* <li>Author: Sloop</li>
|
||||
* <li>Version: v1.0.0</li>
|
||||
* <li>Copyright: Copyright (c) 2015</li>
|
||||
* <li>Date: 2016/2/5</li>
|
||||
* <li><a href="http://www.gcssloop.com/" target="_blank">作者网站</a> </li>
|
||||
* <li><a href="http://weibo.com/5459430586" target="_blank">作者微博</a> </li>
|
||||
* <li><a href="https://github.com/GcsSloop" target="_blank">作者GitHub</a> </li>
|
||||
* </ul>
|
||||
*/
|
||||
public class CheckView extends View {
|
||||
|
||||
private static final int ANIM_NULL = 0; //动画状态-没有
|
||||
private static final int ANIM_CHECK = 1; //动画状态-开启
|
||||
private static final int ANIM_UNCHECK = 2; //动画状态-结束
|
||||
|
||||
private Context mContext; // 上下文
|
||||
private int mWidth, mHeight; // 宽高
|
||||
private Handler mHandler; // handler
|
||||
|
||||
private Paint mPaint;
|
||||
private Bitmap okBitmap;
|
||||
|
||||
private int animCurrentPage = -1; // 当前页码
|
||||
private int animMaxPage = 13; // 总页数
|
||||
private int animDuration = 500; // 动画时长
|
||||
private int animState = ANIM_NULL; // 动画状态
|
||||
|
||||
private boolean isCheck = false; // 是否只选中状态
|
||||
|
||||
public CheckView(Context context) {
|
||||
super(context, null);
|
||||
|
||||
}
|
||||
|
||||
public CheckView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @param context
|
||||
*/
|
||||
private void init(Context context) {
|
||||
mContext = context;
|
||||
|
||||
mPaint = new Paint();
|
||||
mPaint.setColor(0xffFF5317);
|
||||
mPaint.setStyle(Paint.Style.FILL);
|
||||
mPaint.setAntiAlias(true);
|
||||
|
||||
okBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.checkres);
|
||||
|
||||
mHandler = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
super.handleMessage(msg);
|
||||
if (animCurrentPage < animMaxPage && animCurrentPage >= 0) {
|
||||
invalidate();
|
||||
if (animState == ANIM_NULL)
|
||||
return;
|
||||
if (animState == ANIM_CHECK) {
|
||||
|
||||
animCurrentPage++;
|
||||
} else if (animState == ANIM_UNCHECK) {
|
||||
animCurrentPage--;
|
||||
}
|
||||
|
||||
this.sendEmptyMessageDelayed(0, animDuration / animMaxPage);
|
||||
Log.e("AAA", "Count=" + animCurrentPage);
|
||||
} else {
|
||||
if (isCheck) {
|
||||
animCurrentPage = animMaxPage - 1;
|
||||
} else {
|
||||
animCurrentPage = -1;
|
||||
}
|
||||
invalidate();
|
||||
animState = ANIM_NULL;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* View大小确定
|
||||
* @param w
|
||||
* @param h
|
||||
* @param oldw
|
||||
* @param oldh
|
||||
*/
|
||||
@Override
|
||||
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
||||
super.onSizeChanged(w, h, oldw, oldh);
|
||||
mWidth = w;
|
||||
mHeight = h;
|
||||
}
|
||||
|
||||
/**
|
||||
* 绘制内容
|
||||
* @param canvas
|
||||
*/
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
|
||||
// 移动坐标系到画布中央
|
||||
canvas.translate(mWidth / 2, mHeight / 2);
|
||||
|
||||
// 绘制背景圆形
|
||||
canvas.drawCircle(0, 0, 240, mPaint);
|
||||
|
||||
// 得出图像边长
|
||||
int sideLength = okBitmap.getHeight();
|
||||
|
||||
// 得到图像选区 和 实际绘制位置
|
||||
Rect src = new Rect(sideLength * animCurrentPage, 0, sideLength * (animCurrentPage + 1), sideLength);
|
||||
Rect dst = new Rect(-200, -200, 200, 200);
|
||||
|
||||
// 绘制
|
||||
canvas.drawBitmap(okBitmap, src, dst, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 选择
|
||||
*/
|
||||
public void check() {
|
||||
if (animState != ANIM_NULL || isCheck)
|
||||
return;
|
||||
animState = ANIM_CHECK;
|
||||
animCurrentPage = 0;
|
||||
mHandler.sendEmptyMessageDelayed(0, animDuration / animMaxPage);
|
||||
isCheck = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消选择
|
||||
*/
|
||||
public void unCheck() {
|
||||
if (animState != ANIM_NULL || (!isCheck))
|
||||
return;
|
||||
animState = ANIM_UNCHECK;
|
||||
animCurrentPage = animMaxPage - 1;
|
||||
mHandler.sendEmptyMessageDelayed(0, animDuration / animMaxPage);
|
||||
isCheck = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置动画时长
|
||||
* @param animDuration
|
||||
*/
|
||||
public void setAnimDuration(int animDuration) {
|
||||
if (animDuration <= 0)
|
||||
return;
|
||||
this.animDuration = animDuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置背景圆形颜色
|
||||
* @param color
|
||||
*/
|
||||
public void setBackgroundColor(int color){
|
||||
mPaint.setColor(color);
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
Reference in New Issue
Block a user