本文实例为大家分享了Android实现顶部粘性下拉刷新效果的具体代码,供大家参考,具体内容如下

activity_view_mv代码

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:id="@+id/rl_view"

android:layout_width="match_parent"

android:layout_height="wrap_content"

>

android:id="@+id/cs_view"

android:layout_width="match_parent"

android:layout_height="wrap_content"

app:pColor="@color/cff3e19"

app:pContentDrawable="@drawable/shape_circle"

app:pContentDrawableMargin="2dp"

app:pDragHeight="100dp"

app:pTangentAngle="110"

app:pRadius="15dp"

app:pTargetGravityHeight="4dp"

app:pTargetWidth="200dp"

/>

android:layout_width="match_parent"

android:visibility="gone"

android:layout_height="wrap_content"

/>

ViewMvActivity代码

import android.os.Bundle;

import android.support.annotation.Nullable;

import android.view.MotionEvent;

import android.view.View;

import android.widget.RelativeLayout;

import butterknife.BindView;

import trunk.doi.base.R;

import trunk.doi.base.base.BaseActivity;

public class ViewMvActivity extends BaseActivity {

@BindView(R.id.cs_view)

TouchPullView csView;

@BindView(R.id.rl_view)

RelativeLayout rl_view;

private float mTouchStartY;

private static final float TOUCH_MOVE_MAX_Y=600;

@Override

protected int initLayoutId() {

return R.layout.activity_view_mv;

}

@Override

protected void initView(@Nullable Bundle savedInstanceState) {

rl_view.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

int action=event.getActionMasked();

switch (action){

case MotionEvent.ACTION_DOWN:

mTouchStartY=event.getY();

return true;

case MotionEvent.ACTION_MOVE:

float y=event.getY();

if(y>=mTouchStartY){

float moveSize= y-mTouchStartY;

float progress=moveSize>=TOUCH_MOVE_MAX_Y?1:moveSize/TOUCH_MOVE_MAX_Y;

csView.setProgress(progress);

}

return true;

case MotionEvent.ACTION_UP:

csView.release();

return true;

default:

break;

}

return false;

}

});

}

@Override

protected void setListener() {

}

@Override

protected void initData() {

}

}

TouchPullView代码

import android.animation.ValueAnimator;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.Path;

import android.graphics.drawable.Drawable;

import android.os.Build;

import android.support.annotation.Nullable;

import android.support.annotation.RequiresApi;

import android.support.v4.view.animation.PathInterpolatorCompat;

import android.util.AttributeSet;

import android.util.Log;

import android.view.View;

import android.view.animation.DecelerateInterpolator;

import android.view.animation.Interpolator;

import trunk.doi.base.R;

/**

* 作者:Mr.Lee on 2017-9-27 11:57

* 邮箱:569932357@qq.com

*/

public class TouchPullView extends View {

//圆的画笔

private Paint mCirclePaint;

//圆的半径

private int mCircleRadius=50;

private float mCirclePointX;

private float mCirclePointY;

private float mProgress;

//可拖拽高度

private int mDragHeigh=800;

//目标宽度

private int mTargetWidth=400;

//贝塞尔曲线

private Path mPath=new Path();

private Paint mPathPaint;

//重心点最终高度,决定控制点的Y坐标

private int mTargetGravityHeight=10;

//角度变换 0-135

private int mTangentAngle=100;

private Interpolator mProgressInterpolator=new DecelerateInterpolator();

private Interpolator mTanentAngleInterpolator;

private Drawable mContent=null;

private int mContentMargin=0;

public TouchPullView(Context context) {

super(context);

init(null);

}

public TouchPullView(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

init(attrs);

}

public TouchPullView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init(attrs);

}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)

public TouchPullView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {

super(context, attrs, defStyleAttr, defStyleRes);

init(attrs);

}

/**

* 初始化

*/

private void init(AttributeSet attrs){

final Context context=getContext();

TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.TouchPullView,0,0);

int color=array.getColor(R.styleable.TouchPullView_pColor,0x20000000);

mCircleRadius=(int)array.getDimension(R.styleable.TouchPullView_pRadius,mCircleRadius);

mDragHeigh=array.getDimensionPixelOffset(R.styleable.TouchPullView_pDragHeight,mDragHeigh);

