admin
2024-01-26 c2d382d99ca506932985d1843d4371d6ed0203ff
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package com.lcjian.library.widget;
 
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
 
/**
 * A simple text label view that can be applied as a "refresh" to any given {@link android.view.View}. 
 * This class is intended to be instantiated at runtime rather than included in XML layouts.
 * 
 * @author LCJIAN
 */
public class RefreshView {
 
    public static final int POSITION_TOP_LEFT = 1;
    public static final int POSITION_TOP_RIGHT = 2;
    public static final int POSITION_BOTTOM_LEFT = 3;
    public static final int POSITION_BOTTOM_RIGHT = 4;
    public static final int POSITION_CENTER = 5;
    
    private static final int DEFAULT_MARGIN_DIP = 5;
    private static final int DEFAULT_POSITION = POSITION_CENTER;
    
    private static Animation fadeIn;
    private static Animation fadeOut;
    
    private View refresh;
    private View target;
    
    private int refreshPosition;
    private int refreshMarginH;
    private int refreshMarginV;
    
    public RefreshView(View target) {
        init(new ProgressBar(target.getContext()), target);
    }
    
    public RefreshView(View refresh, View target) {
        init(refresh, target);
    }
 
    private void init(View refresh, View target) {
        
        this.refresh = refresh;
        this.target = target;
        
        // apply defaults
        refreshPosition = DEFAULT_POSITION;
        refreshMarginH = dipToPixels(DEFAULT_MARGIN_DIP);
        refreshMarginV = refreshMarginH;
        
        fadeIn = new AlphaAnimation(0, 1);
        fadeIn.setInterpolator(new DecelerateInterpolator());
        fadeIn.setDuration(200);
 
        fadeOut = new AlphaAnimation(1, 0);
        fadeOut.setInterpolator(new AccelerateInterpolator());
        fadeOut.setDuration(200);
        
        if (this.target != null) {
            applyTo(this.target);
        } else {
            show();
        }
    }
 
    private void applyTo(View target) {
        LayoutParams lp = target.getLayoutParams();
        ViewParent parent = target.getParent();
        FrameLayout container = new FrameLayout(target.getContext());
        
        ViewGroup group = (ViewGroup) parent; 
        int index = group.indexOfChild(target);
        
        group.removeView(target);
        group.addView(container, index, lp);
        
        container.addView(target);
 
        refresh.setVisibility(View.GONE);
        container.addView(refresh);
        
        group.invalidate();
    }
    
    /**
     * Make the refresh visible in the UI.
     * 
     */
    public void show() {
        show(false, null);
    }
    
    /**
     * Make the refresh visible in the UI.
     *
     * @param animate flag to apply the default fade-in animation.
     */
    public void show(boolean animate) {
        show(animate, fadeIn);
    }
    
    /**
     * Make the refresh visible in the UI.
     *
     * @param anim Animation to apply to the view when made visible.
     */
    public void show(Animation anim) {
        show(true, anim);
    }
    
    /**
     * Make the refresh non-visible in the UI.
     * 
     */
    public void hide() {
        hide(false, null);
    }
    
    /**
     * Make the refresh non-visible in the UI.
     *
     * @param animate flag to apply the default fade-out animation.
     */
    public void hide(boolean animate) {
        hide(animate, fadeOut);
    }
    
    /**
     * Make the refresh non-visible in the UI.
     *
     * @param anim Animation to apply to the view when made non-visible.
     */
    public void hide(Animation anim) {
        hide(true, anim);
    }
    
    /**
     * Toggle the refresh visibility in the UI.
     * 
     */
    public void toggle() {
        toggle(false, null, null);
    }
    
    /**
     * Toggle the refresh visibility in the UI.
     * 
     * @param animate flag to apply the default fade-in/out animation.
     */
    public void toggle(boolean animate) {
        toggle(animate, fadeIn, fadeOut);
    }
    
    /**
     * Toggle the refresh visibility in the UI.
     *
     * @param animIn Animation to apply to the view when made visible.
     * @param animOut Animation to apply to the view when made non-visible.
     */
    public void toggle(Animation animIn, Animation animOut) {
        toggle(true, animIn, animOut);
    }
    
