I have set <ImageView /> height to wrap_content. I am adding this layout in recycler adapter
There will be around 10 images all images are set to wrap_content.
Is there any way to get the height of image rendered by Glide.
I am using this, but its returning the screen dimensions and not image dimensions.
Glide.with(context).load(imgpth).placeholder(R.drawable.bg_loading)
.error(R.drawable.bg_loading).into(imageProdctBanner).getSize(new SizeReadyCallback() {
#Override
public void onSizeReady(int width, int height) {
Log.e("width","wdthheight "+width+" : "+height);
}
});
I need this to set height of recyclerView.
int totalItems= cardsRecyclerAdapter.getItemCount();
ViewGroup.LayoutParams params=rvCards.getLayoutParams();
params.height= heightOfImageView *totalItems;
rvCards.setLayoutParams(params);
use onResourceReady() method instead
Glide.with(getContext().getApplicationContext())
.asBitmap()
.load(path)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap bitmap,
Transition<? super Bitmap> transition) {
int w = bitmap.getWidth();
int h = bitmap.getHeight()
mImageView.setImageBitmap(bitmap);
}
});
Related
I would like to add a frame around my image which I am loading with Glide. My image doesn't have a fix size, so adding frame as a background does not work.
Is there an easy way to make the frame?
Yeah there is a way .
int myWidth = 512;
int myHeight = 512;
int borderSize=20;
Glide.with(yourApplicationContext))
.load(youUrl)
.asBitmap()
.into(new SimpleTarget<Bitmap>(myWidth, myHeight) {
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
Bitmap bmpWithBorder = Bitmap.createBitmap(bitmap.getWidth() + borderSize * 2, bitmap.getHeight() + borderSize * 2, bitmap.getConfig());
Canvas canvas = new Canvas(bmpWithBorder);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bitmap, borderSize, borderSize, null);
//Now you can use bmpWithBorder on ImageView you want
}
};
I have imageview, which should show only part of image (part which fits into part of screen). After user click, it will open whole image. But I canĀ“t figure out, how to load only part of image into imageview. Problem is that Glide fits image into imageview, always.
Glide.with(context)
.using(new FirebaseImageLoader())
.load(storageReference)
.into(imageView);
How it is looks now. Red part is ImageView.
How I wish to have it.
EDIT
Following code will fits, but it show whole image, only only part of it and it destroy proportions.
android:scaleType="fitXY"
android:adjustViewBounds="true"
Add scale type in your XML of ImageView. add this android:scaleType="matrix"
Glide.with(mContext)
.using(new FirebaseImageLoader())
.load(storageReference)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
Bitmap targetBitmap = Bitmap.createBitmap(resource, x, y, width, height);
imageView.setImageBitmap(targetBitmap);
}
});
Here you can resize your bitmap from Glide according to your dimensions. But make sure your provided dimensions are always smaller than the actual bitmap.
Since you are using this code in list, Bitmap targetBitmap; should be declared globally to reuse same bitmap object throughout the list. This will reduce RAM consumption of your app.
Read links related to cropping bitmaps if you need further clarity for the code Bitmap targetBitmap = Bitmap.createBitmap(resource, x, y, width, height);
Don't forget to make targetBitmap as Global, otherwise memory impact will be too high.
Just add this to your Imageview
android:adjustViewBounds="true"
android:scaleType="fitXY"
Add this into your xml
android:scaleType="fitXY"
android:adjustViewBounds="true"
Ok, thank to Mohammed Atif, Patel Jaimin and Henry, I end up with following solution.
<ImageView
android:id="#+id/ivImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix"/>
Glide.with(this)
.using(new FirebaseImageLoader())
.load(storageReference)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
Bitmap targetBitmap = Bitmap.createBitmap(resource, 0, 0, resource.getWidth(), resource.getHeight());
ivImage.setImageBitmap(targetBitmap);
final Matrix matrix = ivImage.getImageMatrix();
final float imageWidth = ivImage.getDrawable().getIntrinsicWidth();
final float scaleRatio = ivImage.getWidth() / imageWidth;
matrix.postScale(scaleRatio, scaleRatio);
ivImage.setImageMatrix(matrix);
}
});
How could I make image scale down and fit ImageView, but avoid upscaling?
Have solved it with Glide, adding
...
.transform(new FitCenter(context) {
#Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
if (toTransform.getHeight() > outHeight || toTransform.getWidth() > outWidth)
return super.transform(pool, toTransform, outWidth, outHeight);
else
return toTransform;
}
})
...
and android:scaleType="center" in target ImageView.
It seems there is no way to get such a behavior using just ImageView attributes :(
You can use Picasso library with the parameter .onlyScaleDown()
Picasso
.with(context)
.load(imageURI)
.resize(600, 800) // or better use fit()
.onlyScaleDown() // the image will only be resized if it's bigger than 6000x2000 pixels.
.into(imageViewResizeScaleDown);
https://futurestud.io/tutorials/picasso-image-resizing-scaling-and-fit
I am working on an android app and using REST APIs in it. I am receiving image urls from server and I want to show these images according to the original height of the image using image url.
How can I get the width and height of image using network image url. I have already tried getting bitmap from the string url but for this I want to perform this in background thread which leads the image loading very slow.
Is there any way I can get the image width and height from image url and can show the image in imageview according to the width and height.
Please help me if anyone know about this.
Thanks a lot in advanced.
URL url = new URL("url/image.jpg");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
for width:
bmp.getWidth();
for height:
bmp.getHeight();
for setting in imageview:
ImageView.setImageBitmap(bmp);
Further Reference:
https://developer.android.com/reference/android/graphics/Bitmap.html#getHeight%28%29
/* your library */
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
/* your code */
Glide.with(this)
.asBitmap() //get hieght and width
.load("link image")
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable
Transition<? super Bitmap> transition) {
Log.d("TAG", "onResourceReady: "+resource.getWidth()+"
"+resource.getHeight());
}
});
//////////////////////////////////////
Glide.with(this)
.asFile() // get size Image
.load("link image")
.into(new SimpleTarget<File>() {
#Override
public void onResourceReady(#NonNull File resource, #Nullable
Transition<? super File> transition) {
fab_full.setLabelText(""+resource.length());
}
});
I have app. I want to crop my picture that shown in imageview. I want crop with spesific length and width. can you help me? I have code but, that code crop direct from gallery.
I just want crop imageview with spesific length and width. Where length and width just input manual from user.
If you don't mind adding a new Library to your project just to do image loading for you, you can easily add Glide to crop your local drawables or images from the web.
In your build.gradle (module app)
compile 'com.github.bumptech.glide:glide:3.6.0'
You can crop the image with this below:
final ImageView myImage = (ImageView) findViewById(R.id.myimageview);
final int myWidth = 210;
final int myHeight = 80;
Glide.with(getApplicationContext())
.load(R.drawable.example_drawable) // you can add any drawable or url of an image here
.asBitmap()
.into(new SimpleTarget<Bitmap>(myWidth, myHeight) {
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
// Do something with bitmap here.
Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, myWidth, myHeight);
myImage.setImageBitmap(bm);
}
});