mTangentAngle=array.getInteger(R.styleable.TouchPullView_pTangentAngle,mTangentAngle);

mTargetWidth=array.getDimensionPixelOffset(R.styleable.TouchPullView_pDragHeight,mTargetWidth);

mTargetGravityHeight=array.getDimensionPixelOffset(R.styleable.TouchPullView_pTargetGravityHeight,mTargetGravityHeight);

mContent=array.getDrawable(R.styleable.TouchPullView_pContentDrawable);

mContentMargin=array.getDimensionPixelOffset(R.styleable.TouchPullView_pContentDrawableMargin,0);

array.recycle();

Paint p=new Paint(Paint.ANTI_ALIAS_FLAG);

//抗锯齿

p.setAntiAlias(true);

//防抖动

p.setDither(true);

//填充方式

p.setStyle(Paint.Style.FILL);

p.setColor(color);

mCirclePaint=p;

//初始化路径部分画笔

p=new Paint(Paint.ANTI_ALIAS_FLAG);

//抗锯齿

p.setAntiAlias(true);

//防抖动

p.setDither(true);

//填充方式

p.setStyle(Paint.Style.FILL);

p.setColor(color);

mPathPaint=p;

//切角路径插值器

mTanentAngleInterpolator= PathInterpolatorCompat.create(

(mCircleRadius*2.0f)/mDragHeigh,

90.0f/mTangentAngle

);

}

@Override

protected void onSizeChanged(int w, int h, int oldw, int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

updatePathLayout();

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

// super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int widthMode=MeasureSpec.getMode(widthMeasureSpec);

int width=MeasureSpec.getSize(widthMeasureSpec);

int heighMode=MeasureSpec.getMode(heightMeasureSpec);

int heigh=MeasureSpec.getSize(heightMeasureSpec);

int iHeigh=(int)((mDragHeigh*mProgress+0.5)+ 2*mCircleRadius+getPaddingTop()+getPaddingBottom());

int iWidth=2*mCircleRadius+getPaddingLeft()+getPaddingRight();

int measureWidth,measureHeigh;

if(widthMode==MeasureSpec.EXACTLY){

measureWidth=width;

}else if(widthMode==MeasureSpec.AT_MOST){

measureWidth=Math.min(iWidth,width);

}else{

measureWidth=iWidth;

}

if(heighMode==MeasureSpec.EXACTLY){

measureHeigh=heigh;

}else if(heighMode==MeasureSpec.AT_MOST){

measureHeigh=Math.min(iHeigh,heigh);

}else{

measureHeigh=iHeigh;

}

setMeasuredDimension(measureWidth,measureHeigh);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

int count=canvas.save();

float tranX=(getWidth()-getValueByLine(getWidth(),mTargetWidth,mProgress))/2;

canvas.translate(tranX,0);

canvas.drawPath(mPath,mPathPaint);

//画圆

canvas.drawCircle(mCirclePointX,mCirclePointY,mCircleRadius,mCirclePaint);

Drawable drawable=mContent;

if(drawable!=null){

canvas.save();

//剪切矩形区域

canvas.clipRect(drawable.getBounds());

//绘制

drawable.draw(canvas);

canvas.restore();

}

canvas.restoreToCount(count);

}

/**

* 设置进度

* @param progress

*/

public void setProgress(float progress){

Log.e("TAG","progress="+progress);

mProgress=progress;

//重新请求测量

requestLayout();

}

