Android Tip

Glide에 대한 기능 정리

오마로 2017. 8. 24. 14:05
반응형

Glide에 대한 기능을 자주 사용하는데, 매번 찾기도 그래서 아예 블로그로 옮겨옴 ^^


Setup

Add to your app/build.gradle file:

dependencies {
  compile 'com.github.bumptech.glide:glide:3.8.0'
}

Basic Usage

Glide.with(context)
    .load("http://via.placeholder.com/300.png")
    .into(ivImg);

Advanced Usage

Resizing images with:

Glide.with(context)
    .load("http://via.placeholder.com/300.png")
    .override(300, 200)
    .into(ivImg);

Placeholder and error images:

Glide.with(context)
    .load("http://via.placeholder.com/300.png")
    .placeholder(R.drawable.placeholder)
    .error(R.drawable.imagenotfound)
    .into(ivImg);

Cropping images with:

Glide.with(context)
    .load("http://via.placeholder.com/300.png")
    .centerCrop()
    .into(ivImg);

Resizing

Glide.with(context)
    .load("http://via.placeholder.com/300.png")
    .override(100, 200) // resizes the image to 100x200 pixels but does not respect aspect ratio
    .into(ivImg);


Glide.with(context)
    .load("http://via.placeholder.com/300.png")
    .override(100, Target.SIZE_ORIGINAL) // resizes width to 100, preserves original height, does not respect aspect ratio
    .into(ivImg);


Glide.with(context)
    .load("http://via.placeholder.com/300.png")
    .override(100, 200) 
    .centerCrop() // scale to fill the ImageView and crop any extra
    .into(ivImg);


Glide.with(context)
    .load("http://via.placeholder.com/300.png")
    .override(100, 200) 
    .fitCenter() // scale to fit entire image within ImageView
    .into(ivImg);


Loading Errors


Glide.with(context)
        .load("http://via.placeholder.com/300.png")
        .placeholder(R.drawable.placeholder)
        .error(R.drawable.imagenotfound)
        .listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                // log exception
                Log.e("TAG", "Error loading image", e);
                return false; // important to return false so the error placeholder can be placed
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                return false;
            }
        })
        .into(ivImg);



Rounded Corners

int radius = 30; // corner radius, higher value = more rounded
int margin = 10; // crop margin, set to 0 for corners with no crop
Glide.with(this)
        .load("http://via.placeholder.com/300.png")
        .bitmapTransform(new RoundedCornersTransformation(context, radius, margin))
        .into(ivImg);

Crop

Circle crop:

Glide.with(this)
        .load("http://via.placeholder.com/300.png")
        .bitmapTransform(new CropCircleTransformation(context))
        .into(ivImg);

Effects

Blur:

Glide.with(this)
        .load("http://via.placeholder.com/300.png")
        .bitmapTransform(new BlurTransformation(context))
        .into(ivImg);

Multiple transforms:

    Glide.with(this)
            .load("http://via.placeholder.com/300.png")
            .bitmapTransform(new BlurTransformation(context, 25), new CropCircleTransformation(context))
            .into(ivImg);


    Showing a ProgressBar

    Add a ProgressBar or otherwise handle callbacks for an image that is loading:

    progressBar.setVisibility(View.VISIBLE);
    
    Glide.with(this)
            .load("http://via.placeholder.com/300.png")
            .listener(new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                    progressBar.setVisibility(View.GONE);
                    return false; // important to return false so the error placeholder can be placed
                }
    
                @Override
                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    progressBar.setVisibility(View.GONE);
                    return false;
                }
            })
            .into(ivImg);

    Adjusting Image Size Dynamically

    private SimpleTarget target = new SimpleTarget<Bitmap>() {  
        @Override
        public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
            // do something with the bitmap
            // set it to an ImageView
            imageView.setImageBitmap(bitmap);
        }
    };
    

    Next, pass the SimpleTarget to Glide via into():

    Glide.with(context)
            .load("http://via.placeholder.com/300.png")
            .asBitmap()
            .into(target);


    Glide 4.X에서 Bitmap 가지고 오기

    Bitmap bitmap = Glide
        .with(context)
        .asBitmap()
        .load(uri_File_String_Or_ResourceId)
        .submit()
        .get();


    LinearLayout에 Bitmap 세팅

    Glide.with(getApplicationContext())
    .load(mCardBean.getRes_cdp_path())
    .asBitmap()
    .format(DecodeFormat.PREFER_ARGB_8888)
    .into(new SimpleTarget<Bitmap>() {
    @Override
    public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
    Drawable d = new BitmapDrawable(getResources(), bitmap);
    mLltRoot.setBackground(d);
    }
    });
    Glide.with(getApplicationContext())
    .load(mCardBgImgUrl)
    .asBitmap()
    .format(DecodeFormat.PREFER_ARGB_8888)
    .into(new SimpleTarget<Bitmap>() {
    @Override
    public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
    mCurrBitmap = bitmap;
    mCardImg.setImageBitmap(bitmap);
    mPb.setVisibility(View.GONE);
    }

    @Override
    public void onLoadFailed(Exception e, Drawable errorDrawable) {
    mPb.setVisibility(View.GONE);
    }
    });


    반응형