Android自定义下拉刷新

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.refreshHead);

BitmapDrawable mDrawable = (BitmapDrawable) typedArray.getDrawable(R.styleable.refreshHead_refreshbg);

mBitmap = mDrawable.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”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

app:refreshbg=“@drawable/refreshhead”>

</com.wust.mycaryaokong.myUI.refreshHead>

里面涉及一个自定义属性

第三步:难点讲解

自定义View的基础知识我已经在前面讲解过了,在这里我就不过多赘述了,我主要谈谈我自己在编写的过程中遇到的问题:

1、ViewGroup系统默认是不执行onDraw()的,如何解决呢?

最后

给大家送上我成功跳槽复习中所整理的资料,由于文章篇幅有限,所以只是把题目列出来了

image

image

image
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
_70)

第三步:难点讲解

自定义View的基础知识我已经在前面讲解过了,在这里我就不过多赘述了,我主要谈谈我自己在编写的过程中遇到的问题:

1、ViewGroup系统默认是不执行onDraw()的,如何解决呢?

最后

给大家送上我成功跳槽复习中所整理的资料,由于文章篇幅有限,所以只是把题目列出来了

[外链图片转存中…(img-U4cVV9fv-1714759450495)]

[外链图片转存中…(img-sVjFrzvG-1714759450496)]

[外链图片转存中…(img-s7qw8aUI-1714759450497)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值