Android手势下拉抽屉效果

时间: 2023-07-11 admin IT培训

Android手势下拉抽屉效果

Android手势下拉抽屉效果

 效果

手势拖动底部滑动上面,图二中拖动下拉

图二

Activity实现

package me.maxd.demo.activity;import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;import me.maxd.demo.R;/*** 处理lin和手势拖动的实现 设置最小回退位置 最大回退位置 设置速度 手势速度*/
public class TestActivity extends AppCompatActivity implements View.OnTouchListener, GestureDetector.OnGestureListener {//滚动布局LinearLayout show;//滑动按钮Button btn;//动态改变布局用private RelativeLayout.LayoutParams linearParams;//配合ontouch使用,手势监听private GestureDetector mGestureDetector;int phone_high;//屏幕的高度int stop_high;//需要停留的位置int small_high = 260;//最小位置int speed = 50;//自动展开或收回的滑动速率,默认为50,大小自己设,越大越快//设定定时器用来自动弹回和弹出Handler myHanlder = new Handler();Runnable run = new Runnable() {@SuppressLint("ClickableViewAccessibility")@Overridepublic void run() {//向上收回if (stop_high == small_high) {if (linearParams.height > stop_high) {//速率自己调linearParams.height = linearParams.height - speed;show.setLayoutParams(linearParams); // 使设置好的布局参数应用到控件// 递归myHanlder.postDelayed(run, 1);}//向下展开} else if (stop_high == phone_high) {if (linearParams.height < stop_high) {linearParams.height = linearParams.height + speed;show.setLayoutParams(linearParams); // 使设置好的布局参数应用到控件// 递归myHanlder.postDelayed(run, 1);}}}};@SuppressLint("ClickableViewAccessibility")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_test);show = (LinearLayout) findViewById(R.id.show);btn = (Button) findViewById(R.id.btn);// 取控件当前的布局参数linearParams = (RelativeLayout.LayoutParams) show.getLayoutParams();btn.setOnTouchListener(this);mGestureDetector = new GestureDetector(this, this);mGestureDetector.setIsLongpressEnabled(true);//获取屏幕的高度phone_high = getMetrics().heightPixels;}@Overridepublic boolean onTouch(View v, MotionEvent event) {//触摸返回值int action = event.getAction();float y = event.getY();switch (action) {//开始触摸case MotionEvent.ACTION_DOWN://开始触摸时候返回的坐标//取消计时器myHanlder.removeCallbacks(run);break;//触摸移动case MotionEvent.ACTION_MOVE://动态设定布局的高度为手势移动的高度if (event.getRawY() >= small_high) {linearParams.height = (int) event.getRawY();show.setLayoutParams(linearParams);}break;//终止触摸case MotionEvent.ACTION_UP:break;}return mGestureDetector.onTouchEvent(event);}//获取屏幕高度的方法private DisplayMetrics getMetrics() {WindowManager wm = getWindowManager();DisplayMetrics outMetrics = new DisplayMetrics();wm.getDefaultDisplay().getMetrics(outMetrics);return outMetrics;}//以下为手势监听,配合ontouch方法使用//滑动的距离和速率。为了判断手势final int FLING_MIN_DISTANCE = 10, FLING_MIN_VELOCITY = 100;@Overridepublic boolean onDown(MotionEvent e) {return false;}@Overridepublic void onShowPress(MotionEvent e) {}@Overridepublic boolean onSingleTapUp(MotionEvent e) {return false;}@Overridepublic boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {return false;}@Overridepublic void onLongPress(MotionEvent e) {}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {//判断当滑动距离大于50并且速率大于200,即自动收回,向上滑动if (e1.getRawY() - e2.getRawY() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {//停止的高度为50,就是抽屉的最后的停留位置stop_high = small_high;myHanlder.post(run);//判断当滑动距离小于50并且速率大于200,即自动展开,向下滑动} else if (e1.getRawY() - e2.getRawY() < -FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {//停止的高度即屏幕高度,就是抽屉的最后的停留位置stop_high = phone_high;myHanlder.post(run);}return false;}
}

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="@dimen/dp_400"><LinearLayoutandroid:id="@+id/show"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/wifi_bg"android:overScrollMode="never"android:scrollbars="none"><LinearLayoutandroid:id="@+id/info"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:visibility="gone"android:id="@+id/test1"android:layout_width="match_parent"android:layout_height="@dimen/dp_80"android:gravity="center"android:textColor="#ffffff"android:textSize="18sp" /><TextViewandroid:visibility="gone"android:id="@+id/test2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="@dimen/dp_170"android:gravity="center_horizontal"android:scaleType="fitXY"android:textColor="#ffffff"android:textSize="18sp" /></LinearLayout></LinearLayout><Buttonandroid:id="@+id/btn"android:layout_width="match_parent"android:layout_height="@dimen/dp_60"android:layout_alignBottom="@+id/show"android:layout_alignParentStart="true"android:background="@color/transparent" /></RelativeLayout>

图片