    private void show(boolean animate, Animation anim) {
        applyLayoutParams();
        if (animate) {
            refresh.startAnimation(anim);
        }
        refresh.setVisibility(View.VISIBLE);
    }
    
    private void hide(boolean animate, Animation anim) {
        refresh.setVisibility(View.GONE);
        if (animate) {
            refresh.startAnimation(anim);
        }
    }
    
    private void toggle(boolean animate, Animation animIn, Animation animOut) {
        if (refresh.isShown()) {
            hide(animate && (animOut != null), animOut);    
        } else {
            show(animate && (animIn != null), animIn);
        }
    }
    
    private void applyLayoutParams() {
        
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        
        switch (refreshPosition) {
        case POSITION_TOP_LEFT:
            lp.gravity = Gravity.LEFT | Gravity.TOP;
            lp.setMargins(refreshMarginH, refreshMarginV, 0, 0);
            break;
        case POSITION_TOP_RIGHT:
            lp.gravity = Gravity.RIGHT | Gravity.TOP;
            lp.setMargins(0, refreshMarginV, refreshMarginH, 0);
            break;
        case POSITION_BOTTOM_LEFT:
            lp.gravity = Gravity.LEFT | Gravity.BOTTOM;
            lp.setMargins(refreshMarginH, 0, 0, refreshMarginV);
            break;
        case POSITION_BOTTOM_RIGHT:
            lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;
            lp.setMargins(0, 0, refreshMarginH, refreshMarginV);
            break;
        case POSITION_CENTER:
            lp.gravity = Gravity.CENTER;
            lp.setMargins(0, 0, 0, 0);
            break;
        default:
            break;
        }
        
        refresh.setLayoutParams(lp);
    }
 
    /**
     * Returns the target View this refresh has been attached to.
     * 
     */
    public View getTarget() {
        return target;
    }
 
    /**
     * Is this refresh currently visible in the UI?
     * 
     */
    public boolean isShown() {
        return refresh.isShown();
    }
 
    /**
     * Returns the positioning of this refresh.
     * 
     * one of POSITION_TOP_LEFT, POSITION_TOP_RIGHT, POSITION_BOTTOM_LEFT, POSITION_BOTTOM_RIGHT, POSTION_CENTER.
     * 
     */
    public int getBadgePosition() {
        return refreshPosition;
    }
 
    /**
     * Set the positioning of this refresh.
     * 
     * @param layoutPosition one of POSITION_TOP_LEFT, POSITION_TOP_RIGHT, POSITION_BOTTOM_LEFT, POSITION_BOTTOM_RIGHT, POSTION_CENTER.
     * 
     */
    public void setBadgePosition(int layoutPosition) {
        this.refreshPosition = layoutPosition;
    }
 
    /**
     * Returns the horizontal margin from the target View that is applied to this refresh.
     * 
     */
    public int getHorizontalBadgeMargin() {
        return refreshMarginH;
    }
    
    /**
     * Returns the vertical margin from the target View that is applied to this refresh.
     * 
     */
    public int getVerticalBadgeMargin() {
        return refreshMarginV;
    }
 
    /**
     * Set the horizontal/vertical margin from the target View that is applied to this refresh.
     * 
     * @param refreshMargin the margin in pixels.
     */
    public void setBadgeMargin(int refreshMargin) {
        this.refreshMarginH = refreshMargin;
        this.refreshMarginV = refreshMargin;
    }
    
    /**
     * Set the horizontal/vertical margin from the target View that is applied to this refresh.
     * 
     * @param horizontal margin in pixels.
     * @param vertical margin in pixels.
     */
    public void setBadgeMargin(int horizontal, int vertical) {
        this.refreshMarginH = horizontal;
        this.refreshMarginV = vertical;
    }
    
    private int dipToPixels(int dip) {
        Resources r = target.getResources();
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, r.getDisplayMetrics());
        return (int) px;
    }
}