private void updatePathLayout(){

final float progress=mProgressInterpolator.getInterpolation(mProgress);

//获取可绘制区域高度宽度

final float w=getValueByLine(getWidth(),mTargetWidth,mProgress);

final float h=getValueByLine(0,mDragHeigh,mProgress);

//X对称轴的参数,圆的圆心X

final float cPointX=w/2;

//圆的半径

final float cRadius=mCircleRadius;

//圆的圆心Y坐标

final float cPointY =h-cRadius;

//控制点结束Y坐标

final float endControlY=mTargetGravityHeight;

mCirclePointX=cPointX;

mCirclePointY= cPointY;

final Path path=mPath;

//重置

path.reset();

path.moveTo(0,0);

//左边部分的结束点和控制点

float lEndPointX,lEndPointY;

float lControlPointX,lControlPointY;

//角度转弧度

float angle=mTangentAngle*mTanentAngleInterpolator.getInterpolation(progress);

double radian=Math.toRadians(angle);

float x=(float) (Math.sin(radian)*cRadius);

float y=(float) (Math.cos(radian)*cRadius);

lEndPointX=cPointX-x;

lEndPointY= cPointY +y;

//控制点y坐标变化

lControlPointY=getValueByLine(0,endControlY,progress);

//控制点与结束定之前的高度

float tHeigh=lEndPointY-lControlPointY;

//控制点与x坐标的距离

float tWidth= (float) (tHeigh/Math.tan(radian));

lControlPointX=lEndPointX-tWidth;

//左边贝塞尔曲线

path.quadTo(lControlPointX,lControlPointY,lEndPointX,lEndPointY);

//连接到右边

path.lineTo(cPointX+(cPointX-lEndPointX),lEndPointY);

//右边贝塞尔曲线

path.quadTo(cPointX+cPointX-lControlPointX,lControlPointY,w,0);

//更新内容部分Drawable

updateContentLayout(cPointX,cPointY,cRadius);

}

/**

* 对内容部分进行测量并设置

* @param cx

* @param cy

* @param radius

*/

private void updateContentLayout(float cx,float cy,float radius){

Drawable drawable=mContent;

if(drawable!=null){

int margin=mContentMargin;

int l=(int)(cx-radius+margin);

int r=(int)(cx+radius-margin);

int t=(int)(cy-radius+margin);

int b=(int)(cy+radius-margin);

drawable.setBounds(l,t,r,b);

}

}

//释放动画

private ValueAnimator valueAnimator;

/**

* 添加释放动作

*/

public void release(){

if(valueAnimator==null){

ValueAnimator animator=ValueAnimator.ofFloat(mProgress,0f);

animator.setInterpolator(new DecelerateInterpolator());

animator.setDuration(400);

animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator animation) {

Object val=animation.getAnimatedValue();

if(val instanceof Float){

setProgress((Float) val);

}

}

});

valueAnimator=animator;

}else{

valueAnimator.cancel();

valueAnimator.setFloatValues(mProgress,0f);

}

valueAnimator.start();

}

/**

* 获取当前值

* @param start

* @param end

* @param progress

* @return

*/

private float getValueByLine(float start,float end ,float progress){

return start+(end-start)*progress;

}

}

TestViewBezer代码

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.Path;

import android.os.Build;

import android.support.annotation.Nullable;

import android.support.annotation.RequiresApi;

import android.util.AttributeSet;

import android.view.View;

/**

* 作者:Mr.Lee on 2017-9-27 18:08

* 邮箱:569932357@qq.com

*/

public class TestViewBezer extends View {

private Paint mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);

private Path mPath=new Path();

public TestViewBezer(Context context) {

super(context);

init();

}

public TestViewBezer(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

init();

}

public TestViewBezer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init();

}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)

public TestViewBezer(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {

super(context, attrs, defStyleAttr, defStyleRes);

init();

}

private void init(){

Paint paint=mPaint;

paint.setAntiAlias(true);

paint.setDither(true);

mPaint.setColor(0xff000000);

paint.setStyle(Paint.Style.STROKE);

paint.setStrokeWidth(10);

Path path=mPath;

path.moveTo(100,100);

path.lineTo(400,400);

path.quadTo(600,100,800,400);

path.moveTo(400,800);

path.cubicTo(500,600,700,1200,800,800);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

canvas.drawPath(mPath,mPaint);

canvas.drawPoint(600,100,mPaint);

canvas.drawPoint(500,600,mPaint);

canvas.drawPoint(700,1200,mPaint);

}

}

attr_pull.xml代码

shape_circle代码

android:shape="oval"

>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

