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

Android實(shí)現(xiàn)滑動(dòng)效果

 更新時(shí)間:2020年09月24日 17:31:07   作者:黎明前的硝煙  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)滑動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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:
 //手指離開(kāi)時(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í)開(kāi)始檢測(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 {
 //打開(kāi)菜單
 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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論