android ScrollView實(shí)現(xiàn)水平滑動(dòng)回彈
本文實(shí)例為大家分享了android ScrollView實(shí)現(xiàn)水平滑動(dòng)回彈的具體代碼,供大家參考,具體內(nèi)容如下
在研究了View的一些屬性之后做了個(gè)Scroll的水平滑動(dòng)回彈。
效果圖:
主要代碼:
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水平滑動(dòng)回彈 ?*? ?* @author qhg ?* @date 2014年3月12日 ?*? ?*/ public class MHorizontalScrollView extends HorizontalScrollView { ? ?? ?private View view; ?? ?/** ?? ? * 移動(dòng)時(shí)的增量 ?? ? */ ?? ?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: ?? ??? ??? ?// 在當(dāng)前視圖內(nèi)容繼續(xù)偏移(x , y)個(gè)單位,顯示(可視)區(qū)域也跟著偏移(x,y)個(gè)單位 ?? ??? ??? ?scrollBy(deltaX, 0); ?? ??? ??? ?// 當(dāng)滾動(dòng)到最左或最右時(shí)就不會(huì)再滾動(dòng),這時(shí)移動(dòng)布局達(dá)到回彈效果 ?? ??? ??? ?if (isLayoutMove()) { ?? ??? ??? ??? ?if (normalRt.isEmpty()) { ?? ??? ??? ??? ??? ?// 保存當(dāng)前正常的布局位置,拉過(guò)頭才能回彈到正常位置 ?? ??? ??? ??? ??? ?normalRt.set(view.getLeft(), view.getTop(), ?? ??? ??? ??? ??? ??? ??? ?view.getRight(), view.getBottom()); ? ?? ??? ??? ??? ?} ?? ??? ??? ??? ?// 移動(dòng)布局 ?? ??? ??? ??? ?view.layout(view.getLeft() - deltaX, view.getTop(), ?? ??? ??? ??? ??? ??? ?view.getRight() - deltaX, view.getBottom()); ?? ??? ??? ?} ?? ??? ??? ?break; ?? ??? ?case MotionEvent.ACTION_UP: ?? ??? ??? ?if (isNeedAnimation()) { ?? ??? ??? ??? ?animationImpl(); ?? ??? ??? ?} ?? ??? ??? ?break; ?? ??? ?default: ?? ??? ??? ?break; ?? ??? ?} ?? ?} ? ?? ?/** ?? ? * 動(dòng)畫(huà)移動(dòng) ?? ? */ ?? ?private void animationImpl() { ?? ??? ?// 移動(dòng)動(dòng)畫(huà) ?? ??? ?TranslateAnimation ta = new TranslateAnimation(view.getLeft(), ?? ??? ??? ??? ?normalRt.left, 0, 0); ?? ??? ?// 動(dòng)畫(huà)持續(xù)時(shí)間 ?? ??? ?ta.setDuration(50); ?? ??? ?view.startAnimation(ta); ?? ??? ?// 設(shè)置回到當(dāng)前正常的布局位置 ?? ??? ?view.layout(normalRt.left, normalRt.top, normalRt.right, ?? ??? ??? ??? ?normalRt.bottom); ?? ??? ?normalRt.setEmpty(); ?? ?} ? ?? ?/** ?? ? * 是否需要開(kāi)啟動(dòng)畫(huà) ?? ? *? ?? ? * @return ?? ? */ ?? ?private boolean isNeedAnimation() { ?? ??? ?return !normalRt.isEmpty(); ?? ?} ? ?? ?/** ?? ? * 是否需要移動(dòng)布局 ?? ? *? ?? ? * @return ?? ? */ ?? ?private boolean isLayoutMove() { ?? ??? ?int offset = view.getMeasuredWidth() - getWidth(); ?? ??? ?if (offset <= 0) { ?? ??? ??? ?return false; ?? ??? ?} ?? ??? ?// 上面已固定deltaX=1,scrollX永遠(yuǎn)等于1所以向右拉不動(dòng) ?? ??? ?// 但當(dāng)向左拉動(dòng)到內(nèi)容布局的最右端時(shí)scrollX == offset時(shí)還可以繼續(xù)拉動(dòng) ?? ??? ?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" ? ? ? ? ? ? ? ? ?/> ? ? ? ? ? ? <!-- 使右邊多空一點(diǎn) --> ? ? ? ? ? ? <ImageView ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:layout_marginRight="20dp" ? ? ? ? ? ? ? ? ?/> ? ? ? ? </LinearLayout> ? ? </cn.qhg.MHorizontalScrollView> ? </LinearLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android自定義View實(shí)現(xiàn)圓環(huán)顏色選擇器
這篇文章主要介紹了android自定義View實(shí)現(xiàn)圓環(huán)顏色選擇器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android仿IOS ViewPager滑動(dòng)進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android仿IOS ViewPager滑動(dòng)進(jìn)度條的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01基于Android中Webview使用自定義的javascript進(jìn)行回調(diào)的問(wèn)題詳解
本篇文章對(duì)Android中Webview使用自定義的javascript進(jìn)行回調(diào)的問(wèn)題進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05Android實(shí)現(xiàn)圖片壓縮(bitmap的六種壓縮方式)
Android中圖片是以bitmap形式存在的,這篇文章主要介紹了Android實(shí)現(xiàn)圖片壓縮(bitmap的六種壓縮方式),有興趣的可以了解一下。2017-02-02