日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

android?ScrollView實現水平滑動回彈_Android

作者:球球_2014 ? 更新時間: 2022-06-18 編程語言

本文實例為大家分享了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

欄目分類
最近更新