当前位置: 移动技术网 > 移动技术>移动开发>Android > Android简洁的下拉放大刷新效果示例

Android简洁的下拉放大刷新效果示例

2019年07月24日  | 移动技术网移动技术  | 我要评论

序言

国庆放假过后眼看一年又要过完了,年初指望着已经有一年的经验本以为自己不是刚出校的学生以为翅膀已经硬了,打算辞职换新工作,一面试才发现自己就是个垃圾,什么oninterceptevent,dispatchtouchevent ,aysnctask都不会。做了一年的项目也是用的xutils2.6版本 还有一堆不常用不好的不主流不时尚的框架,技术也没任何长进。还好公司真的轻松(所以也学不到任何东西)可以趁闲下来的时间多学点东西。于是写了个简单但也有需求的控件练练手。

首先先看效果图吧

这个是listview的效果还有一个scrollview的效果当然使用和实现时一样的原理这里就一listview来讲解,文末传送门可以看到全部的代码

1、具体使用

项目build.gradle

allprojects {
 repositories {
  jcenter()
  maven { url 'https://jitpack.io' }
 }
}

app model build.gradle

compile 'com.github.xypmhxy:pullzoomlayout:1.1'

布局文件中

<com.ren.pullzoom.widget.pullzoomlayout
  android:id="@+id/pull"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  app:image_height="200dp" 图片高度
  app:image_res="@mipmap/timg" 图片资源
  app:refresh_enable="true" 是否开启刷新
  app:scale_type="center_crop">//图片缩放方式

  <listview
   android:id="@+id/listview"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@android:color/white" />

 </com.ren.pullzoom.widget.pullzoomlayout>

2、实现思路

其思路很简单

1.首先在构造方法中动态添加下拉缩放的imageview和刷新的refreshprogress(控件中为实现跟随手指滑动旋转因此使用的为imageview)

2.获取到listview对象,然后监听listview的滑动事件,判断滑到顶部后继续向下滑动的时候将需要放大的imageview高度增加然后利用imageview的scale方法完成缩放。

3.最后放开手指的时候用属性动画让imageview平滑回到最初状态,并且如果开启下拉刷新则回调其方法。

3、具体实现

1.动态添加两个imageview(下拉放大的和刷新的progress),大致原理就是将这两个imageview添加到relativelayout中然后将relativelayout 添加到自身中。代码如下

/*实例化头部布局包含pullzoomimage 和 refreshprogress*/
protected void init(context context) {
  relativelayout head = new relativelayout(context);
  viewgroup.layoutparams headparams = new viewgroup.layoutparams(-1, -2);
  head.setlayoutparams(headparams);
  /*实例化pullzoomimage*/
  ·······
  pullzoomimage.setimageresource(imageres);
  originalparams = new viewgroup.layoutparams(viewgroup.layoutparams.match_parent, imageheight);
  pullzoomimage.setlayoutparams(originalparams);
  head.addview(pullzoomimage);
  /*实例化refreshprogress*/
  refreshprogress = new imageview(context);
  refreshprogress.setvisibility(gone);
  refreshprogress.setimageresource(r.drawable.refresh);
  relativelayout.layoutparams refreshparams = new relativelayout.layoutparams(dip2px(context, 35), dip2px(context, 35));
  refreshparams.addrule(relativelayout.align_parent_end, relativelayout.true);
  refreshprogress.setlayoutparams(refreshparams);
  head.addview(refreshprogress);
  /*将头部添加到此控件中*/
  addview(head, 0);
}

2.是获取listview对象,因为listview属于子控件所以不能在构造方法里直接获取,因为此时控件不一定加载完成所以需要等待子控件加载完成后获取因此在onfinishinflate方法中获取

 @override
 protected void onfinishinflate() {
  super.onfinishinflate();
  /*获取listview*/
  if (getchildat(1) instanceof listview) {
   listview = (listview) getchildat(1);
   listview.setonscrolllistener(scrolllistener);
   listview.setontouchlistener(touchlistener);
  }
 }

