android 全局下拉view 仿手机下拉通知栏

baseActivity

package com.app.cp1.globalpullview;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class BaseActivity extends AppCompatActivity {

    RelativeLayout relativeLayout;
    GlobalTopView globalTopView;
    int downX = 0;
    int downY = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        relativeLayout = new RelativeLayout(this);

        globalTopView = (GlobalTopView) LayoutInflater.from(this).inflate(R.layout.topview,null);
        globalTopView.setY(-MyApplication.screenH);
    }

    @Override
    public void setContentView(int layoutResID) {
        ViewGroup view = (ViewGroup) LayoutInflater.from(this).inflate(layoutResID, relativeLayout, true);
        view.addView(globalTopView);

        ViewGroup.LayoutParams layoutParams = globalTopView.getLayoutParams();
        layoutParams.height = MyApplication.screenH;
        layoutParams.width = MyApplication.screenW;
        globalTopView.setLayoutParams(layoutParams);

        super.setContentView(view);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (!MyApplication.isTopShowing) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    downX = (int) event.getX();
                    downY = (int) event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (downX > 200 && downX < MyApplication.screenW - 200 && downY < MyApplication.dip2px(this, 100)) {
                        int nowY = (int) event.getY();
                        int moveY = nowY - downY;
                        int scrollToY = -globalTopView.getHeight() + moveY;
                        globalTopView.setY(scrollToY);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if (!MyApplication.isTopShowing && downX > 200 && downX < MyApplication.screenW - 200 && downY < MyApplication.dip2px(this, 100)) {
                        if (Math.abs(globalTopView.getY()) > MyApplication.screenH * 2/ 3) {
                            up(globalTopView);
                        } else {
                            down(globalTopView);
                        }
                    }
                    break;
            }
        }
        return super.dispatchTouchEvent(event);
    }

    public void up(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", globalTopView.getY(), -MyApplication.screenH);
        animator.setDuration(300);
        animator.start();
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                MyApplication.isTopShowing = false;
            }
        });

    }

    public void down(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", globalTopView.getY(), 0);
        animator.setDuration(300);
        animator.start();
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                MyApplication.isTopShowing = true;
            }
        });
    }
}

GlobalTopView

package com.app.cp1.globalpullview;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;


public class GlobalTopView extends FrameLayout {

    public int downY = 0;

    public GlobalTopView(Context context) {
        super(context);
    }

    public GlobalTopView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflate(context,R.layout.global_top_view,this);
    }

    public GlobalTopView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context,R.layout.global_top_view,this);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
//        LayoutInflater.from(getContext()).inflate(R.layout.global_top_view,this,true);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downY = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                int nowY = (int) event.getRawY();
                int moveY = nowY - downY;
                setY(moveY);
                break;
            case MotionEvent.ACTION_UP:
                if (Math.abs(event.getRawY()) < MyApplication.screenH * 3/ 4) {
                    up(this);
                } else {
                    down(this);
                }
                break;
        }
        return true;
    }

    public void up(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", view.getY(), -MyApplication.screenH);
        animator.setDuration(300);
        animator.start();
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                MyApplication.isTopShowing = false;
            }
        });
    }

    public void down(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", view.getY(), 0);
        animator.setDuration(300);
        animator.start();
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                MyApplication.isTopShowing = true;
            }
        });
    }
}

MyApplication

package com.app.cp1.globalpullview;


import android.app.Application;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class MyApplication extends Application {

    public static int screenH;
    public static int screenW;
    public static boolean isTopShowing = false;

    @Override
    public void onCreate() {
        super.onCreate();
        screenH = getWindowPixle(this)[1];
        screenW = getWindowPixle(this)[0];

    }

    /**
     * 获取手机长宽像素
     *
     * @return int[0] = 宽 int[1] = 高
     */
    public static int[] getWindowPixle(Context context) {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager manager = (WindowManager)
                context.getSystemService(Context.WINDOW_SERVICE);
        manager.getDefaultDisplay().getMetrics(metrics);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        return new int[]{width, height};
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}

activity_base.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.app.cp1.globalpullview.BaseActivity">

</android.support.constraint.ConstraintLayout>

global_top_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#A8FF24">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="25sp"
        android:text="我是全局顶部布局"/>
</LinearLayout>

top_view

<?xml version="1.0" encoding="utf-8"?>
<com.app.cp1.globalpullview.GlobalTopView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</com.app.cp1.globalpullview.GlobalTopView>

新建Activity 直接继承BaseActiviy即可

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值