Android仿微信朋友圈实现滚动条下拉反d效果

Android仿微信朋友圈实现滚动条下拉反d效果,第1张

概述微信朋友圈上面的图片封面,QQ空间说说上面的图片封面都有下拉反d的效果,这些都是使用滚动条实现的。下拉,当松开时候,反d至原来的位置。下拉时候能看到背景图片。那么这里简单介绍一下这种效果的实现。

微信朋友圈上面的图片封面,QQ空间说说上面的图片封面都有下拉反d的效果,这些都是使用滚动条实现的。下拉,当松开时候,反d至原来的位置。下拉时候能看到背景图片。那么这里简单介绍一下这种效果的实现。
1、效果图


这部手机显示的分辨率有限,很老的手机调试。
2、具有反d效果BounceScrollVIEw

package com.org.scroll;  import androID.content.Context; import androID.graphics.Rect; import androID.util.AttributeSet; import androID.vIEw.MotionEvent; import androID.vIEw.VIEw; import androID.vIEw.animation.TranslateAnimation; import androID.Widget.ScrollVIEw;  /**  * ScrollVIEw反d效果的实现  */ public class BounceScrollVIEw extends ScrollVIEw {   private VIEw inner;// 孩子VIEw    private float y;// 点击时y坐标   // 矩形(这里只是个形式,只是用于判断是否需要动画.)   private Rect normal = new Rect();    private boolean isCount = false;// 是否开始计算    public BounceScrollVIEw(Context context,AttributeSet attrs) {     super(context,attrs);   }    /***    * 根据 XML 生成视图工作完成.该函数在生成视图的最后调用,在所有子视图添加完之后. 即使子类覆盖了 onFinishInflate    * 方法,也应该调用父类的方法,使该方法得以执行.    */   @OverrIDe   protected voID onFinishInflate() {     if (getChildCount() > 0) {       inner = getChildAt(0);     }   }    /***    * 监听touch    */   @OverrIDe   public boolean ontouchEvent(MotionEvent ev) {     if (inner != null) {       commOntouchEvent(ev);     }      return super.ontouchEvent(ev);   }    /***    * 触摸事件    *    * @param ev    */   public voID commOntouchEvent(MotionEvent ev) {     int action = ev.getAction();     switch (action) {     case MotionEvent.ACTION_DOWN:       break;     case MotionEvent.ACTION_UP:       // 手指松开.       if (isNeedAnimation()) {         animation();         isCount = false;       }       break;     /***      * 排除出第一次移动计算,因为第一次无法得知y坐标, 在MotionEvent.ACTION_DOWN中获取不到,      * 因为此时是MyScrollVIEw的touch事件传递到到了ListVIEw的孩子item上面.所以从第二次计算开始.      * 然而我们也要进行初始化,就是第一次移动的时候让滑动距离归0. 之后记录准确了就正常执行.      */     case MotionEvent.ACTION_MOVE:       final float preY = y;// 按下时的y坐标       float NowY = ev.getY();// 时时y坐标       int deltaY = (int) (preY - NowY);// 滑动距离       if (!isCount) {         deltaY = 0; // 在这里要归0.       }        y = NowY;       // 当滚动到最上或者最下时就不会再滚动,这时移动布局       if (isNeedMove()) {         // 初始化头部矩形         if (normal.isEmpty()) {           // 保存正常的布局位置           normal.set(inner.getleft(),inner.gettop(),inner.getRight(),inner.getBottom());         } //       Log.e("jj","矩形:" + inner.getleft() + "," + inner.gettop() //           + "," + inner.getRight() + "," + inner.getBottom());         // 移动布局         inner.layout(inner.getleft(),inner.gettop() - deltaY / 2,inner.getBottom() - deltaY / 2);       }       isCount = true;       break;      default:       break;     }   }    /***    * 回缩动画    */   public voID animation() {     // 开启移动动画     TranslateAnimation ta = new TranslateAnimation(0,normal.top);     ta.setDuration(200);     inner.startAnimation(ta);     // 设置回到正常的布局位置     inner.layout(normal.left,normal.top,normal.right,normal.bottom);  //   Log.e("jj","回归:" + normal.left + "," + normal.top + "," + normal.right //       + "," + normal.bottom);      normal.setEmpty();    }    // 是否需要开启动画   public boolean isNeedAnimation() {     return !normal.isEmpty();   }    /***    * 是否需要移动布局 inner.getMeasuredHeight():获取的是控件的总高度    *    * getHeight():获取的是屏幕的高度    *    * @return    */   public boolean isNeedMove() {     int offset = inner.getMeasuredHeight() - getHeight();     int scrollY = getScrollY(); //   Log.e("jj","scrolly=" + scrollY);     // 0是顶部,后面那个是底部     if (scrollY == 0 || scrollY == offset) {       return true;     }     return false;   }  } 

3、MainActivity

package com.org.activity;  import androID.os.Bundle; import androID.app.Activity; import androID.vIEw.Menu; import androID.vIEw.Window;  public class MainActivity extends Activity {    @OverrIDe   protected voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     requestwindowFeature(Window.FEATURE_NO_Title);     setContentVIEw(R.layout.activity_main);   }    @OverrIDe   public boolean onCreateOptionsMenu(Menu menu) {     // Inflate the menu; this adds items to the action bar if it is present.     getMenuInflater().inflate(R.menu.activity_main,menu);     return true;   }  } 

这个没做什么,主要看布局,以及BounceScrollVIEw类。

4、activity_main布局

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"   xmlns:tools="http://schemas.androID.com/tools"   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"   androID:orIEntation="vertical" >    <include layout="@layout/common_Title_bg" />    <com.org.scroll.BounceScrollVIEw     androID:layout_wIDth="match_parent"     androID:layout_height="wrap_content"     androID:background="@drawable/coversation_bg"     androID:focusable="true"     androID:focusableIntouchMode="true" >      <linearLayout       androID:layout_wIDth="match_parent"       androID:layout_height="match_parent"       androID:orIEntation="vertical"       androID:paddingtop="10.0dip" >        <relativeLayout         androID:ID="@+ID/accountSetting"         androID:layout_wIDth="fill_parent"         androID:layout_height="63.0dip"         androID:background="#80ffffff"         androID:focusable="true" >          <FrameLayout           androID:ID="@+ID/frameLayout1"           androID:layout_wIDth="54.0dip"           androID:layout_height="54.0dip"           androID:layout_centerVertical="true"           androID:layout_marginleft="10.0dip" >            <ImageVIEw             androID:ID="@+ID/face"             androID:layout_wIDth="50.0dip"             androID:layout_height="50.0dip"             androID:layout_gravity="center"             androID:contentDescription="@null"             androID:src="@drawable/h0" />            <ImageVIEw             androID:ID="@+ID/statusIcon"             androID:layout_wIDth="18.0dip"             androID:layout_height="18.0dip"             androID:layout_gravity="bottom|right|center"             androID:contentDescription="@null" />         </FrameLayout>          <ImageVIEw           androID:ID="@+ID/imageVIEw1"           androID:layout_wIDth="wrap_content"           androID:layout_height="wrap_content"           androID:layout_alignParentRight="true"           androID:layout_centerVertical="true"           androID:layout_marginRight="10.0dip"           androID:contentDescription="@null"           androID:duplicateParentState="true" />          <TextVIEw           androID:ID="@+ID/status"           androID:layout_wIDth="wrap_content"           androID:layout_height="wrap_content"           androID:layout_alignBottom="@+ID/nick"           androID:layout_marginRight="10.0dip"           androID:layout_toleftOf="@ID/imageVIEw1"           androID:duplicateParentState="true"           androID:text="在线" />          <TextVIEw           androID:ID="@+ID/nick"           androID:layout_wIDth="wrap_content"           androID:layout_height="wrap_content"           androID:layout_centerVertical="true"           androID:layout_marginleft="10.0dip"           androID:layout_marginRight="69.0dip"           androID:layout_toRightOf="@ID/frameLayout1"           androID:duplicateParentState="true"           androID:ellipsize="end"           androID:singleline="true" />       </relativeLayout>        <linearLayout         androID:layout_wIDth="match_parent"         androID:layout_height="600dp"         androID:layout_margintop="16.0dip"         androID:layout_weight="2.13"         androID:background="#ffffffff"         androID:orIEntation="vertical" >          <TextVIEw           androID:ID="@+ID/my_profile"           androID:layout_wIDth="fill_parent"           androID:layout_height="44.0dip"           androID:background="#800000ff"           androID:clickable="true"           androID:gravity="center_vertical"           androID:paddingleft="10.0dip"           androID:paddingRight="10.0dip"           androID:text="标题一" />          <linearLayout           androID:layout_wIDth="fill_parent"           androID:layout_height="wrap_content"           androID:layout_margintop="16.0dip"           androID:orIEntation="vertical" >            <relativeLayout             androID:ID="@+ID/set_Feedback"             androID:layout_wIDth="fill_parent"             androID:layout_height="44.0dip"             androID:background="#8000ffff"             androID:clickable="true"             androID:focusable="true" >              <TextVIEw               androID:layout_wIDth="wrap_content"               androID:layout_height="wrap_content"               androID:layout_centerVertical="true"               androID:layout_marginleft="12.0dip"               androID:duplicateParentState="true"               androID:gravity="center_vertical"               androID:text="反馈" />           </relativeLayout>         </linearLayout>       </linearLayout>     </linearLayout>   </com.org.scroll.BounceScrollVIEw>  </linearLayout> 

希望本文对大家学习AndroID软件编程有所帮助。

总结

以上是内存溢出为你收集整理的Android仿微信朋友圈实现滚动条下拉反d效果全部内容,希望文章能够帮你解决Android仿微信朋友圈实现滚动条下拉反d效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1141882.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存