【No11.】仿IOS界面最常用的回彈效果

好久沒來寫文章了!

今天爲大家分享一個仿照ios界面的回彈效果。(下面先上效果圖)

 

 

 

 

其實就是通過重寫ScrollView,監聽onTouchEvent方法,再去移動佈局實現回彈效果;

 

上代碼:

 

IosScrollViewLayout.java

 


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.ScrollView;

public class IosScrollViewLayout extends ScrollView {

	// 拖動的距離 size = 3 的意思 只允許拖動屏幕的1/3
	private static final int size = 3;
	private View inner;
	private float y;
	private Rect normal = new Rect();;

	public IosScrollViewLayout(Context context) {
		super(context);
		setVerticalScrollBarEnabled(false);//隱藏ScrollView滾動條
	}

	public IosScrollViewLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		setVerticalScrollBarEnabled(false);//隱藏ScrollView滾動條
	}

	@Override
	protected void onFinishInflate() {
		if (getChildCount() > 0) {
			inner = getChildAt(0);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if (inner == null) {
			return super.onTouchEvent(ev);
		} else {
			commOnTouchEvent(ev);
		}
		return super.onTouchEvent(ev);
	}

	public void commOnTouchEvent(MotionEvent ev) {
		int action = ev.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			y = ev.getY();
			break;
		case MotionEvent.ACTION_UP:
			if (isNeedAnimation()) {
				// Log.v("mlguitar", "will up and animation");
				animation();
			}
			break;
		case MotionEvent.ACTION_MOVE:
			final float preY = y;
			float nowY = ev.getY();
			/**
			 * size=3 表示 拖動的距離爲屏幕的高度的1/3
			 */
			int deltaY = (int) (preY - nowY) / size;
			// 滾動
			// scrollBy(0, deltaY);

			y = nowY;
			// 當滾動到最上或者最下時就不會再滾動,這時移動佈局
			if (isNeedMove()) {
				if (normal.isEmpty()) {
					// 保存正常的佈局位置
					normal.set(inner.getLeft(), inner.getTop(),
							inner.getRight(), inner.getBottom());
					return;
				}
				int yy = inner.getTop() - deltaY;

				// 移動佈局
				inner.layout(inner.getLeft(), yy, inner.getRight(),
						inner.getBottom() - deltaY);
			}
			break;
		default:
			break;
		}
	}

	// 開啓動畫移動

	public void animation() {
		// 開啓移動動畫
		TranslateAnimation ta = new TranslateAnimation(0, 0, inner.getTop(),
				normal.top);
		ta.setDuration(200);
		inner.startAnimation(ta);
		// 設置回到正常的佈局位置
		inner.layout(normal.left, normal.top, normal.right, normal.bottom);
		normal.setEmpty();
	}

	// 是否需要開啓動畫
	public boolean isNeedAnimation() {
		return !normal.isEmpty();
	}

	// 是否需要移動佈局
	public boolean isNeedMove() {
		int offset = inner.getMeasuredHeight() - getHeight();
		int scrollY = getScrollY();
		if (scrollY == 0 || scrollY == offset) {
			return true;
		}
		return false;
	}

}

 

 

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.example.myscrollview.IosScrollViewLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仿IOS界面回彈效果" />
        </LinearLayout>
    </com.example.myscrollview.IosScrollViewLayout>

</RelativeLayout>


 

MainActivity.java


import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

}


 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章