android 横向滑动 回弹,android ScrollView水平滑动回弹
在研究了View的一些属性之后做了个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.HorizontalScrollView;
/**
* ScrollView水平滑动回弹
*
* @author qhg
* @date 2014年3月12日
*
*/
public class MHorizontalScrollView extends HorizontalScrollView {
private View view;
/**
* 移动时的增量
*/
private static final int deltaX = 1;
private Rect normalRt = new Rect();
public MHorizontalScrollView(Context context) {
super(context);
}
public MHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 在xml布局执行完后执行此方法
*/
protected void onFinishInflate() {
if (getChildCount() > 0) {
view = getChildAt(0);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (view != null) {
onTouchEventImpl(event);
}
return super.onTouchEvent(event);
}
private void onTouchEventImpl(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
// 在当前视图内容继续偏移(x , y)个单位,显示(可视)区域也跟着偏移(x,y)个单位
scrollBy(deltaX, 0);
// 当滚动到最左或最右时就不会再滚动,这时移动布局达到回弹效果
if (isLayoutMove()) {
if (normalRt.isEmpty()) {
// 保存当前正常的布局位置,拉过头才能回弹到正常位置
normalRt.set(view.getLeft(), view.getTop(),
view.getRight(), view.getBottom());
}
// 移动布局
view.layout(view.getLeft() - deltaX, view.getTop(),
view.getRight() - deltaX, view.getBottom());
}
break;
case MotionEvent.ACTION_UP:
if (isNeedAnimation()) {
animationImpl();
}
break;
default:
break;
}
}
/**
* 动画移动
*/
private void animationImpl() {
// 移动动画
TranslateAnimation ta = new TranslateAnimation(view.getLeft(),
normalRt.left, 0, 0);
// 动画持续时间
ta.setDuration(50);
view.startAnimation(ta);
// 设置回到当前正常的布局位置
view.layout(normalRt.left, normalRt.top, normalRt.right,
normalRt.bottom);
normalRt.setEmpty();
}
/**
* 是否需要开启动画
*
* @return
*/
private boolean isNeedAnimation() {
return !normalRt.isEmpty();
}
/**
* 是否需要移动布局
*
* @return
*/
private boolean isLayoutMove() {
int offset = view.getMeasuredWidth() - getWidth();
if (offset <= 0) {
return false;
}
// 上面已固定deltaX=1,scrollX永远等于1所以向右拉不动
// 但当向左拉动到内容布局的最右端时scrollX == offset时还可以继续拉动
int scrollX = getScrollX();
if (scrollX == 0 || scrollX == offset) {
return true;
}
return false;
}
}
在xml布局文件里直接使用:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/background"
>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingTop="100dp"
android:id="@+id/ll_test"
android:onClick="test"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/house_1"
android:layout_marginRight="40dp"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/house_4"
android:layout_marginRight="40dp"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/house_2"
android:layout_marginRight="40dp"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/house_3"
android:layout_marginRight="40dp"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/house_1"
android:layout_marginRight="40dp"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/house_4"
android:layout_marginRight="40dp"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/house_2"
android:layout_marginRight="40dp"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/house_3"
android:layout_marginRight="40dp"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
/>
Android爱好者之家 QQ群1:321585812
群内会不定期更新传智播客黎老师的视频讲座,欢迎交流学习!
android 横向滑动 回弹,android ScrollView水平滑动回弹相关推荐
- Android横向滚动卡片,Android实现横向滑动卡片效果
Android实现横向滑动卡片效果 发布时间:2020-09-04 22:51:17 来源:脚本之家 阅读:255 作者:itbobby 最近项目上需要实现这样效果的一个页面,本来想找个现成的两下搞定 ...
- android 横向滚动图片,Android使用Photoview实现图片左右滑动及缩放功能
Android使用Photoview实现图片左右滑动及缩放功能 发布时间:2020-10-12 14:51:12 来源:脚本之家 阅读:119 作者:kuaizilanqiu 我想,不管是做什么样的a ...
- ScrollView水平滑动条选中条目的居中显示
创建选择器实现圆角矩形<shape xmlns:android="http://schemas.android.com/apk/res/android"android:sha ...
- android 横向多级列表,Android实现横向二级菜单
本文实例为大家分享了Android二级横向菜单的实现过程.效果如上图: 这种横向的二级菜单在很多的app都有所应用.效果看起来还是非常的美观的.也算是项目需要,自己也就学了一下这个效果,首先说一下逻辑 ...
- android 横向跑马灯,Android 横向 跑马灯效果TextView
你瞅啥? 自定义控件MarqueeView import android.content.Context; import android.graphics.Rect; import android.t ...
- Android横向滚动卡片,Android滑动卡片成效:Swipecards
一个类似于 Tinder的Android库,用于创建滑动卡片效果. 您可以向左或向右滑动来切换喜欢或不喜欢的内容. //implement the onFlingListener public cla ...
- Android横向滚动卡片,Android仿探探卡片式滑动效果实现
前言 第一次进入探探软件界面,就被这种通过卡片式滑动来选择"喜欢/不喜欢"的设计所吸引了.当时就非常想通过自己来实现这种仿探探式的效果,然而却没什么思路.不过毋庸置疑的是,这种效果 ...
- android 横向拖动选取,Android HorizontalRefresh 一个可以左右拖动刷新的控件
欢迎Follow我的GitHub, 关注我的CSDN. 其余参考Android目录. 欢迎关注我们的微信公众号:杨守乐 转载来源:杨守乐 本文由:[吧主的博客]分享 每日英语 life is craz ...
- android 横向竖排文字,[Android]实现文字竖排
最终效果:文字竖排,英文字母是横着的,中文字是竖的,如下图 竖排中英混合.PNG 分析:英文是一个完整的单词旋转了90度,中文一字一行 思路: 要放textview的地方用LinearLayout占位 ...
最新文章
- bzoj2339[HNOI2011]卡农 dp+容斥
- 手机网页 右边的空白区
- 数组中a和a 的区别?
- centos6.5_x64远程链接输入正确的账号密码无法登陆
- 数据集特征提取_基于PCA算法实现鸢尾花数据集的特征提取任务
- sap.ui.viewModifications view extension
- 广东电网计算机专业笔试题目,广东电网笔试题目
- 计算机网络学习socket--day3
- Word2Vec简明教程:入门、原理及代码实现
- Redis雪崩,击穿,穿透产生原因以及其解决方案
- blos硬盘启动台式计算机,最新戴尔台式机bios设置硬盘启动图解
- java socket 实现原理_java socket原理
- 有没有Type-C接口的无线投屏器?HDMI、USB、Type-C接口的无线投屏器又有什么区别呢?
- SQL注入之Mssql注入及原理
- 小米净水器更换滤芯步骤和水龙头亮黄灯的解决办法
- 【机器学习】机器学习之梯度下降法简单应用(Python)
- iPad怎么分屏?学会这个方法,轻松拿捏
- Android数据库选择恐惧症
- 华为防火墙性能指标监控
- 上海交通大学2022年计算机考研复试分数线
热门文章
- 计算机考研专业课除了408,计算机改考408的院校越来越多,考研难度系数更高了吗?...
- android 蓝牙4.0(BLE) 开发
- 基于Istio的高级流量管理二(Envoy流量劫持、Istio架构、高级流量管理)
- 2的2.8次方用计算机怎么算,计算器2.8e9是多少,谢谢
- 世上最杰出程序员,B 语言、Unix 之父嫌计算机发展太慢,让孩子学生物?
- OSChina 周四乱弹 —— 每次我穿短裙的时候
- 《Photoshop Lightroom4 经典教程》—第1课1.5节修改和编辑照片
- html5单击修改背景色,js实现点击ul/li等改变背景颜色
- 找出用户需求,挖掘用户痛点
- java二维数组扫雷,C语言二维数组实现扫雷游戏