您的位置:首页 > 移动开发 > Android开发

android viewdraghelper使用

2015-06-19 15:17 393 查看
viewdraghelper是v4包提供的一个为了简化手势操作的类,下面是一个slidermenu的例子来展示viewdraghelper的一些基本使用方法。

package com.example.test;

import android.annotation.SuppressLint;

import android.content.Context;

import android.support.v4.view.ViewCompat;

import android.support.v4.widget.ViewDragHelper;

import android.util.AttributeSet;

import android.util.DisplayMetrics;

import android.view.MotionEvent;

import android.view.View;

import android.view.ViewGroup;

/**

 * User: cb Date: 2015-06-19 Time: 10:26

 */

@SuppressLint("NewApi")

public class SliderMenuLayout extends ViewGroup {

    private ViewDragHelper viewDragHelper;

    private View menuView;

    private View contentView;

    private int menuWidth = 500;

    private DisplayMetrics dm;

    private boolean isOpen;

    public void setMenuWidth(int menuWidth) {

        this.menuWidth = menuWidth;

    }

    public SliderMenuLayout(Context context) {

        super(context);

        setWillNotDraw(false);

    }

    public SliderMenuLayout(Context context, AttributeSet attrs) {

        super(context, attrs);

        setWillNotDraw(false);

    }

    public SliderMenuLayout(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

        setWillNotDraw(false);

    }

    @Override

    protected void onFinishInflate() {

        super.onFinishInflate();

        dm = getContext().getResources().getDisplayMetrics();

        menuView = findViewById(R.id.menu);

        LayoutParams params = menuView.getLayoutParams();

        params.width = menuWidth;

        params.height = dm.heightPixels;

        contentView = findViewById(R.id.content);

        LayoutParams params2 = contentView.getLayoutParams();

        params2.width = dm.widthPixels;

        params2.height = dm.heightPixels;

        viewDragHelper = ViewDragHelper.create(this, 1.0f, new DragCallback());

    }

    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        measureChild(menuView, MeasureSpec.EXACTLY, MeasureSpec.EXACTLY);

        measureChild(contentView, MeasureSpec.EXACTLY, MeasureSpec.EXACTLY);

    }

    @Override

    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {

        contentView.layout(0, 0, contentView.getMeasuredWidth(), contentView.getMeasuredHeight());

        menuView.layout(-menuWidth, 0, 0, menuView.getMeasuredHeight());

    }

    @Override

    public boolean onInterceptTouchEvent(MotionEvent ev) {

        return viewDragHelper.shouldInterceptTouchEvent(ev);

    }

    @Override

    public void computeScroll() {

        super.computeScroll();

        if (viewDragHelper.continueSettling(true)) {

            ViewCompat.postInvalidateOnAnimation(this);

        }

    }

    @Override

    public boolean onTouchEvent(MotionEvent event) {

        viewDragHelper.processTouchEvent(event);

        return true;

    }

    // call back

    public class DragCallback extends ViewDragHelper.Callback {

        @Override

        public boolean tryCaptureView(View view, int i) {

            return true;

        }

        @Override

        public int clampViewPositionHorizontal(View child, int left, int dx) {

            if (child == contentView) {

                if (left > menuWidth) {

                    return menuWidth;

                } else if (left < 0) {

                    return 0;

                }

            } else if (child == menuView) {

                if (left > 0) {

                    return 0;

                } else if (left < -menuWidth) {

                    return -menuWidth;

                }

            }

            return left;

        }

        @Override

        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {

            if (changedView == contentView) {

                menuView.layout(-menuWidth + left, 0, left, menuView.getMeasuredHeight());

            } else if (changedView == menuView) {

                contentView.setLeft(contentView.getLeft() + dx);

                

            }

        }

        @Override

        public void onViewReleased(View releasedChild, float xvel, float yvel) {

            super.onViewReleased(releasedChild, xvel, yvel);

            if (yvel > 0) {

                isOpen = false;

                viewDragHelper.settleCapturedViewAt(releasedChild == contentView ? 0 : -menuWidth, 0);

            } else {

                isOpen = true;

                viewDragHelper.settleCapturedViewAt(releasedChild == contentView ? menuWidth : 0, 0);

            }

            invalidate();

        }

    }

    public void toggle() {

        viewDragHelper.smoothSlideViewTo(menuView, isOpen ? -menuWidth : 0, 0);

        ViewCompat.postInvalidateOnAnimation(this);

        isOpen = !isOpen;

    }

}
-------------------华丽分割线--------------------

xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <Button

        android:id="@+id/open"

        android:layout_width="match_parent"

        android:layout_height="30dip" />

    <com.example.test.SliderMenuLayout

        android:id="@+id/slider"

        android:layout_width="match_parent"

        android:layout_height="match_parent" >

        <LinearLayout

            android:id="@+id/content"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:background="#541231"

            android:orientation="horizontal" >

            <TextView

                android:layout_width="0dip"

                android:layout_height="30dip"

                android:layout_weight="1"

                android:text="sdsdsdsdsd" />

            <TextView

                android:layout_width="0dip"

                android:layout_height="30dip"

                android:layout_weight="1"

                android:text="只能爽肤水的沙发沙发沙发沙发上" />

        </LinearLayout>

        <LinearLayout

            android:id="@+id/menu"

            android:layout_width="200dip"

            android:layout_height="match_parent"

            android:background="#ffff12"

            android:orientation="horizontal" >

            <TextView

                android:layout_width="0dip"

                android:layout_height="30dip"

                android:layout_weight="1"

                android:text="sdsdsdsdsd" />

            <TextView

                android:layout_width="0dip"

                android:layout_height="30dip"

                android:layout_weight="1"

                android:text="只能爽肤水的沙发沙发沙发沙发上" />

        </LinearLayout>

    </com.example.test.SliderMenuLayout>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: