This repository was archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIceImageUtils.java
More file actions
145 lines (98 loc) · 4.12 KB
/
Copy pathIceImageUtils.java
File metadata and controls
145 lines (98 loc) · 4.12 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
package ovh.ice.icecons;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ImageView;
import java.lang.ref.WeakReference;
public class IceImageUtils {
public static void bitmapLoadAsync( ImageView imageView, Resources res, int resId, int width, int height ) {
BitmapWorkerTask task = new BitmapWorkerTask( imageView );
task.res = res;
task.resId = resId;
task.width = width;
task.height = height;
task.execute( resId );
}
public static Bitmap bitmapLoad( Resources res, int resId, int width, int height ) {
// calc scale, load appropriately sampled bitmap from given resource
BitmapFactory.Options resOptions = new BitmapFactory.Options();
resOptions.inJustDecodeBounds = true;
BitmapFactory.decodeResource( res, resId, resOptions );
int resHeight = resOptions.outHeight;
int resWidth = resOptions.outWidth;
float xScale = (float) width / (float) resWidth;
float yScale = (float) height / (float) resHeight;
float scale = Math.max( xScale, yScale );
if( width == 0 ) width = Math.round( resWidth / scale );
else if( height == 0 ) height = Math.round( resHeight / scale );
resOptions.inSampleSize = sampleSize( scale );
resWidth /= resOptions.inSampleSize;
resHeight /= resOptions.inSampleSize;
resOptions.inJustDecodeBounds = false;
Bitmap rawBitmap = BitmapFactory.decodeResource( res, resId, resOptions );
// compare aspect ratio and crop
rawBitmap = bitmapCrop( rawBitmap, width, height, resWidth, resHeight );
// scale to desired size
return Bitmap.createScaledBitmap( rawBitmap, width, height, true );
}
// calc sample size for scaled resource loading
private static int sampleSize( float scale ) {
int size = 1;
while( scale < 0.5f ) {
size *= 2;
scale *= 2;
}
return size;
}
// crop bitmap to chosen aspect ratio
private static Bitmap bitmapCrop( Bitmap rawBitmap, int width, int height, int resWidth, int resHeight ) {
int cropX, cropY, cropWidth, cropHeight;
float xScale = (float) width / (float) resWidth;
float yScale = (float) height / (float) resHeight;
float scale = Math.max( xScale, yScale );
if( xScale >= yScale ) {
cropWidth = Math.round( resWidth );
cropX = 0;
cropHeight = Math.round( height / scale );
cropY = ( resHeight - cropHeight ) / 2;
}
else {
cropWidth = Math.round( width / scale );
cropX = ( resWidth - cropWidth ) / 2;
cropHeight = Math.round( resHeight );
cropY = 0;
}
return Bitmap.createBitmap( rawBitmap, cropX, cropY, cropWidth, cropHeight );
}
}
class BitmapWorkerTask extends AsyncTask< Integer, Void, Bitmap > {
private final WeakReference< ImageView > imageViewReference;
Resources res;
int resId, width, height;
public BitmapWorkerTask( ImageView imageView ) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background
@Override
protected Bitmap doInBackground( Integer... parameters ) {
return IceImageUtils.bitmapLoad( res, resId, width, height );
}
// Check if image view still exists and set bitmap
@Override
protected void onPostExecute( Bitmap bitmap ) {
if ( imageViewReference != null && bitmap != null ) {
final ImageView imageView = imageViewReference.get();
if ( imageView != null ) {
imageView.setImageBitmap(bitmap);
imageView.setAlpha( 0f );
imageView.setVisibility( View.VISIBLE );
imageView.animate()
.alpha(1f)
.setDuration( res.getInteger( android.R.integer.config_mediumAnimTime ) )
.setListener( null );
}
}
}
}