欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)簡單的下拉阻尼效應示例代碼

 更新時間:2018年01月20日 12:28:35   作者:xiaohongjun_can  
下面小編就為大家分享一篇Android實現(xiàn)簡單的下拉阻尼效應示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

OS的下拉上拉都會出現(xiàn)一個很玄的動態(tài)效果。在Android中,雖然可以實現(xiàn)類似的效果,但有點不同的是,如果調(diào)用overScrollBy來實現(xiàn)類似的阻尼效應的話,最頂部會出現(xiàn)一片亮的區(qū)域,讓人感覺不是很爽。所以決定不采用該方法來實現(xiàn)而是改用自定義的方式來實現(xiàn)。

下面是自定義控件的代碼部分:

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;
	}
	
	//當布局加載完成之后調(diào)用該方法
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		//返回加載完成后所看到的第一個item,這里就是看到的第一個item,通過對該對象的移動來實現(xiàn)整體的移動
		inner=getChildAt(0);
		Log.i("inner", inner.toString());
	}
	//onTouchEvent的返回值 返回true的話表示該事件已經(jīng)被處理了,返回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時,繼續(xù)下拉就會進入down狀態(tài)。
				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()返回的是手機屏幕左上角和調(diào)用該方法的view的左上角之間的Y坐標只差。
				 * 在這里,自定義空間的布局方式看看布局文件就會發(fā)現(xiàn),當View滑動的時候,View的狀態(tài)在up,normal;
				 * down之間切換。在View下來的過程中,normal和down有一個臨界值,這個臨界值就是該view的
				 * 左上角是不是和屏幕的左上角相等。相等的話就說明再向下拉的話就down狀態(tài)了。*/	
				
				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實現(xiàn)簡單的下拉阻尼效應示例代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論