android 自定义顶部,Android自定义实现顶部粘性下拉刷新效果相关推荐

  1. Android下拉刷新效果实现

    本文主要包括以下内容 自定义实现pulltorefreshView 使用google官方SwipeRefreshLayout 下拉刷新大致原理 判断当前是否在最上面而且是向下滑的,如果是的话,则加载数 ...

  2. Android开发笔记(一百二十三)下拉刷新布局SwipeRefreshLayout

    SwipeRefreshLayout 下拉刷新布局SwipeRefreshLayout是Android又一与时俱进的控件,顾名思义它随着用户手势向下滑动就会触发刷新操作.从实际的下拉效果来看,Swip ...

  3. android抖音自动刷新,Android 使用SwipeRefreshLayout控件仿抖音做的视频下拉刷新效果...

    SwipeRefreshLayout(这个控件),我先跟大家介绍一下这个控件: 一.SwipeRefreshLayout简单介绍 •先看以下官方文档,已有了很详细的描述了. 官方文档说明 •这里我再大 ...

  4. android精品源码,下拉刷新效果高德地图五子棋游戏定制日历全民TV源码

    1.Android图片涂鸦 2.在AchartEngine的基础上进行功能扩展的图表,新增非常多的... 3.上拉刷新.下拉加载 4.实现多种下拉刷新效果.上拉加载更多以及配置自定义头... 5.高德 ...

  5. Android SwipeRefreshLayout GMail的下拉刷新效果

    新浪微博:@_阡陌花开 Android 讨论群:92058507 今天更新了一下手机App,随手点开GMail客户端,发现它的下拉刷新效果很炫: 还打算研究研究如何实现呢,不过先去百度了一下,发现这个 ...

  6. android 美团下拉刷新,美团外卖下拉刷新效果实现方法

    美团外卖下拉刷新效果实现方法,我们的下拉刷新的效果是在android-Ultra-Pull-To-Refresh框架上做的扩展,这是一款非常强大的下拉刷新框架,有着默认的下拉刷新动画,当然如果我们要实 ...

  7. android多个下拉控件,Android实现支持所有View的通用的下拉刷新控件

    下拉刷新对于一个app来说是必不可少的一个功能,在早期大多数使用的是chrisbanes的PullToRefresh,或是修改自该框架的其他库.而到现在已经有了更多的选择,github上还是有很多体验 ...

  8. 基于Android的计步器(Pedometer)的讲解(六)——ListView下拉刷新页面

    计步器(Pedometer)整个项目的源代码,最近做了比较大的修改,可能以前下载的不能运行,感兴趣的朋友可以下载来看看(记得帮小弟在github打个星~) https://github.com/296 ...

  9. Android源码解析--超好看的下拉刷新动画

    本篇博客代码下载地址:https://github.com/Yalantis/Taurus 最近在github上看到了好多高端.大气.上档次的动画效果,如果给你的项目中加上这些动画,相信你的app一定 ...

最新文章

  1. 用Remoting 实现一个文件传输组件
  2. JAVA进阶教学之(IO流)
  3. Git如何进行减少提交历史数量以及修改自己的commit中的邮箱
  4. Vue「六」前端路由、vue-router
  5. session对象的使用
  6. 7种方式,教你提升 SpringBoot 项目的吞吐量
  7. php 动态修改文本内容_九大编程语言优缺点第八期:PHP
  8. Ansible详解(五)——Ansible其他模块使用详解
  9. Silverlight 离线安装包
  10. 简约前端工程师简历PPT模板
  11. sata接口 图解 定义_SATA数据和电源接口定义详解(多图).pdf
  12. 星际争霸 vs 魔兽争霸3 vs 红警完全对比
  13. 就业和工作?毕业生何去何从?
  14. 玩客云刷入armbian系统总结
  15. 天天炫斗服务器维修,【天天炫斗】弱网处理及断线重连方案
  16. -克服不良习惯读后感
  17. 吉布斯采样(Gibbs Sampling)及相关算法
  18. oracle中 unino,union all,minus,intersect的用法
  19. 10.30纪中DAY2_小麦亩产一千八(kela) 休息(rest) 军训(training)
  20. 苹果home键在哪里设置_iphonex关机键 iphonex关机键在哪里【怎么关机】

热门文章

  1. 基于html5背景图片自适应代码
  2. 解决MSChart底部横坐标显示不全的问题
  3. javascript中类的定义和使用{转载}
  4. 50%企业数据治理失败!这9大要素才是成功关键
  5. 人工智能、机器学习、深度学习的关系,终于有人讲明白了
  6. platform平台总线
  7. STM32之RCC原理
  8. 一本关于HTTP的恋爱日记
  9. 2020年2月全国程序员工资统计
  10. 如何利用开源项目,帮助企业免费搭建小程序官网