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;
public class NotificationView extends RelativeLayout {
TextView tvTitle;
TextView tvContent;
TextView tvTime;
int originX;
int originY;
int pressX;
int previousMoveX;
int afterX;
int upX;
int moveX = 0;
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();
previousMoveX = pressX;
break;
case MotionEvent.ACTION_MOVE:
afterX = (int) event.getRawX();
moveX = afterX - previousMoveX;
moveViewByScroll(moveX);
previousMoveX = afterX;
break;
case MotionEvent.ACTION_UP:
upX = (int) event.getRawX();
if ((upX - pressX) > 450) {
setVisibility(GONE);
} else {
scrollViewTo(originX, originY);
}
break;
}
return super.onTouchEvent(event);
}
private void moveView(int moveX) {
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) getLayoutParams();
int marginLeft = layoutParams.leftMargin;
layoutParams.leftMargin = marginLeft + moveX;
setLayoutParams(layoutParams);
}
private void moveViewByScroll(int moveX) {
scrollBy(-moveX, 0);
}
private void scrollViewTo(int x, int y) {
scrollTo(x, y);
}
public void setTitle(String title) {
tvTitle.setText(title);
}
public void setTime(String time) {
tvTime.setText(time);
}
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>