private int mHeight;
private float mRate = 0;
private int mRingHeight = 120;
private int mStartY;
private int mHeadHeight = 0;
private stateCallBack mStateCallBack;
private Paint mTextPaint;
private Timer mTimer = new Timer();
private TimerTask mTimerTask;
private boolean refreshState = false;
private long mRefreshTime = 1000;
public refreshHead(Context context) {
this(context, null);
}
public refreshHead(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public refreshHead(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.refreshHead);
BitmapDrawable mDrawable = (BitmapDrawable) typedArray.getDrawable(R.styleable.refreshHead_refreshbg);
mBitmap = m
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
Drawable.getBitmap();
//注意回收,别忘记了
typedArray.recycle();
//获取屏幕宽高
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point point = new Point();
wm.getDefaultDisplay().getSize(point);
mScreenWidth = point.x;
//初始化画笔
mImgPaint = initPaint(0, 0);
mRingPaint = initPaint(Color.parseColor("#ff0000"), 10);
mTextPaint = initPaint(Color.BLUE, 8);
}
private Paint initPaint(int color, int PaintWidth) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
if (color != 0) paint.setColor(color);
if (PaintWidth != 0) paint.setTextSize(PaintWidth);
return paint;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = MeasureSpec.getSize(widthMeasureSpec);
mHeight = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(mWidth, mHeight);
}
@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
}
//ViewGroup不会执行 onDraw ,看过源码的都知道
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
drawPicture(canvas);
//保存画布状态,因为画布 旋转 缩放 移动是不可逆的
canvas.save();
canvas.scale(mRate, mRate, mWidth / 2, mHeadHeight / 2);
canvas.rotate(180 * mRate, mWidth / 2, mHeadHeight / 2);
if (!refreshState){
if (mRate == 1.0f) {
//还原画布状态
canvas.restore();
drawText(canvas,“松手刷新”);
} else if (mRate < 1) {
drawRing(canvas);
}
}else {
//还原画布状态
canvas.restore();
drawText(canvas,“刷新中…”);
}
}
private void drawText(Canvas canvas,String text) {
mTextPaint.setTextSize(40);
//获取文字基线
Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
int dy = (int) ((fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom);
int baseline = mHeadHeight / 2 + dy;
//获取文字宽度
Rect bounds = new Rect();
mTextPaint.getTextBounds(text, 0, text.length(), bounds);
int dx = bounds.width() / 2;
int x = mScreenWidth / 2 - dx;
canvas.drawText(text, x, baseline, mTextPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mStartY = (int) event.getY();
System.out.println(“ACTION_DOWN->” + mStartY);
break;
case MotionEvent.ACTION_MOVE:
int endY = (int) event.getY();
int dy = endY - mStartY;
mHeadHeight = dy;
mRate = dy * 1.0f / 200;
if (mHeadHeight > 200) {
mHeadHeight = 200;
mRate = 1.0f;
System.out.println(“ACTION_MOVE ->” + mRate);
invalidate();
if (mStateCallBack != null) {
//调用到底的接口
mStateCallBack.onBottom();
}
return false;
}
invalidate();
break;
case MotionEvent.ACTION_UP:
if (mRate == 1.0f) {
refreshState = true;
invalidate();
//调用刷新的借口
mStateCallBack.refreshing();
mTimerTask = new TimerTask() {
@Override
public void run() {
mHeadHeight = 0;
mRate = 0;
refreshState = false;
invalidate();
//调用刷新完成接口
mStateCallBack.refreshed();
}
};
mTimer.schedule(mTimerTask,mRefreshTime);
}else {
mHeadHeight = 0;
mRate = 0;
invalidate();
}
break;
}
return true;
}
private void drawRing(Canvas canvas) {
if (mRate <= 0.2) {
mRingPaint.setColor(Color.TRANSPARENT);
} else {
mRingPaint.setColor(Color.RED);
}
Path path = new Path();
path.moveTo(mWidth / 2, mHeadHeight / 2 + 31);
path.lineTo(mWidth / 2 + 50, mHeadHeight / 2 - 31);
path.lineTo(mWidth / 2 - 50, mHeadHeight / 2 - 31);
path.close();
canvas.drawPath(path, mRingPaint);
}
private void drawPicture(Canvas canvas) {
Rect src = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
Rect dst = new Rect(0, 0, mScreenWidth, mHeadHeight);
canvas.drawBitmap(mBitmap, src, dst, mImgPaint);
}
//编写接口,规范用户
private interface stateCallBack {
void onBottom();
void refreshing();
void refreshed();
}
//监听刷新状态回调 ,把接口暴露出去,让用户实现
public void setOnStateCallBack(stateCallBack stateCallBack) {
if (this.mStateCallBack == null) {
this.mStateCallBack = stateCallBack;
}
}
//设置刷新时间
public void setRefreshTime(long duration){
this.mRefreshTime = duration;
}
}
<?xml version="1.0" encoding="utf-8"?>第二步:布局中使用
<FrameLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">
<com.wust.mycaryaokong.myUI.refreshHead
android:id="@+id/rfh_heard"