android---Scroll实现滑动效果

实现滑动的方法

package lzl.edu.com.scrolldemos;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Scroller;

public class MainActivity extends AppCompatActivity {
    private ImageView mImageView;
    private int lastX;
    private int lastY;
    private Scroller mScroll;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }
    private void initView(){
        mScroll = new Scroller(this);
        mImageView = (ImageView) findViewById(R.id.mImageView);
       /* mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent(MainActivity.this, SlidScrollActivity.class);
                startActivity(mIntent);
            }
        });*/
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                //记录触摸到坐标
                lastX = x;
                lastY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                //记录偏移量坐标
                int offsetX = x - lastX;
                int offsetY = y - lastY;
                //在当前的left top right bottom 上加上偏移量
               /* 方法一:
               mImageView.layout(mImageView.getLeft()+offsetX,mImageView.getTop()+offsetY,
                        mImageView.getRight()+offsetX,mImageView.getBottom()+offsetY);*/
                Log.i("ACTION_MOVE","----x "+offsetX+"---y "+offsetY);
                /*
                方法二:
                mImageView.offsetLeftAndRight(offsetX);
                mImageView.offsetTopAndBottom(offsetY);
                */
                /*
                方法三:
                ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) mImageView.getLayoutParams();
                layoutParams.leftMargin += offsetX;
                layoutParams.topMargin += offsetY;
                mImageView.setLayoutParams(layoutParams);
                */
              /*  方法四:
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mImageView.getLayoutParams();
                Log.i("params"," left:"+mImageView.getLeft()+"  top:"+mImageView.getTop());
                layoutParams.leftMargin += offsetX;
                layoutParams.topMargin += offsetY;
                mImageView.setLayoutParams(layoutParams);*/
                /*方法五
                这个需要使用View所在的ViewGroup中来使用scrollBy方法
                ((View)mImageView.getParent()).scrollBy(-offsetX,-offsetY);
                */
                //重新设置初始坐标
                lastX = x;
                lastY = y;
                break;
        }
        return super.onTouchEvent(event);
    }

}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="lzl.edu.com.scrolldemos.MainActivity">

    <ImageView
        android:id="@+id/mImageView"
        android:src="@mipmap/ic_launcher"
        android:layout_width="100dp"
        android:layout_height="100dp"
         />
</RelativeLayout>

用ViewDragHelper实现侧滑菜单

MyDragLayout.java

package lzl.edu.com.scrolldemos.view;

import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

/**
 * Created by admin on 2015/12/1.
 * 利用ViewDragHelper类来实现类似QQ侧滑的功能。这里只实现功能,并没有优化界面
 * 1、初始化ViewDragHelper
 * 2、拦截事件
 * 3、处理computeScroll
 * 4、处理回调CallBack 实现里面的方法
 * 
 */
public class MyViewDrag extends FrameLayout {
    private ViewDragHelper mViewDragHelper;
    private LinearLayout mLinearLeft,mLinearMain;
    private int mWidth;
    //处理回调
    private ViewDragHelper.Callback callback = new ViewDragHelper.Callback(){
        //
        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            //当触摸到主界面时,开始检测
            return mLinearMain == child;
        }
        //向左滑动
        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            return left;
        }
        //向下滑动,这里显示为0
        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {
            return 0;
        }
        //拖动结束时调用
        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            super.onViewReleased(releasedChild, xvel, yvel);
            if(mLinearMain.getLeft()<500){
                //当现做滑动小于500时,回到指定位置
                mViewDragHelper.smoothSlideViewTo(mLinearMain,0,0);
                ViewCompat.postInvalidateOnAnimation(MyViewDrag.this);
            }else {
                //打开左滑菜单
                mViewDragHelper.smoothSlideViewTo(mLinearMain,300,0);
                ViewCompat.postInvalidateOnAnimation(MyViewDrag.this);
            }
        }
    };
    public MyViewDrag(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public MyViewDrag(Context context) {
        this(context,null);
    }

    public MyViewDrag(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    /**
     * 初始化布局
     */
    private void initView(){
        mViewDragHelper = ViewDragHelper.create(this,callback);
    }
    //拦截事件
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return mViewDragHelper.shouldInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mViewDragHelper.processTouchEvent(event);
        return true;
    }

    //加载完布局后调用
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mLinearLeft = (LinearLayout) getChildAt(0);
        mLinearMain = (LinearLayout) getChildAt(1);
    }

    @Override
    public void computeScroll() {
        if(mViewDragHelper.continueSettling(true)){
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = mLinearLeft.getWidth();
    }
}

官方提供的DrawerLayout使用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="lzl.edu.com.scrolldemos.activity.DrawerActivity">
    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!-- The main content view -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:background="#fff000"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <!-- The navigation drawer -->
        <ListView android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#111"/>
    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

完整例子下载



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值