μΉ΄ν…Œκ³ λ¦¬ 보관물: Android

Android

κΈ€λΌμ΄λ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ 이미지λ₯Ό λΉ„νŠΈ 맡으둜 λ‹€μš΄λ‘œλ“œν•˜λŠ” 방법은 λ¬΄μ—‡μž…λ‹ˆκΉŒ? μ‚¬μš©ν•˜μ—¬ URL을 λ‹€μš΄λ‘œλ“œν•˜λŠ” 것은 맀우 μ‰½μŠ΅λ‹ˆλ‹€. Glide

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을 μ‚¬μš©ν•˜μ§€ λ§ˆμ‹­μ‹œμ˜€


이 글은 Android μΉ΄ν…Œκ³ λ¦¬λ‘œ λΆ„λ₯˜λ˜μ—ˆκ³  λ‹˜μ— μ˜ν•΄ 에 μž‘μ„±λμŠ΅λ‹ˆλ‹€.