ImageViewκΈλΌμ΄λλ₯Ό μ¬μ©νμ¬ URLμ λ€μ΄λ‘λνλ κ²μ λ§€μ° μ½μ΅λλ€.
Glide
.with(context)
.load(getIntent().getData())
.placeholder(R.drawable.ic_loading)
.centerCrop()
.into(imageView);
μ λ€μ΄λ‘λ ν μ μλμ§ κΆκΈν©λλ€ Bitmap. λ€λ₯Έ λꡬλ₯Ό μ¬μ©νμ¬ μ‘°μ ν μμλ μμ λΉνΈ λ§΅μΌλ‘ λ€μ΄λ‘λνκ³ μΆμ΅λλ€. λλ μ½λλ₯Ό κ²ͺμ΄ μμΌλ©° κ·Έκ²μνλ λ°©λ²μ λ³΄μ§ λͺ»νμ΅λλ€.
λ΅λ³
μ΅μ λ²μ μΈμ§ νμΈνμμμ€
implementation 'com.github.bumptech.glide:glide:4.10.0'
μ½ νλ¦° :
Glide.with(this)
.asBitmap()
.load(imagePath)
.into(object : CustomTarget<Bitmap>(){
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
imageView.setImageBitmap(resource)
}
override fun onLoadCleared(placeholder: Drawable?) {
// this is called when imageView is cleared on lifecycle call or for
// some other reason.
// if you are referencing the bitmap somewhere else too other than this imageView
// clear it here as you can no longer have the bitmap
}
})
λΉνΈ λ§΅ ν¬κΈ° :
μ΄λ―Έμ§μ μλ ν¬κΈ°λ₯Ό μ¬μ©νλ €λ©΄ μμ κ°μ΄ κΈ°λ³Έ μμ±μλ₯Ό μ¬μ©νμμμ€. κ·Έλ μ§ μμΌλ©΄ μνλ λΉνΈ λ§΅ ν¬κΈ°λ₯Ό μ λ¬ν μ μμ΅λλ€
into(object : CustomTarget<Bitmap>(1980, 1080)
μλ°:
Glide.with(this)
.asBitmap()
.load(path)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
κΈ°μ‘΄ λ΅λ³ :
μ compile 'com.github.bumptech.glide:glide:4.8.0'μλ
Glide.with(this)
.asBitmap()
.load(path)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
}
});
λ΄μ©μ compile 'com.github.bumptech.glide:glide:3.7.0'μλ
Glide.with(this)
.load(path)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
imageView.setImageBitmap(resource);
}
});
μ΄μ κ²½κ³ κ° νμ λ μ μμ΅λλ€ SimpleTarget is deprecated
μ΄μ :
SimpleTargetμ λ μ΄μ μ¬μ©νμ§ μλ μ£Όμ μμ μ Glideμ API κ³μ½μ μλ°νλ €λ μ νΉμ λν΄ κ²½κ³ νλ κ²μ λλ€. νΉν SimpleTargetμ΄ μ§μμ§λ©΄λ‘λ ν 리μμ€ μ¬μ©μ κ°μ λ‘ μ€μ§νμ§ μμλλλ―λ‘ μΆ©λ λ° κ·Έλν½ μμμ΄ λ°μν μ μμ΅λλ€.
SimpleTargetimageViewκ° μ§μμ§λ©΄ λΉνΈ λ§΅μ μ¬μ©νμ§ μλ ν μ¬μ ν μ€νΈμ μ¬μ©ν μ μμ΅λλ€.
λ΅λ³
κΈλΌμ΄λμ μ΅μνμ§ μμ§λ§ λͺ©ν ν¬κΈ°λ₯Ό μκ³ μλ€λ©΄ λ€μκ³Ό κ°μ΄ μ¬μ©ν μ μμ΅λλ€.
Bitmap theBitmap = Glide.
with(this).
load("http://....").
asBitmap().
into(100, 100). // Width and height
get();
ν΅κ³Ό ν μμλ κ²μ²λΌ 보μ΄κ³ -1,-1μ 체 ν¬κΈ° μ΄λ―Έμ§λ₯Ό μ»μ΅λλ€ (μμ ν ν
μ€νΈλ₯Ό κΈ°λ°μΌλ‘ λ¬Έμν λ κ²μ λ³Ό μ μμ).
μ°Έκ³ into(int,int)λ€μ λ°μ FutureTarget<Bitmap>νλ©΄ μ·¨μ¬ μλ-catch λΈλ‘μ΄ ν¬μ₯ ν μ μλλ‘, ExecutionExceptionκ·Έλ¦¬κ³ InterruptedException. ν
μ€νΈλκ³ μλνλλ³΄λ€ μμ ν μμ ꡬνμ λ€μκ³Ό κ°μ΅λλ€.
class SomeActivity extends Activity {
private Bitmap theBitmap = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// onCreate stuff ...
final ImageView image = (ImageView) findViewById(R.id.imageView);
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
Looper.prepare();
try {
theBitmap = Glide.
with(SomeActivity.this).
load("https://www.google.es/images/srpr/logo11w.png").
asBitmap().
into(-1,-1).
get();
} catch (final ExecutionException e) {
Log.e(TAG, e.getMessage());
} catch (final InterruptedException e) {
Log.e(TAG, e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void dummy) {
if (null != theBitmap) {
// The full bitmap should be available here
image.setImageBitmap(theBitmap);
Log.d(TAG, "Image loaded");
};
}
}.execute();
}
}
μλ μ£Όμμμ Monkeylessμ μ μμ λ°λΌ (κ·Έλ¦¬κ³ μ΄κ²μ 곡μμ μΈ λ°©λ² μΈ κ²μ²λΌ 보μ
λλ€ ) SimpleTarget, μ νμ μΌλ‘ override(int,int)μ½λλ₯Ό λ¨μννκΈ° μν΄ μ ν¨κ» μ¬μ©ν μ μμ΅λλ€ . κ·Έλ¬λμ΄ κ²½μ° μ νν ν¬κΈ°λ₯Ό μ 곡ν΄μΌν©λλ€ (1 λ―Έλ§μ νλͺ©μ νμ©λμ§ μμ).
Glide
.with(getApplicationContext())
.load("https://www.google.es/images/srpr/logo11w.png")
.asBitmap()
.into(new SimpleTarget<Bitmap>(100,100) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
image.setImageBitmap(resource); // Possibly runOnUiThread()
}
});
λμΌν μ΄λ―Έμ§κ° νμν κ²½μ° @hennry κ° μ μνλλ‘new SimpleTarget<Bitmap>()
λ΅λ³
κ·Έκ²μ μ¬μ κ³Ό κ°μ Targetν΄λμ€ λλ κ°μ ꡬν μ€ νλλ₯Ό BitmapImageViewTargetνκ³ λ¬΄μ setResourceλΉνΈ λ§΅μ΄ κ° μμλ λ°©λ²μ΄ λ μ μΊ‘μ² λ°©λ²μ β¦
μ΄κ²μ ν μ€νΈλμ§ μμμ΅λλ€. π
Glide.with(context)
.load("http://goo.gl/h8qOq7")
.asBitmap()
.into(new BitmapImageViewTarget(imageView) {
@Override
protected void setResource(Bitmap resource) {
// Do bitmap magic here
super.setResource(resource);
}
});
λ΅λ³
μ΅μ μ 보
μ΄μ μ°λ¦¬λ μ¬μ©ν΄μΌν©λλ€ Custom Targets
μν μ½λ
Glide.with(mContext)
.asBitmap()
.load("url")
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
κΈλΌμ΄λλ₯Ό μ¬μ©νμ¬ μ΄λ―Έμ§λ₯Ό λΉνΈ λ§΅μΌλ‘ λ€μ΄λ‘λνλ λ°©λ²μ 무μμ λκΉ?
μμ λͺ¨λ λ΅λ³μ μ ννμ§λ§ ꡬμμ λλ€.
κΈλΌμ΄λμ μλ‘μ΄ λ²μ μμ implementation 'com.github.bumptech.glide:glide:4.8.0'
μ½λμμ μλ μ€λ₯λ₯Ό μ°Ύμ μ μμ΅λλ€
- λ
.asBitmap()μ¬μ©ν μ μμ΅λλ€glide:4.8.0
λ μ΄μ μ¬μ©λμ§ μμ΅λλ€SimpleTarget<Bitmap>
μ¬κΈ°μ ν΄κ²°μ± μ΄ μμ΅λλ€
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.Request;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SizeReadyCallback;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
Glide.with(this)
.load("")
.apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.NONE))
.into(new Target<Drawable>() {
@Override
public void onLoadStarted(@Nullable Drawable placeholder) {
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
}
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
Bitmap bitmap = drawableToBitmap(resource);
imageView.setImageBitmap(bitmap);
// now you can use bitmap as per your requirement
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
@Override
public void getSize(@NonNull SizeReadyCallback cb) {
}
@Override
public void removeCallback(@NonNull SizeReadyCallback cb) {
}
@Override
public void setRequest(@Nullable Request request) {
}
@Nullable
@Override
public Request getRequest() {
return null;
}
@Override
public void onStart() {
}
@Override
public void onStop() {
}
@Override
public void onDestroy() {
}
});
}
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
}
λ΅λ³
μ΄κ²μ΄ λλ₯Ό μν΄ μΌν κ²μ λλ€ : https://github.com/bumptech/glide/wiki/Custom-targets#overriding-default-behavior
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.transition.Transition;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
...
Glide.with(yourFragment)
.load("yourUrl")
.asBitmap()
.into(new BitmapImageViewTarget(yourImageView) {
@Override
public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> anim) {
super.onResourceReady(bitmap, anim);
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
// Here's your generated palette
Palette.Swatch swatch = palette.getDarkVibrantSwatch();
int color = palette.getDarkVibrantColor(swatch.getTitleTextColor());
}
});
}
});
λ΅λ³
λΉνΈ λ§΅ λ³μμ λμ λΉνΈ λ§΅ μ΄λ―Έμ§λ₯Ό ν λΉνλ €λ κ²½μ°
μ kotlin
backgroundImage = Glide.with(applicationContext).asBitmap().load(PresignedUrl().getUrl(items!![position].img)).into(100, 100).get();
μμ λ΅λ³μ μ μκ² ν¨κ³Όκ° μμμ΅λλ€.
.asBitmap μ μ μμ΄μΌν©λλ€ .load("http://....")
λ΅λ³
μ λ²μ μ λ°μ΄νΈ
Glide.with(context.applicationContext)
.load(url)
.listener(object : RequestListener<Drawable> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>?,
isFirstResource: Boolean
): Boolean {
listener?.onLoadFailed(e)
return false
}
override fun onResourceReady(
resource: Drawable?,
model: Any?,
target: com.bumptech.glide.request.target.Target<Drawable>?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
listener?.onLoadSuccess(resource)
return false
}
})
.into(this)
μ€λλ λ΅λ³
@ outlyerμ λλ΅μ μ ννμ§λ§ μλ‘μ΄ κΈλΌμ΄λ λ²μ μ μ½κ°μ λ³νκ° μμ΅λλ€
λ΄ λ²μ : 4.7.1
μνΈ:
Glide.with(context.applicationContext)
.asBitmap()
.load(iconUrl)
.into(object : SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
override fun onResourceReady(resource: Bitmap, transition: com.bumptech.glide.request.transition.Transition<in Bitmap>?) {
callback.onReady(createMarkerIcon(resource, iconId))
}
})
μ°Έκ³ :μ΄ μ½λλ UI μ€λ λμμ μ€νλλ―λ‘ AsyncTask, Executor λλ λμμ±μ μν΄ @outlyerμ μ½λμ κ°μ λ€λ₯Έ κ²μ μ¬μ©ν μ μμ΅λλ€. μλ ν¬κΈ°λ₯Ό μ»μΌλ €λ©΄ Target.SIZE_ORIGINAλ₯Ό λ΄ μ½λλ‘ μ λ ₯νμμμ€. -1, -1μ μ¬μ©νμ§ λ§μμμ€