3.添加listview滑动监听判断是否滑动到顶部,可以开启下拉放大功能

 /*listview滑动监听*/
 protected abslistview.onscrolllistener scrolllistener = new abslistview.onscrolllistener() {
  @override
  public void onscrollstatechanged(abslistview view, int scrollstate) {
   /*判断是否滑动到顶部*/
   int firstvisibleitem = listview.getfirstvisibleposition();
   if (firstvisibleitem == 0 && scrollstate == abslistview.onscrolllistener.scroll_state_touch_scroll) {
    view firstview = getchildat(0);
    canzoom = firstview != null && firstview.gettop() == 0;
   } else
    canzoom = false;
  }

  @override
  public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {
  }
 };

4.实现ontouchlistener根据事件调用放大和缩小动画,抬手时实现刷新等操作

/*listview touchlistener监听*/
 protected ontouchlistener touchlistener = new ontouchlistener() {
  @override
  public boolean ontouch(view v, motionevent ev) {
   if (pullzoomimage == null) return false;
   switch (ev.getaction()) {
    case motionevent.action_down:
     pressy = ev.gety();//获取按下的y坐标
     break;
    case motionevent.action_move:
     if (canzoom)//如果已经滑动到顶部并继续滑动则开始放大pullzoomimage
      return zoomview(ev);
     break;
    case motionevent.action_cancel:
    case motionevent.action_up:
     if (canzoom)
      restroe();//还原pullzoomimage动画
     if (needrefresh && refreshlistener != null) {//达到刷新条件并且实现刷新监听
      refreshlistener.onrefresh();
      rotationprogress();//刷新时progress旋转动画
     } else
      refreshprogress.setvisibility(gone);
     //重置变量
     needrefresh = false;
     canzoom = false;
     break;

   }
   return false;
  }
 };

缩放imageview

 /*放大pullzoomimage*/
 protected boolean zoomview(motionevent ev) {
  float offy = ev.gety() - pressy;
  if (offy <= 0 || offy < 16)//滑动方向上滑或者滑动距离小于16则不管
   return false;
  /*如果开启下拉刷新判断滑动距离是否大于refrshslop则显示refreshprogress*/
  if (refreshenable) {
   needrefresh = offy >= refrshslop;
   if (needrefresh)
    refreshprogress.setvisibility(visible);
  }
  viewgroup.layoutparams params = pullzoomimage.getlayoutparams();
  float height = originalparams.height + offy / damp;//根据滑动距离增加pullzoomimage的高度
  params.height = (int) height;
  scaleimage(height);//放大图片
  rotationprogress(offy);//旋转refreshprogress
  if (params.height >= originalparams.height)
   pullzoomimage.setlayoutparams(params);//为pullzoomimage设置改变后的params
  return true;
 }
 /*缩放imageview*/
 protected void scaleimage(float height) {
//  if (pullzoomimage.getscaletype() == imageview.scaletype.center_crop)
//   return;
  float scale = (height - originalparams.height) / originalparams.height;//根据滑动的大小判断缩放比例
  pullzoomimage.setscalex(1 + scale);
  pullzoomimage.setscaley(1 + scale);
 }

抬手后通过属性动画还原pullzoomimage

 /*放开后还原pullzoomimage*/
 protected void restroe() {
  valueanimator animator = valueanimator.offloat(pullzoomimage.getlayoutparams().height, originalparams.height);// 动画更新的监听
  animator.addupdatelistener(new valueanimator.animatorupdatelistener() {

   @override
   public void onanimationupdate(valueanimator arg0) {
    float height = (float) arg0.getanimatedvalue();// 获取动画当前变化的值
    // 根据最新高度,更新布局高度
    viewgroup.layoutparams params = pullzoomimage.getlayoutparams();
    params.height = (int) height;
    scaleimage(height);
    pullzoomimage.setlayoutparams(params);
   }
  });
  animator.setduration(200);// 动画时间
  animator.start();// 开启动画
 }

大致原理就是这样最后传送门开启 pullzoomlayout

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网