網站首頁 編程語言 正文
本文實例為大家分享了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布局文件里直接使用:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="fill_parent" ? ? android:layout_height="fill_parent" ? ? android:orientation="vertical"? ? ? android:background="@drawable/background" ? ? > ? ? ? <cn.qhg.MHorizontalScrollView ? ? ? ? android:layout_width="fill_parent" ? ? ? ? android:layout_height="fill_parent" ? ? ? ? android:scrollbars="none"? ? ? ? ? > ? ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? android:orientation="horizontal"? ? ? ? ? ? ? android:paddingTop="100dp" ? ? ? ? ? ? android:id="@+id/ll_test" ? ? ? ? ? ? android:onClick="test" ? ? ? ? ? ? > ? ? ? ? ? ? ? <ImageView ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:background="@drawable/house_1"? ? ? ? ? ? ? ? ? android:layout_marginRight="40dp" ? ? ? ? ? ? ? ? /> ? ? ? ? ? ? ? <ImageView ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:background="@drawable/house_4"? ? ? ? ? ? ? ? ? android:layout_marginRight="40dp" ? ? ? ? ? ? ? ? /> ? ? ? ? ? ? ? <ImageView ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:background="@drawable/house_2" ? ? ? ? ? ? ? ? android:layout_marginRight="40dp" ? ? ? ? ? ? ? ? ?/> ? ? ? ? ? ? ? <ImageView ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:background="@drawable/house_3" ? ? ? ? ? ? ? ? android:layout_marginRight="40dp" ? ? ? ? ? ? ? ? ?/> ? ? ? ? ? ? <ImageView ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:background="@drawable/house_1"? ? ? ? ? ? ? ? ? android:layout_marginRight="40dp" ? ? ? ? ? ? ? ? /> ? ? ? ? ? ? ? <ImageView ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:background="@drawable/house_4"? ? ? ? ? ? ? ? ? android:layout_marginRight="40dp" ? ? ? ? ? ? ? ? /> ? ? ? ? ? ? ? <ImageView ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:background="@drawable/house_2" ? ? ? ? ? ? ? ? android:layout_marginRight="40dp" ? ? ? ? ? ? ? ? ?/> ? ? ? ? ? ? ? <ImageView ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:background="@drawable/house_3" ? ? ? ? ? ? ? ? android:layout_marginRight="40dp" ? ? ? ? ? ? ? ? ?/> ? ? ? ? ? ? <!-- 使右邊多空一點 --> ? ? ? ? ? ? <ImageView ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:layout_marginRight="20dp" ? ? ? ? ? ? ? ? ?/> ? ? ? ? </LinearLayout> ? ? </cn.qhg.MHorizontalScrollView> ? </LinearLayout>
原文鏈接:https://blog.csdn.net/qhg2014/article/details/21102913
相關推薦
- 2022-07-12 Oracle 19c 創建和遷移PDB的幾種方式
- 2022-05-04 python中的PywebIO模塊制作一個數據大屏_python
- 2022-04-10 elasticsearch + spring boot 配置
- 2023-07-30 el-table自定義合并行或列
- 2022-04-20 C#實現變量交換、斐波那契數列、質數、回文方法合集_C#教程
- 2023-04-03 Input系統截斷策略的分析與應用詳解_Android
- 2022-04-27 簡單聊聊PyTorch里面的torch.nn.Parameter()_python
- 2022-02-12 使用background-attachment實現視差滾動、水波
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支