1
1
package fr .nuage .souvenirs .view ;
2
2
3
- import static com .bumptech .glide .request .RequestOptions .bitmapTransform ;
4
-
3
+ import android .graphics .Bitmap ;
5
4
import android .graphics .drawable .AnimatedVectorDrawable ;
6
5
import android .graphics .drawable .Drawable ;
7
6
import android .util .TypedValue ;
8
7
import android .view .View ;
9
- import android .widget .FrameLayout ;
10
8
import android .widget .ImageView ;
11
- import android .widget .VideoView ;
12
9
13
- import androidx .annotation .Nullable ;
14
10
import androidx .constraintlayout .widget .ConstraintLayout ;
15
11
import androidx .constraintlayout .widget .Guideline ;
16
12
import androidx .core .content .ContextCompat ;
17
13
import androidx .databinding .BindingAdapter ;
18
14
19
15
import com .bumptech .glide .Glide ;
20
- import com .bumptech .glide .load .engine .DiskCacheStrategy ;
21
- import com .bumptech .glide .request .RequestOptions ;
22
16
import com .bumptech .glide .request .target .CustomTarget ;
23
- import com .bumptech .glide .request .target .SimpleTarget ;
24
17
import com .bumptech .glide .request .transition .Transition ;
25
18
import com .google .android .material .card .MaterialCardView ;
26
19
27
20
import java .io .File ;
28
-
21
+ import androidx .annotation .NonNull ;
22
+ import androidx .annotation .Nullable ;
29
23
import fr .nuage .souvenirs .R ;
30
24
import fr .nuage .souvenirs .model .ImageElement ;
31
- import fr .nuage .souvenirs .view .helpers .BlurTransformation ;
32
- import fr .nuage .souvenirs .view .helpers .ZoomOffsetTransformation ;
33
- import fr .nuage .souvenirs .viewmodel .ImageElementViewModel ;
25
+ import fr .nuage .souvenirs .viewmodel .ElementViewModel ;
34
26
35
27
public class DataBindingAdapters {
36
28
@@ -45,36 +37,41 @@ public static void setLayoutConstraintGuidePercent(Guideline guideline, Integer
45
37
46
38
47
39
@ BindingAdapter (value = { "srcCompat" , "android:scrollX" , "android:scrollY" , "android:scaleX" }, requireAll =false )
48
- public static void setSrcCompatZoomOffset (ImageView view , String imagePath , int offsetX , int offsetY , int scaleX ) {
40
+ public static void setSrcCompatZoomOffset (ImageElementView view , String imagePath , int offsetX , int offsetY , int scaleX ) {
49
41
if (imagePath != null ) {
50
42
if (imagePath .equals ("" )) {
51
43
view .setImageDrawable (ContextCompat .getDrawable (view .getContext (),R .drawable .ic_image_black_24dp ));
52
44
} else {
53
- if (view .getScaleType () == ImageView .ScaleType .MATRIX ) {
54
- Glide .with (view .getContext ()).load (new File (imagePath )).dontTransform ().transform (new ZoomOffsetTransformation (offsetX , offsetY ,scaleX )).into (view );
55
- if (scaleX < 100 ) {
56
- //if image smaller than layout, display blured background
57
- Glide .with (view .getContext ()).load (new File (imagePath ))
58
- .transform (new BlurTransformation (25 ,view .getContext ()))
59
- .into (new CustomTarget <Drawable >() {
60
- @ Override
61
- public void onResourceReady (Drawable resource , Transition <? super Drawable > transition ) {
62
- view .setBackground (resource );
63
- }
64
- @ Override
65
- public void onLoadCleared (@ Nullable @ org .jetbrains .annotations .Nullable Drawable placeholder ) { }
66
- });
67
- }
68
-
69
- } else {
70
- Glide .with (view .getContext ()).load (new File (imagePath )).into (view );
71
- }
45
+ view .setOffsetX (offsetX );
46
+ view .setOffsetY (offsetY );
47
+ view .setZoom (scaleX );
48
+ Glide .with (view .getContext ()).load (new File (imagePath )).into (view );
72
49
}
73
50
} else {
74
51
view .setImageDrawable (null );
75
52
}
76
53
}
77
54
55
+ @ BindingAdapter (value = { "srcCompat" }, requireAll =false )
56
+ public static void setSrcCompatZoomOffset (PaintElementView view , String imagePath ) {
57
+ if (imagePath != null ) {
58
+ if (!imagePath .equals ("" )) {
59
+ Glide .with (view .getContext ())
60
+ .asBitmap ()
61
+ .load (new File (imagePath ))
62
+ .into (new CustomTarget <Bitmap >() {
63
+ @ Override
64
+ public void onResourceReady (@ NonNull Bitmap resource , Transition <? super Bitmap > transition ) {
65
+ view .setFirstBitmap (resource );
66
+ }
67
+
68
+ @ Override
69
+ public void onLoadCleared (@ Nullable Drawable placeholder ) { }
70
+ });
71
+ }
72
+ }
73
+ }
74
+
78
75
@ BindingAdapter ("srcCompat" )
79
76
public static void setSrcCompat (ImageView view , Drawable drawable ) {
80
77
view .setImageDrawable (drawable );
@@ -112,11 +109,33 @@ public static void setTint(ImageView v, int color) {
112
109
v .setColorFilter (color );
113
110
}
114
111
115
- @ BindingAdapter (value = { "android:layout_marginLeft" , "android:layout_marginBottom" , "android:layout_marginRight" , "android:layout_marginTop" })
116
- public static void setLayoutMarginLeft (MaterialCardView v , float marginLeft , float marginTop , float marginRight , float marginBottom ) {
117
- ConstraintLayout .LayoutParams params = (ConstraintLayout .LayoutParams ) v .getLayoutParams ();
118
- params .setMargins ((int )marginLeft , (int )marginTop , (int )marginRight , (int )marginBottom );
119
- v .setLayoutParams (params );
112
+ @ BindingAdapter (value = { "android:layout_marginStart" , "android:layout_marginTop" , "android:layout_marginEnd" , "android:layout_marginBottom" }, requireAll =false )
113
+ public static void setLayoutMarginLeft (View v , int marginLeft , int marginTop , int marginRight , int marginBottom ) {
114
+ if ((v .getParent () != null ) && !(v instanceof PaintElementView )) {
115
+ int height = ((View )(v .getParent ())).getHeight ();
116
+ int width = ((View )(v .getParent ())).getWidth ();
117
+ if ((height > 0 ) && (width > 0 )) {
118
+ ConstraintLayout .LayoutParams params = (ConstraintLayout .LayoutParams ) v .getLayoutParams ();
119
+ params .setMargins ((int )width * marginLeft / 100 ,
120
+ (int )height * marginTop / 100 ,
121
+ (int )(100 - marginRight ) * width / 100 ,
122
+ (int )(100 - marginBottom ) * height / 100 );
123
+ v .setLayoutParams (params );
124
+ }
125
+ ImageElementView imageElementView = v .findViewById (R .id .image_imageview );
126
+ if (imageElementView != null ) {
127
+ imageElementView .updateMatrix ();
128
+ }
129
+ }
130
+ }
131
+
132
+ public static void onLayoutChange (View view , int left , int top , int right , int bottom ) {
133
+ setLayoutMarginLeft (view , left , top , right , bottom );
134
+ }
135
+
136
+ @ BindingAdapter ("is_selected" )
137
+ public static void setSelected (View view , boolean selected ) {
138
+ view .setSelected (selected );
120
139
}
121
140
122
141
}
0 commit comments