最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android ScrollView實現(xiàn)水平滑動回彈

 更新時間:2022年04月18日 17:11:55   作者:球球_2014  
這篇文章主要為大家詳細(xì)介紹了android ScrollView實現(xiàn)水平滑動回彈,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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:
?? ??? ??? ?// 在當(dāng)前視圖內(nèi)容繼續(xù)偏移(x , y)個單位,顯示(可視)區(qū)域也跟著偏移(x,y)個單位
?? ??? ??? ?scrollBy(deltaX, 0);
?? ??? ??? ?// 當(dāng)滾動到最左或最右時就不會再滾動,這時移動布局達(dá)到回彈效果
?? ??? ??? ?if (isLayoutMove()) {
?? ??? ??? ??? ?if (normalRt.isEmpty()) {
?? ??? ??? ??? ??? ?// 保存當(dāng)前正常的布局位置,拉過頭才能回彈到正常位置
?? ??? ??? ??? ??? ?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è)置回到當(dāng)前正常的布局位置
?? ??? ?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永遠(yuǎn)等于1所以向右拉不動
?? ??? ?// 但當(dāng)向左拉動到內(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使用

    這篇文章主要為大家詳細(xì)介紹了Android業(yè)務(wù)組件化之URL Schema使用,感興趣的小伙伴們可以參考一下
    2016-09-09
  • 深入淺析Android Fragment(下篇)

    深入淺析Android Fragment(下篇)

    本篇文章給大家介紹如何管理Fragment回退棧,F(xiàn)ragment如何與Activity交互,F(xiàn)ragment與Activity交互的最佳實踐,沒有視圖的Fragment的用處,使用Fragment創(chuàng)建對話框,如何與ActionBar,MenuItem集成,對Android Fragment感興趣的朋友可以參考下本篇文章
    2015-10-10
  • Android開發(fā)實現(xiàn)Launcher3應(yīng)用列表修改透明背景的方法

    Android開發(fā)實現(xiàn)Launcher3應(yīng)用列表修改透明背景的方法

    這篇文章主要介紹了Android開發(fā)實現(xiàn)Launcher3應(yīng)用列表修改透明背景的方法,結(jié)合實例形式分析了Launcher3相關(guān)配置文件與功能函數(shù)修改設(shè)置操作技巧,需要的朋友可以參考下
    2017-11-11
  • Android 雙擊返回鍵退出程序的方法總結(jié)

    Android 雙擊返回鍵退出程序的方法總結(jié)

    這篇文章主要介紹了Android 雙擊返回鍵退出程序的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android實現(xiàn)帶有進(jìn)度條的按鈕效果

    Android實現(xiàn)帶有進(jìn)度條的按鈕效果

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)帶有進(jìn)度條的按鈕效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Android開發(fā)之TabActivity用法實例詳解

    Android開發(fā)之TabActivity用法實例詳解

    這篇文章主要介紹了Android開發(fā)之TabActivity用法,結(jié)合實例形式較為詳細(xì)的分析了Android擴展Activity實現(xiàn)標(biāo)簽頁效果的具體步驟與相關(guān)技巧,需要的朋友可以參考下
    2016-03-03
  • Android編程實現(xiàn)動態(tài)支持多語言的方法

    Android編程實現(xiàn)動態(tài)支持多語言的方法

    這篇文章主要介紹了Android編程實現(xiàn)動態(tài)支持多語言的方法,涉及Android資源、控件及屬性相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06
  • 深入分析Android NFC技術(shù) android nfc開發(fā)

    深入分析Android NFC技術(shù) android nfc開發(fā)

    本篇文章我們對android開發(fā)中nfc技術(shù)做了全面的原理分析以及實現(xiàn)過程,需要的讀者們一起參考一下吧。
    2017-11-11
  • Kotlin Service實現(xiàn)消息推送通知過程

    Kotlin Service實現(xiàn)消息推送通知過程

    這幾天分析了一下的啟動過程,于是乎,今天寫一下Service使用; 給我的感覺是它并不復(fù)雜,千萬不要被一坨一坨的代碼嚇住了,雖然彎彎繞繞不少,重載函數(shù)一個接著一個,就向走迷宮一樣,但只要抓住主線閱讀,很快就能找到出口
    2022-12-12
  • Android 仿摩拜單車共享單車進(jìn)度條實現(xiàn)StepView效果

    Android 仿摩拜單車共享單車進(jìn)度條實現(xiàn)StepView效果

    這篇文章主要介紹了android 仿摩拜單車共享單車進(jìn)度條實現(xiàn)StepView效果的實例,通過定義五個狀態(tài),分別為:為完成、正在進(jìn)行、已完成、終點完成、終點未完成。具體實現(xiàn)代碼,大家參考下
    2017-03-03

最新評論

建宁县| 宁夏| 日照市| 醴陵市| 红原县| 濉溪县| 峨山| 琼海市| 湖北省| 汾阳市| 鲜城| 崇左市| 大兴区| 宁陵县| 合阳县| 嘉禾县| 淮阳县| 合山市| 定西市| 杭锦旗| 德惠市| 子洲县| 哈巴河县| 商河县| 桐乡市| 崇文区| 海口市| 论坛| 新丰县| 玛沁县| 铜山县| 静海县| 胶州市| 南充市| 若羌县| 岫岩| 湟源县| 镇坪县| 华宁县| 南安市| 龙胜|