Android實(shí)現(xiàn)滑動(dòng)效果
本文實(shí)例為大家分享了Android實(shí)現(xiàn)滑動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下
坐標(biāo)系與視圖坐標(biāo)系相輔相成
1、坐標(biāo)系:描述了View在屏幕中的位置關(guān)系(以屏幕最左上角的頂點(diǎn)作為Android坐標(biāo)系的原點(diǎn))
2、視圖坐標(biāo)系:描述了子視圖在父視圖中的位置關(guān)系(以父視圖最左上角為坐標(biāo)系原點(diǎn))
獲取坐標(biāo)值的方法
1.View提供的獲取坐標(biāo)方法
getTop():獲取到的是View自身的頂邊到其父布局頂邊的距離getLeft():獲取到的是View自身的左邊到其父布局頂邊的距離getRight():獲取到的是View自身的右邊到其父布局頂邊的距離getBottom():獲取到的是View自身的底邊到其父布局頂邊的距離
2. MotionEvent提供的方法
getX():獲取點(diǎn)擊事件距離控件左邊的距離,即視圖坐標(biāo)getY():獲取點(diǎn)擊事件距離控件頂邊的距離,即視圖坐標(biāo)getRawX():獲取點(diǎn)擊事件距離整個(gè)屏幕左邊的距離,即絕對(duì)坐標(biāo)getRawY():獲取點(diǎn)擊事件距離整個(gè)屏幕右邊的距離,即絕對(duì)坐標(biāo)
實(shí)現(xiàn)滑動(dòng)的七種方法
1.layout方法
case MotionEvent.ACTION_MOVE: //計(jì)算偏移量 int offsetX=x-lastX; int offsetY=y-lastY; layout(getLeft()+offsetX,getTop()+offsetY,getRight()+offsetX,getBottom()+offsetY); break;
2.offsetLeftAndRight()與 offsetTopAndBottom()
offsetLeftAndRight(offsetX);offsetTopAndBottom(offsetY);
3.LayoutParams
LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) getLayoutParams();params.leftMargin= getLeft()+offsetX;params.topMargin= getTop()+offsetY;setLayoutParams(params);
4.scrollBy()與scrollTo()
scrollBy(x,y)表示移動(dòng)到一個(gè)具體的位置scrollTo(dx,dy)表示移動(dòng)的增量為dx,dy
int offsetX=x-lastX;int offsetY=y-lastY;View parent= (View) getParent();parent.scrollBy(-offsetX,-offsetY);
5.Scroller
通過(guò)Scroller類可以實(shí)現(xiàn)平滑移動(dòng)的效果,而不是瞬間完成的效果,與動(dòng)畫的實(shí)現(xiàn)原理基本相似
@Override public void computeScroll() { super.computeScroll(); //判斷scroller是否執(zhí)行完畢 if (scroller.computeScrollOffset()){ View view= (View) getParent(); //獲得當(dāng)前的滑動(dòng)坐標(biāo) view.scrollTo(scroller.getCurrX(),scroller.getCurrY()); //通過(guò)重繪來(lái)不斷調(diào)用computeScroll invalidate(); //invalidate()--->draw()---->computeScroll() } }
case MotionEvent.ACTION_UP: //手指離開時(shí),執(zhí)行滑動(dòng)過(guò)程 View viewGroup= (View) getParent(); scroller.startScroll( viewGroup.getScrollX(), viewGroup.getScrollY(), -viewGroup.getScrollX(), -viewGroup.getScrollY(),500); invalidate(); break;
6.屬性動(dòng)畫
7.ViewDragHelper類
public class DrawGroup extends FrameLayout { private ViewDragHelper helper; private View mainView,menuView; public DrawGroup(@NonNull Context context) { super(context); inView(); } public DrawGroup(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); inView(); } public DrawGroup(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); inView(); } private void inView(){ helper=ViewDragHelper.create(this, new ViewDragHelper.Callback() { @Override public boolean tryCaptureView(@NonNull View child, int pointerId) { //如果當(dāng)前觸摸的child是mainView時(shí)開始檢測(cè) return child==mainView; } @Override public int clampViewPositionHorizontal(@NonNull View child, int left, int dx) { //水平方向上的滑動(dòng) return left; } @Override public int clampViewPositionVertical(@NonNull View child, int top, int dy) { //垂直方向上的滑動(dòng) return 0; } @Override public void onViewReleased(@NonNull View releasedChild, float xvel, float yvel) { //拖動(dòng)結(jié)束后調(diào)用 super.onViewReleased(releasedChild, xvel, yvel); //手指抬起后緩慢移動(dòng)到指定位置 if (mainView.getLeft()<300){ //關(guān)閉菜單 helper.smoothSlideViewTo(mainView,0,0); //相當(dāng)于scroller的startScroll方法 }else { //打開菜單 helper.smoothSlideViewTo(mainView,300,0); } ViewCompat.postInvalidateOnAnimation(DrawGroup.this); } }); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return helper.shouldInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { //將觸摸事件傳遞給ViewDragHelper,此操作必不可少 helper.processTouchEvent(event); return true; } @Override public void computeScroll() { if (helper.continueSettling(true)){ ViewCompat.postInvalidateOnAnimation(this); } } @Override protected void onFinishInflate() { super.onFinishInflate(); //加載完布局調(diào)用 menuView=getChildAt(0); mainView=getChildAt(1); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); }} onViewCaptured():在用戶觸摸到View后回調(diào) onViewDragStateChanged():在拖拽狀態(tài)改變時(shí)回調(diào)(idle,dragging…) onViewPositionChanged():在位置改變時(shí)回調(diào),常用于滑動(dòng)時(shí)更改scale進(jìn)行縮放等效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)2. asp.net core 認(rèn)證和授權(quán)實(shí)例詳解3. Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件列表展示(二)4. XML在語(yǔ)音合成中的應(yīng)用5. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁(yè)6. 基于PHP做個(gè)圖片防盜鏈7. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車輛管理系統(tǒng)8. ASP.NET MVC把數(shù)據(jù)庫(kù)中枚舉項(xiàng)的數(shù)字轉(zhuǎn)換成文字9. jscript與vbscript 操作XML元素屬性的代碼10. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)
