0
点赞
收藏
分享

微信扫一扫

Android自定义下拉刷新,flutter文档发布组件

Ad大成 2022-01-20 阅读 48

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);

//保存画布状态,因为画布 旋转 缩放 移动是不可逆的

ca
nvas.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()的,如何解决呢?

解:请大家参考我写的这篇文章,为什么ViewGroup的onDraw()方法不执行,里面写得很详细

2、手指划太快了,顶部还没滑到自己设定的高度就划不动了?(这个里面也涉及到事件处理,如果你不熟悉,可以先去补补这方面的知识)

解:原因:当布局还在onDraw你  70%预定的头部高度时  你的手指已经滑够了预想的像素点,提前 return false,导致后面的事件不能被消费,这段话对应的代码如下:

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

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

解:请大家参考我写的这篇文章,为什么ViewGroup的onDraw()方法不执行,里面写得很详细

2、手指划太快了,顶部还没滑到自己设定的高度就划不动了?(这个里面也涉及到事件处理,如果你不熟悉,可以先去补补这方面的知识)

解:原因:当布局还在onDraw你  70%预定的头部高度时  你的手指已经滑够了预想的像素点,提前 return false,导致后面的事件不能被消费,这段话对应的代码如下:

举报

相关推荐

0 条评论