溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在Android中實現一個下拉阻尼效應

發布時間:2021-04-08 17:46:22 來源:億速云 閱讀:296 作者:Leah 欄目:移動開發

本篇文章給大家分享的是有關怎么在Android中實現一個下拉阻尼效應,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

public class MyView extends ScrollView {
	//記錄下最開始點擊的位置
	int initY;
	//移動的位置
	int deltaY;
	
	int touchY;
	//記錄第一個item的位置的矩形
	Rect topRect;
	//用來存放第一個可見的item
	View inner;
	//記錄下ImageView最原始的頂部位置和底部位置
	int initTop,initButtom;
	int left = 0,top = 0,right = 0,bottom = 0;
	ImageView imageView;
	State state;
	 boolean recordFlag;
	 enum State
	{
		UP,NORMAL,DOWN
	}
	
	 
	boolean isMoving;
	boolean shutScroll;
	private int current_Bottom;
	private int current_Top;
	
	public MyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		state = State.NORMAL;
		topRect=new Rect();
		recordFlag=false;
		
	}
	public void setImageView(ImageView imageView)
	{
		this.imageView=imageView;
	}
	
	//當布局加載完成之后調用該方法
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		//返回加載完成后所看到的第一個item,這里就是看到的第一個item,通過對該對象的移動來實現整體的移動
		inner=getChildAt(0);
		Log.i("inner", inner.toString());
	}
	//onTouchEvent的返回值 返回true的話表示該事件已經被處理了,返回false表示改時間還未被處理
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if(inner!=null)
		{
			commOnTouchEvent(ev);
			
		}
		if(shutScroll)
		{
			return true;
		}else
		{
			return super.onTouchEvent(ev);
		}
		
	}
	private void commOnTouchEvent(MotionEvent ev) {
		switch(ev.getAction())
		{
		case MotionEvent.ACTION_DOWN:
		{
			if(recordFlag==false)
			{
				left=inner.getLeft();
				top=inner.getTop();
				right=inner.getRight();
				bottom=inner.getBottom();
				recordFlag=true;
			}
			
			//開始的時候接觸點的坐標值
			initY=(int) ev.getY();
			//記錄下ImageView的原始高度
			initTop=imageView.getTop();
			//記錄下ImageView的原始的底部的像素坐標
			initButtom=imageView.getBottom();
			break;
		}
		
		case MotionEvent.ACTION_MOVE:
		{
			//滑動的距離
			deltaY=(int) (ev.getY()-initY);
			
			if(deltaY<0)
			{
				//向上滑動
				state=State.UP;	
				isMoving=false;
				shutScroll=false;
			}
			else if(deltaY>=0)
			{
				//在這里做一下判斷,當getScrollY為0時,繼續下拉就會進入down狀態。
				if(getScrollY()==0)
				{
					//向下滑動
					state=State.DOWN;
					isMoving=true;
					shutScroll=true;
				}
				
			}
			
			if(isMoving)
			{
				if (topRect.isEmpty()) {
					
					// 保存正常的布局位置
					topRect.set(left, top,right,bottom);
				}
					
				float inner_move_H = deltaY / 5;
				inner.layout(topRect.left, (int) (topRect.top + inner_move_H),
							topRect.right, (int) (topRect.bottom + inner_move_H));
				float image_move_H = deltaY / 10;
				current_Top = (int) (initTop + image_move_H);
				current_Bottom = (int) (initButtom + image_move_H);
				imageView.layout(imageView.getLeft(), current_Top,
							imageView.getRight(), current_Bottom);
				
			}
			break;
		}
		
		
		case MotionEvent.ACTION_UP:
		{
			if(needToScroll())
			{
				animation();
				
			}
			if(getScrollY()==0)
			{
				/*這里為什么要這么寫呢?這里有很重要的一個知識點:
				 * getScrollY()返回的是手機屏幕左上角和調用該方法的view的左上角之間的Y坐標只差。
				 * 在這里,自定義空間的布局方式看看布局文件就會發現,當View滑動的時候,View的狀態在up,normal;
				 * down之間切換。在View下來的過程中,normal和down有一個臨界值,這個臨界值就是該view的
				 * 左上角是不是和屏幕的左上角相等。相等的話就說明再向下拉的話就down狀態了。*/	
				
				state=State.NORMAL;
			}
			break;
		}
		}
		
	}
	private void animation() {
		//背景圖片平移的動畫
		TranslateAnimation image_Anim = new TranslateAnimation(0, 0,
				Math.abs(initTop - current_Top), 0);
		image_Anim.setDuration(200);
		imageView.startAnimation(image_Anim);
		imageView.layout(imageView.getLeft(), (int) initTop,
				imageView.getRight(), (int) initButtom);
		// 開啟移動動畫
		TranslateAnimation inner_Anim = new TranslateAnimation(0, 0,
				inner.getTop(), topRect.top);
		inner_Anim.setDuration(200);
		inner.startAnimation(inner_Anim);
		inner.layout(topRect.left, topRect.top, topRect.right, topRect.bottom);
		//state=State.NORMAL;
		topRect.setEmpty();
	}
	private boolean needToScroll() {
		if(state==State.DOWN)
		{
			return true;
		}
		return false;
	}
}

以上就是怎么在Android中實現一個下拉阻尼效應,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女