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

public class MyView extends ScrollView { //記錄下最開始點(diǎn)擊的位置 int initY; //移動(dòng)的位置 int deltaY; int touchY; //記錄第一個(gè)item的位置的矩形 Rect topRect; //用來存放第一個(gè)可見的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; } //當(dāng)布局加載完成之后調(diào)用該方法 @Override protected void onFinishInflate() { super.onFinishInflate(); //返回加載完成后所看到的第一個(gè)item,這里就是看到的第一個(gè)item,通過對該對象的移動(dòng)來實(shí)現(xiàn)整體的移動(dòng) inner=getChildAt(0); Log.i("inner", inner.toString()); } //onTouchEvent的返回值 返回true的話表示該事件已經(jīng)被處理了,返回false表示改時(shí)間還未被處理 @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; } //開始的時(shí)候接觸點(diǎn)的坐標(biāo)值 initY=(int) ev.getY(); //記錄下ImageView的原始高度 initTop=imageView.getTop(); //記錄下ImageView的原始的底部的像素坐標(biāo) initButtom=imageView.getBottom(); break; } case MotionEvent.ACTION_MOVE: { //滑動(dòng)的距離 deltaY=(int) (ev.getY()-initY); if(deltaY<0) { //向上滑動(dòng) state=State.UP; isMoving=false; shutScroll=false; } else if(deltaY>=0) { //在這里做一下判斷,當(dāng)getScrollY為0時(shí),繼續(xù)下拉就會(huì)進(jìn)入down狀態(tài)。 if(getScrollY()==0) { //向下滑動(dòng) 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) { /*這里為什么要這么寫呢?這里有很重要的一個(gè)知識(shí)點(diǎn): * getScrollY()返回的是手機(jī)屏幕左上角和調(diào)用該方法的view的左上角之間的Y坐標(biāo)只差。 * 在這里,自定義空間的布局方式看看布局文件就會(huì)發(fā)現(xiàn),當(dāng)View滑動(dòng)的時(shí)候,View的狀態(tài)在up,normal; * down之間切換。在View下來的過程中,normal和down有一個(gè)臨界值,這個(gè)臨界值就是該view的 * 左上角是不是和屏幕的左上角相等。相等的話就說明再向下拉的話就down狀態(tài)了。*/ state=State.NORMAL; } break; } } } private void animation() { //背景圖片平移的動(dòng)畫 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); // 開啟移動(dòng)動(dòng)畫 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實(shí)現(xiàn)簡單的下拉阻尼效應(yīng)示例代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注