Repository navigation
Expand file tree
/
Copy pathMaskView.java
More file actions
180 lines (163 loc) · 4.59 KB
/
Copy pathMaskView.java
File metadata and controls
180 lines (163 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.azhon.lightmode.view;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
/**
* 项目名: LightMode
* 包名 com.azhon.lightmode
* 文件名: MaskView
* 创建时间: 2018/1/2 on 23:14
* 描述: TODO 模仿网页观看视频,关灯效果;需要在完全显示在屏幕上时,添加才有效
*
* @author 阿钟
*/
public class MaskView extends View {
/**
* activity 根视图
*/
private ViewGroup decorView;
/**
* 不加遮罩的视图
*/
private View brightView;
/**
* 遮罩画笔
*/
private Paint paint;
/**
* 不遮罩的view
*/
private RectF brightRectF;
/**
* 遮罩的路径
*/
private Path path;
/**
* 是否已经添加了
*/
private boolean showing = false;
public MaskView(Context context) {
super(context);
init(context);
}
private void init(Context context) {
//获取activity顶层视图
decorView = (ViewGroup) ((Activity) context).getWindow().getDecorView();
//初始化蒙版视图,充满全屏幕
setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
paint = new Paint();
//设置为黑色 在加点透明度
paint.setColor(Color.argb(230, 0, 0, 0));
path = new Path();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (decorView == null || brightView == null) {
return;
}
//路径恢复
path.reset();
//A
path.moveTo(brightRectF.left, brightRectF.top);
//B
path.lineTo(brightRectF.right, brightRectF.top);
//C
path.lineTo(brightRectF.right, brightRectF.bottom);
//D
path.lineTo(brightRectF.left, brightRectF.bottom);
//A
path.lineTo(brightRectF.left, brightRectF.top);
//E
path.lineTo(0, brightRectF.top);
//F
path.lineTo(0, decorView.getHeight());
//F
path.lineTo(decorView.getWidth(), decorView.getHeight());
//H
path.lineTo(decorView.getWidth(), 0);
//I
path.lineTo(0, 0);
//E
path.lineTo(0, brightRectF.top);
//A
path.lineTo(brightRectF.left, brightRectF.top);
//闭合曲线
path.close();
canvas.drawPath(path, paint);
}
/**
* 添加不加蒙版的View
*
* @param view 视图
*/
public void attachView(View view) {
if (!showing) {
this.brightView = view;
//没有添加,才可以添加
initMask();
}
}
/**
* 移除遮罩
*/
public void removeMask() {
if (!showing) {
//没有添加
return;
}
//使用动画消失
AlphaAnimation animation = new AlphaAnimation(1f, 0f);
animation.setDuration(500);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
ViewParent parent = MaskView.this.getParent();
if (parent != null && parent instanceof ViewGroup) {
((ViewGroup) parent).removeView(MaskView.this);
showing = false;
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
startAnimation(animation);
}
/**
* 初始化不加遮罩view的位置
*/
private void initMask() {
int[] location = new int[2];
//获取view在屏幕上的坐标
brightView.getLocationOnScreen(location);
brightRectF = new RectF(location[0], location[1], location[0] + brightView.getWidth(),
location[1] + brightView.getHeight());
//添加蒙版到Activity之上
decorView.addView(this);
invalidate();
showing = true;
}
/**
* 是否已经添加
*
* @return
*/
public boolean isShowing() {
return showing;
}
}