android ScrollView實現(xiàn)水平滑動回彈
本文實例為大家分享了android ScrollView實現(xiàn)水平滑動回彈的具體代碼,供大家參考,具體內(nèi)容如下
在研究了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布局執(zhí)行完后執(zhí)行此方法
?? ? */
?? ?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:
?? ??? ??? ?// 在當前視圖內(nèi)容繼續(xù)偏移(x , y)個單位,顯示(可視)區(qū)域也跟著偏移(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);
?? ??? ?// 動畫持續(xù)時間
?? ??? ?ta.setDuration(50);
?? ??? ?view.startAnimation(ta);
?? ??? ?// 設(shè)置回到當前正常的布局位置
?? ??? ?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所以向右拉不動
?? ??? ?// 但當向左拉動到內(nèi)容布局的最右端時scrollX == offset時還可以繼續(xù)拉動
?? ??? ?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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Android業(yè)務(wù)組件化之URL Schema使用
這篇文章主要為大家詳細介紹了Android業(yè)務(wù)組件化之URL Schema使用,感興趣的小伙伴們可以參考一下2016-09-09
Android開發(fā)實現(xiàn)Launcher3應(yīng)用列表修改透明背景的方法
這篇文章主要介紹了Android開發(fā)實現(xiàn)Launcher3應(yīng)用列表修改透明背景的方法,結(jié)合實例形式分析了Launcher3相關(guān)配置文件與功能函數(shù)修改設(shè)置操作技巧,需要的朋友可以參考下2017-11-11
Android開發(fā)之TabActivity用法實例詳解
這篇文章主要介紹了Android開發(fā)之TabActivity用法,結(jié)合實例形式較為詳細的分析了Android擴展Activity實現(xiàn)標簽頁效果的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-03-03
Android編程實現(xiàn)動態(tài)支持多語言的方法
這篇文章主要介紹了Android編程實現(xiàn)動態(tài)支持多語言的方法,涉及Android資源、控件及屬性相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
深入分析Android NFC技術(shù) android nfc開發(fā)
本篇文章我們對android開發(fā)中nfc技術(shù)做了全面的原理分析以及實現(xiàn)過程,需要的讀者們一起參考一下吧。2017-11-11
Kotlin Service實現(xiàn)消息推送通知過程
這幾天分析了一下的啟動過程,于是乎,今天寫一下Service使用; 給我的感覺是它并不復(fù)雜,千萬不要被一坨一坨的代碼嚇住了,雖然彎彎繞繞不少,重載函數(shù)一個接著一個,就向走迷宮一樣,但只要抓住主線閱讀,很快就能找到出口2022-12-12
Android 仿摩拜單車共享單車進度條實現(xiàn)StepView效果
這篇文章主要介紹了android 仿摩拜單車共享單車進度條實現(xiàn)StepView效果的實例,通過定義五個狀態(tài),分別為:為完成、正在進行、已完成、終點完成、終點未完成。具體實現(xiàn)代碼,大家參考下2017-03-03

