-
Notifications
You must be signed in to change notification settings - Fork 176
/
ImageFilterButton.java
615 lines (566 loc) · 20 KB
/
ImageFilterButton.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.constraintlayout.utils.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Outline;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewOutlineProvider;
import androidx.annotation.RequiresApi;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.constraintlayout.widget.R;
/**
* An ImageButton that can display, combine and filter images. <b>Added in 2.0</b>
* <p>
* Subclass of AppCompatImageButton to handle various common filtering operations.
* </p>
* <h2>ImageFilterButton attributes</h2>
* <table summary="KeyTrigger attributes">
* <tr>
* <td>altSrc</td>
* <td>Provide and alternative image to the src image to allow cross fading</td>
* </tr>
* <tr>
* <td>saturation</td>
* <td>Sets the saturation of the image.<br> 0 = grayscale, 1 = original, 2 = hyper saturated</td>
* </tr
* <tr>
* <td>brightness</td>
* <td>Sets the brightness of the image.<br> 0 = black, 1 = original, 2 = twice as bright
* </td>
* </tr>
* <tr>
* <td>warmth</td>
* <td>This adjust the apparent color temperature of the image.<br> 1=neutral, 2=warm, .5=cold</td>
* </tr>
* <tr>
* <td>contrast</td>
* <td>This sets the contrast. 1 = unchanged, 0 = gray, 2 = high contrast</td>
* </tr>
* <tr>
* <td>crossfade</td>
* <td>Set the current mix between the two images. <br> 0=src 1= altSrc image</td>
* </tr>
* <tr>
* <td>round</td>
* <td>(id) call the TransitionListener with this trigger id</td>
* </tr>
* <tr>
* <td>roundPercent</td>
* <td>Set the corner radius of curvature as a fraction of the smaller side.
* For squares 1 will result in a circle</td>
* </tr>
* <tr>
* <td>overlay</td>
* <td>Defines whether the alt image will be faded in on top of the original image or if it will be
* crossfaded with it. Default is true. Set to false for semitransparent objects</td>
* </tr>
* </table>
*/
public class ImageFilterButton extends androidx.appcompat.widget.AppCompatImageButton {
private ImageFilterView.ImageMatrix mImageMatrix = new ImageFilterView.ImageMatrix();
private float mCrossfade = 0;
private float mRoundPercent = 0; // rounds the corners as a percent
private float mRound = Float.NaN; // rounds the corners in dp if NaN RoundPercent is in effect
private Path mPath;
ViewOutlineProvider mViewOutlineProvider;
RectF mRect;
Drawable[] mLayers = new Drawable[2];
LayerDrawable mLayer;
private boolean mOverlay = true;
private Drawable mAltDrawable = null;
private Drawable mDrawable = null;
public ImageFilterButton(Context context) {
super(context);
init(context, null);
}
public ImageFilterButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public ImageFilterButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
setPadding(0, 0, 0, 0);
if (attrs != null) {
TypedArray a = getContext()
.obtainStyledAttributes(attrs, R.styleable.ImageFilterView);
final int N = a.getIndexCount();
mAltDrawable = a.getDrawable(R.styleable.ImageFilterView_altSrc);
for (int i = 0; i < N; i++) {
int attr = a.getIndex(i);
if (attr == R.styleable.ImageFilterView_crossfade) {
mCrossfade = a.getFloat(attr, 0);
} else if (attr == R.styleable.ImageFilterView_warmth) {
setWarmth(a.getFloat(attr, 0));
} else if (attr == R.styleable.ImageFilterView_saturation) {
setSaturation(a.getFloat(attr, 0));
} else if (attr == R.styleable.ImageFilterView_contrast) {
setContrast(a.getFloat(attr, 0));
} else if (attr == R.styleable.ImageFilterView_round) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setRound(a.getDimension(attr, 0));
}
} else if (attr == R.styleable.ImageFilterView_roundPercent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setRoundPercent(a.getFloat(attr, 0));
}
} else if (attr == R.styleable.ImageFilterView_overlay) {
setOverlay(a.getBoolean(attr, mOverlay));
} else if (attr == R.styleable.ImageFilterView_imagePanX) {
setImagePanX(a.getFloat(attr, mPanX));
} else if (attr == R.styleable.ImageFilterView_imagePanY) {
setImagePanY(a.getFloat(attr, mPanY));
} else if (attr == R.styleable.ImageFilterView_imageRotate) {
setImageRotate(a.getFloat(attr, mRotate));
} else if (attr == R.styleable.ImageFilterView_imageZoom) {
setImageZoom(a.getFloat(attr, mZoom));
}
}
a.recycle();
mDrawable = getDrawable();
if (mAltDrawable != null && mDrawable != null) {
mLayers[0] = mDrawable = getDrawable().mutate();
mLayers[1] = mAltDrawable.mutate();
mLayer = new LayerDrawable(mLayers);
mLayer.getDrawable(1).setAlpha((int) (255 * (mCrossfade)));
if (!mOverlay) {
mLayer.getDrawable(0).setAlpha((int) (255 * (1 - mCrossfade)));
}
super.setImageDrawable(mLayer);
} else {
mDrawable = getDrawable();
if (mDrawable != null) {
mLayers[0] = mDrawable = mDrawable.mutate();
}
}
}
}
// ======================== support for pan/zoom/rotate =================
// defined as 0 = center of screen
// if with < scree with, 1 is the right edge lines up with screen
// if width > screen width, 1 is thee left edge lines up
// -1 works similarly
// zoom 1 = the image fits such that the view is filed
private float mPanX = Float.NaN;
private float mPanY = Float.NaN;
private float mZoom = Float.NaN;
private float mRotate = Float.NaN;
/**
* gts the pan from the center
* pan of 1 the image is "all the way to the right"
* if the images width is greater than the screen width, pan = 1 results in the left edge lining up
* if the images width is less than the screen width, pan = 1 results in the right edges lining up
* if image width == screen width it does nothing
*
* @return the pan in X. Where 0 is centered = Float. NaN if not set
*/
public float getImagePanX() {
return mPanX;
}
/**
* gets the pan from the center
* pan of 1 the image is "all the way to the bottom"
* if the images width is greater than the screen height, pan = 1 results in the bottom edge lining up
* if the images width is less than the screen height, pan = 1 results in the top edges lining up
* if image height == screen height it does nothing
*
* @return pan in Y. Where 0 is centered
*/
public float getImagePanY() {
return mPanY;
}
/**
* sets the zoom where 1 scales the image just enough to fill the view
*
* @return the zoom
*/
public float getImageZoom() {
return mZoom;
}
/**
* gets the rotation
*
* @return the rotation in degrees
*/
public float getImageRotate() {
return mRotate;
}
/**
* sets the pan from the center
* pan of 1 the image is "all the way to the right"
* if the images width is greater than the screen width, pan = 1 results in the left edge lining up
* if the images width is less than the screen width, pan = 1 results in the right edges lining up
* if image width == screen width it does nothing
*
* @param pan sets the pan in X. Where 0 is centered
*/
public void setImagePanX(float pan) {
mPanX = pan;
updateViewMatrix();
}
/**
* sets the pan from the center
* pan of 1 the image is "all the way to the bottom"
* if the images width is greater than the screen height, pan = 1 results in the bottom edge lining up
* if the images width is less than the screen height, pan = 1 results in the top edges lining up
* if image height == screen height it does nothing
*
* @param pan sets the pan in Y. Where 0 is centered
*/
public void setImagePanY(float pan) {
mPanY = pan;
updateViewMatrix();
}
/**
* sets the zoom where 1 scales the image just enough to fill the view
*
* @param zoom the zoom factor
*/
public void setImageZoom(float zoom) {
mZoom = zoom;
updateViewMatrix();
}
/**
* sets the rotation angle of the image in degrees
*
* @rotation the rotation in degrees
*/
public void setImageRotate(float rotation) {
mRotate = rotation;
updateViewMatrix();
}
@Override
public void setImageDrawable(Drawable drawable) {
if (mAltDrawable != null && drawable !=null) {
mDrawable = drawable.mutate();
mLayers[0] = mDrawable;
mLayers[1] = mAltDrawable;
mLayer = new LayerDrawable(mLayers);
super.setImageDrawable(mLayer);
setCrossfade(mCrossfade);
} else {
super.setImageDrawable(drawable);
}
}
@Override
public void setImageResource(int resId) {
if (mAltDrawable != null) {
mDrawable = AppCompatResources.getDrawable(getContext(), resId).mutate();
mLayers[0] = mDrawable;
mLayers[1] = mAltDrawable;
mLayer = new LayerDrawable(mLayers);
super.setImageDrawable(mLayer);
setCrossfade(mCrossfade);
} else {
super.setImageResource(resId);
}
}
/**
* Set the alternative image used to cross fade to.
*
* @param resId
*/
public void setAltImageResource(int resId) {
mAltDrawable = AppCompatResources.getDrawable(getContext(), resId).mutate();
mLayers[0] = mDrawable;
mLayers[1] = mAltDrawable;
mLayer = new LayerDrawable(mLayers);
super.setImageDrawable(mLayer);
setCrossfade(mCrossfade);
}
private void updateViewMatrix() {
if (Float.isNaN(mPanX) &&
Float.isNaN(mPanY) &&
Float.isNaN(mZoom) &&
Float.isNaN(mRotate)
) {
setScaleType(ScaleType.FIT_CENTER);
return;
}
setMatrix();
}
private void setMatrix() {
if (Float.isNaN(mPanX) &&
Float.isNaN(mPanY) &&
Float.isNaN(mZoom) &&
Float.isNaN(mRotate)
) {
return;
}
float panX = (Float.isNaN(mPanX)) ? 0 : mPanX;
float panY = (Float.isNaN(mPanY)) ? 0 : mPanY;
float zoom = (Float.isNaN(mZoom)) ? 1 : mZoom;
float rota = (Float.isNaN(mRotate)) ? 0 : mRotate;
Matrix imageMatrix = new Matrix();
imageMatrix.reset();
float iw = getDrawable().getIntrinsicWidth();
float ih = getDrawable().getIntrinsicHeight();
float sw = getWidth();
float sh = getHeight();
float scale = zoom * ((iw * sh < ih * sw) ? sw / iw : sh / ih);
imageMatrix.postScale(scale, scale);
float tx = 0.5f * (panX * (sw - scale * iw) + sw - (scale * iw));
float ty = 0.5f * (panY * (sh - scale * ih) + sh - (scale * ih));
imageMatrix.postTranslate(tx, ty);
imageMatrix.postRotate(rota, sw / 2, sh / 2);
setImageMatrix(imageMatrix);
setScaleType(ScaleType.MATRIX);
}
// ================================================================
/**
* Defines whether the alt image will be faded in on top of the original image or if it will be
* crossfaded with it. Default is true.
*
* @param overlay
*/
private void setOverlay(boolean overlay) {
mOverlay = overlay;
}
/**
* sets the saturation of the image;
* 0 = grayscale, 1 = original, 2 = hyper saturated
*
* @param saturation
*/
public void setSaturation(float saturation) {
mImageMatrix.mSaturation = saturation;
mImageMatrix.updateMatrix(this);
}
/**
* Returns the currently applied saturation
*
* @return 0 = grayscale, 1 = original, 2 = hyper saturated
*/
public float getSaturation() {
return mImageMatrix.mSaturation;
}
/**
* This sets the contrast. 1 = unchanged, 0 = gray, 2 = high contrast
*
* @param contrast
*/
public void setContrast(float contrast) {
mImageMatrix.mContrast = contrast;
mImageMatrix.updateMatrix(this);
}
/**
* Returns the currently applied contrast
*
* @return 1 = unchanged, 0 = gray, 2 = high contrast
*/
public float getContrast() {
return mImageMatrix.mContrast;
}
/**
* This makes the apparent color temperature of the image warmer or colder.
*
* @param warmth 1 is neutral, 2 is warm, .5 is cold
*/
public void setWarmth(float warmth) {
mImageMatrix.mWarmth = warmth;
mImageMatrix.updateMatrix(this);
}
/**
* Returns the currently applied warmth
*
* @return warmth 1 is neutral, 2 is warm, .5 is cold
*/
public float getWarmth() {
return mImageMatrix.mWarmth;
}
/**
* Set the current mix between the two images that can be set on this view.
*
* @param crossfade a number from 0 to 1
*/
public void setCrossfade(float crossfade) {
mCrossfade = crossfade;
if (mLayers != null) {
if (!mOverlay) {
mLayer.getDrawable(0).setAlpha((int) (255 * (1 - mCrossfade)));
}
mLayer.getDrawable(1).setAlpha((int) (255 * (mCrossfade)));
super.setImageDrawable(mLayer);
}
}
/**
* Returns the currently applied crossfade.
*
* @return a number from 0 to 1
*/
public float getCrossfade() {
return mCrossfade;
}
/**
* sets the brightness of the image;
* 0 = black, 1 = original, 2 = twice as bright
*
* @param brightness
*/
public void setBrightness(float brightness) {
mImageMatrix.mBrightness = brightness;
mImageMatrix.updateMatrix(this);
}
/**
* Set the corner radius of curvature as a fraction of the smaller side.
* For squares 1 will result in a circle
*
* @param round the radius of curvature as a fraction of the smaller width
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
public void setRoundPercent(float round) {
boolean change = (mRoundPercent != round);
mRoundPercent = round;
if (mRoundPercent != 0.0f) {
if (mPath == null) {
mPath = new Path();
}
if (mRect == null) {
mRect = new RectF();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (mViewOutlineProvider == null) {
mViewOutlineProvider = new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
int w = getWidth();
int h = getHeight();
float r = Math.min(w, h) * mRoundPercent / 2;
outline.setRoundRect(0, 0, w, h, r);
}
};
setOutlineProvider(mViewOutlineProvider);
}
setClipToOutline(true);
}
int w = getWidth();
int h = getHeight();
float r = Math.min(w, h) * mRoundPercent / 2;
mRect.set(0, 0, w, h);
mPath.reset();
mPath.addRoundRect(mRect, r, r, Path.Direction.CW);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setClipToOutline(false);
}
}
if (change) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
invalidateOutline();
}
}
}
/**
* Set the corner radius of curvature
*
* @param round the radius of curvature NaN = default meaning roundPercent in effect
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
public void setRound(float round) {
if (Float.isNaN(round)) {
mRound = round;
float tmp = mRoundPercent;
mRoundPercent = -1;
setRoundPercent(tmp); // force eval of roundPercent
return;
}
boolean change = (mRound != round);
mRound = round;
if (mRound != 0.0f) {
if (mPath == null) {
mPath = new Path();
}
if (mRect == null) {
mRect = new RectF();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (mViewOutlineProvider == null) {
mViewOutlineProvider = new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
int w = getWidth();
int h = getHeight();
outline.setRoundRect(0, 0, w, h, mRound);
}
};
setOutlineProvider(mViewOutlineProvider);
}
setClipToOutline(true);
}
int w = getWidth();
int h = getHeight();
mRect.set(0, 0, w, h);
mPath.reset();
mPath.addRoundRect(mRect, mRound, mRound, Path.Direction.CW);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setClipToOutline(false);
}
}
if (change) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
invalidateOutline();
}
}
}
/**
* Get the fractional corner radius of curvature.
*
* @return Fractional radius of curvature with respect to smallest size
*/
public float getRoundPercent() {
return mRoundPercent;
}
/**
* Get the corner radius of curvature NaN = RoundPercent in effect.
*
* @return Radius of curvature
*/
public float getRound() {
return mRound;
}
@Override
public void draw(Canvas canvas) {
boolean clip = false;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
if (mRound != 0.0f && mPath != null) {
clip = true;
canvas.save();
canvas.clipPath(mPath);
}
}
super.draw(canvas);
if (clip) {
canvas.restore();
}
}
@Override
public void layout(int l, int t, int r, int b) {
super.layout(l, t, r, b);
setMatrix();
}
}