当前位置: 移动技术网 > 移动技术>移动开发>Android > Android开发实现带有反弹效果仿IOS反弹scrollview教程详解

Android开发实现带有反弹效果仿IOS反弹scrollview教程详解

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

首先给大家看一下我们今天这个最终实现的效果图:

这里写图片描述 

这个是ios中的反弹效果。当然我们安卓中如果想要实现这种效果,感觉不会那么生硬,滚动到底部或者顶部的时候。当然

使用scrollview是无法实现的。所以我们需要新建一个view继承scrollview

package davidbouncescrollview.qq986945193.com.davidbouncescrollview;
import android.annotation.suppresslint;
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;
/**
* @author :程序员小冰
* @新浪微博 :http://weibo.com/mcxiaobing
* @github:https://github.com/qq986945193
* @csdn博客: http://blog.csdn.net/qq_21376985
* @交流qq :986945193
* 类名:带有反弹效果的scrollview
*/
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
* 方法,也应该调用父类的方法,使该方法得以执行.
*/
@suppresslint("missingsupercall")
@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. 之后记录准确了就正常执行.
* https://github.com/qq986945193
*/
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.getright(), inner.getbottom() - deltay / 2);
}
iscount = true;
break;
default:
break;
}
}
/***
* 回缩动画
*/
public void animation() {
// 开启移动动画
translateanimation ta = new translateanimation(0, 0, inner.gettop(),
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():获取的是控件的总高度
* <p/>
* getheight():获取的是屏幕的高度
* <p/>
* https://github.com/qq986945193
*
* @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;
}
}

然后他的用法就是和scrollview用法一样了。比如直接在布局中引用:

<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingbottom="@dimen/activity_vertical_margin"
android:paddingleft="@dimen/activity_horizontal_margin"
android:paddingright="@dimen/activity_horizontal_margin"
android:paddingtop="@dimen/activity_vertical_margin"
tools:context="davidbouncescrollview.qq986945193.com.davidbouncescrollview.mainactivity">
<davidbouncescrollview.qq986945193.com.davidbouncescrollview.bouncescrollview
android:layout_width="match_parent"
android:layout_height="match_parent">
<linearlayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<view
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="大家好,我是程序员小冰" />
<view
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="大家好,我是程序员小冰" />
<view
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="大家好,我是程序员小冰" />
<view
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="大家好,我是程序员小冰" />
<view
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="大家好,我是程序员小冰" />
<view
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="大家好,我是程序员小冰" />
<view
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="大家好,我是程序员小冰" />
<view
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="大家好,我是程序员小冰" />
</linearlayout>
</davidbouncescrollview.qq986945193.com.davidbouncescrollview.bouncescrollview>
</linearlayout>

最后直接运行即可看到上面的效果。

(androidstudio版)github下载地址:

https://github.com/qq986945193/davidbouncescrollview

以上所述是小编给大家介绍的android开发实现带有反弹效果仿ios反弹scrollview教程详解,希望对大家有所帮助

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

相关文章:

验证码:
移动技术网