自定义view------横幅通知(滑动超过一定距离才会消失)

package com.les.study.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.les.study.R;

/**
 * 自定义通知view
 * Created by liangenshuo on 2020/8/18
 */
public class NotificationView extends RelativeLayout {
     

    TextView tvTitle;//题目
    TextView tvContent;//内容
    TextView tvTime;//时间

    int originX;//初始的x坐标(左上角)
    int originY;//初始的y坐标(左上角)

    int pressX;//手指按下的x坐标

    int previousMoveX;//手指滑动上一次的x坐标

    int afterX;//移动后的x坐标

    int upX;//手指抬起时的x坐标

    int moveX = 0;//在x方向上移动的距离

    public NotificationView(Context context) {
     
        super(context);
        initView(context);
    }

    public NotificationView(Context context, AttributeSet attrs) {
     
        super(context, attrs);
        initView(context);
    }

    public NotificationView(Context context, AttributeSet attrs, int defStyleAttr) {
     
        super(context, attrs, defStyleAttr);
    }

    private void initView(Context context) {
     
        View view = LayoutInflater.from(context).inflate(R.layout.notification_view, this);
        tvTitle = view.findViewById(R.id.tv_title);
        tvContent = view.findViewById(R.id.tv_content);
        tvTime = view.findViewById(R.id.tv_time);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
     

        originX = ((FrameLayout.LayoutParams)getLayoutParams()).leftMargin;
        originY = ((FrameLayout.LayoutParams)getLayoutParams()).topMargin;
        switch (event.getAction()) {
     
            case MotionEvent.ACTION_DOWN:
                pressX = (int) event.getRawX();//记录按下时的x坐标
                previousMoveX = pressX;//初始化移动前的x坐标为按下时的坐标
                break;
            case MotionEvent.ACTION_MOVE:
                afterX = (int) event.getRawX();//记录移动后的x坐标
                moveX = afterX - previousMoveX;//记录移动的距离
                moveViewByScroll(moveX);
                previousMoveX = afterX;//赋值上次移动后的x坐标
                break;
            case MotionEvent.ACTION_UP:
                upX = (int) event.getRawX();
                if ((upX - pressX) > 450) {
     
                    //超过一定的移动范围再消失
                    setVisibility(GONE);
                } else {
     
                    //移动未超过一定的范围则弹回去
                    scrollViewTo(originX, originY);
                }
                break;
        }
        return super.onTouchEvent(event);
    }

    /**
     * 移动view
     *
     * @param moveX x方向的移动距离
     */
    private void moveView(int moveX) {
     
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) getLayoutParams();
        int marginLeft = layoutParams.leftMargin;//初始的marginleft
        layoutParams.leftMargin = marginLeft + moveX;
        setLayoutParams(layoutParams);
    }

    /**
     * 移动view
     *
     * @param moveX 移动的距离
     */
    private void moveViewByScroll(int moveX) {
     
        scrollBy(-moveX, 0);
    }

    /**
     * 移动view到指定位置
     *
     * @param x 移动后的x坐标
     * @param y 移动后的y坐标
     */
    private void scrollViewTo(int x, int y) {
     
        scrollTo(x, y);
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
     
        tvTitle.setText(title);
    }

    /**
     * 设置时间
     *
     * @param time 时间
     */
    public void setTime(String time) {
     
        tvTime.setText(time);
    }

    /**
     * 设置内容
     *
     * @param content
     */
    public void setContent(String content) {
     
        tvContent.setText(content);
    }

}


```java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginTop="15dp"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:background="@drawable/bg_notification_view">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="人脸告警"
        android:textColor="#F000"
        android:textSize="20sp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/tv_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2020-08-18 15:56"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/tv_title"
        android:layout_marginRight="10dp"/>

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="检测到可疑人员"
        android:layout_alignLeft="@id/tv_title"
        android:layout_marginBottom="10dp"
        android:textColor="#F000"
        android:textSize="18sp"/>

</RelativeLayout>

你可能感兴趣的:(android)