-
Notifications
You must be signed in to change notification settings - Fork 2
/
OverTheTopLayer.java
187 lines (138 loc) · 5.48 KB
/
OverTheTopLayer.java
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
181
182
183
184
185
186
187
package live.videosdk.android.hlsdemo.common.reactions;
import android.app.Activity;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import java.lang.ref.WeakReference;
public class OverTheTopLayer {
public static class OverTheTopLayerException extends RuntimeException {
public OverTheTopLayerException(String msg) {
super(msg);
}
}
private WeakReference<Activity> mWeakActivity;
private WeakReference<ViewGroup> mWeakRootView;
private FrameLayout mCreatedOttLayer;
private float mScalingFactor = 1.0f;
private int[] mDrawLocation = {0, 0};
private Bitmap mBitmap;
public OverTheTopLayer() {
}
/**
* To create a layer on the top of activity
*/
public OverTheTopLayer with(Activity weakReferenceActivity) {
mWeakActivity = new WeakReference<Activity>(weakReferenceActivity);
return this;
}
public OverTheTopLayer setBitmap(Bitmap bitmap, int[] location) {
if (location == null) {
location = new int[]{0, 0};
} else if (location.length != 2) {
throw new OverTheTopLayerException("Requires location as an array of length 2 - [x,y]");
}
this.mBitmap = bitmap;
this.mDrawLocation = location;
return this;
}
/**
* Holds the scaling factor for the image.
*/
public OverTheTopLayer scale(float scale) {
if (scale <= 0) {
throw new OverTheTopLayerException("Scaling should be > 0");
}
this.mScalingFactor = scale;
return this;
}
/**
* Attach the OTT layer as the child of the given root view.
*/
public OverTheTopLayer attachTo(ViewGroup rootView) {
this.mWeakRootView = new WeakReference<ViewGroup>(rootView);
return this;
}
/**
* Creates an OTT.
*/
public FrameLayout create() {
if(mCreatedOttLayer != null) {
destroy();
}
if (mWeakActivity == null) {
throw new OverTheTopLayerException("Could not create the layer as not activity reference was provided.");
}
Activity activity = mWeakActivity.get();
if (activity != null) {
ViewGroup attachingView = null;
if (mWeakRootView != null && mWeakRootView.get() != null) {
attachingView = mWeakRootView.get();
} else {
attachingView = (ViewGroup) activity.findViewById(android.R.id.content);
}
ImageView imageView = new ImageView(activity);
imageView.setImageBitmap(mBitmap);
int minWidth = mBitmap.getWidth();
int minHeight = mBitmap.getHeight();
imageView.measure(View.MeasureSpec.makeMeasureSpec(minWidth, View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(minHeight, View.MeasureSpec.AT_MOST));
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) imageView.getLayoutParams();
if (params == null) {
params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.TOP);
imageView.setLayoutParams(params);
}
int xPosition = mDrawLocation[0];
int yPosition = mDrawLocation[1];
params.width = minWidth;
params.height = minHeight;
params.leftMargin = xPosition;
params.topMargin = yPosition;
imageView.setLayoutParams(params);
FrameLayout ottLayer = new FrameLayout(activity);
FrameLayout.LayoutParams topLayerParam = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT, Gravity.TOP);
ottLayer.setLayoutParams(topLayerParam);
ottLayer.addView(imageView);
attachingView.addView(ottLayer);
mCreatedOttLayer = ottLayer;
} else {
Log.e(OverTheTopLayer.class.getSimpleName(), "Could not create the layer. Reference to the activity was lost");
}
return mCreatedOttLayer;
}
/**
* Kills the OTT
*/
public void destroy() {
if (mWeakActivity == null) {
throw new OverTheTopLayerException("Could not create the layer as not activity reference was provided.");
}
Activity activity = mWeakActivity.get();
if (activity != null) {
ViewGroup attachingView = null;
if (mWeakRootView != null && mWeakRootView.get() != null) {
attachingView = mWeakRootView.get();
} else {
attachingView = (ViewGroup) activity.findViewById(android.R.id.content);
}
if (mCreatedOttLayer != null) {
attachingView.removeView(mCreatedOttLayer);
mCreatedOttLayer = null;
}
} else {
Log.e(OverTheTopLayer.class.getSimpleName(), "Could not destroy the layer as the layer was never created.");
}
}
/**
* Applies the animation to the image view present in OTT.
*/
public void applyAnimation(Animation animation) {
if(mCreatedOttLayer != null) {
ImageView drawnImageView = (ImageView) mCreatedOttLayer.getChildAt(0);
drawnImageView.startAnimation(animation);
}
}
}