ImageView allow scale down, but not up - android

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

Related

How to get width and height of an image from Glide

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);
}
});

Get bitmap from imageview in viewpager with glide

I'm showing in imageview with glide. User can save image on button click.
Bitmap bitmap = ((BitmapDrawable)imageViewPreview.getDrawable()).getBitmap();
when i use this code it save whole screen image(Image and black space).
imageViewPreview.setDrawingCacheEnabled(true);
imageViewPreview.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
imageViewPreview.layout(0, 0,
imageViewPreview.getMeasuredWidth(), imageViewPreview.getMeasuredHeight());
imageViewPreview.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(imageViewPreview.getDrawingCache());
imageViewPreview.setDrawingCacheEnabled(false);
when is use this, sometime it work but image get cropped in half or image go to up like "android:layout_alignParentTop="true".
You can get Bitmap from DrawingCache. After Settting image in ImageView you can get it as below:
imageViewPreview.setDrawingCacheEnabled(true);
imageViewPreview.buildDrawingCache(true);
final Bitmap bmp = Bitmap.createBitmap(layout.getDrawingCache());
imageViewPreview.setDrawingCacheEnabled(false);
If you need full size image then this is not right way i think .A better solution would be to get it From Glide as Bitmap . If full image was loaded already it will be quick (from cache) if not then it will load the full image from server or you can still use Drawing cache in case of No internet connection and glide failure.
Glide.with(imageView.getContext())
.asBitmap()
.load("imageUrl")
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
// Use resource here
}
});
}

Blur thumbnail image before loading actual image in picasso

i am using picasso to display the image from URL, i am displaying thumbnail image first before loading actual image, i want to blur that thumbnail image , how i can achieve in picasso ?
here is my source code
pb.setVisibility(View.GONE);
Picasso.with(getApplicationContext())
.load(thumbUrl) // thumbnail url goes here
//.placeholder(R.drawable.progress_animation)
.resize(width, width)
.transform(new BlurTransformation(getApplicationContext(), 25, 1))
.into(imageview, new Callback()
{
#Override
public void onSuccess()
{
pb.setVisibility(View.GONE);
Log.e(TAG,"OnSuccess");
Picasso.with(getApplicationContext())
.load(url) // image url goes here
.resize(width, width)
.placeholder(imageview.getDrawable())
.into(imageview);
iv_reDownload.setVisibility(View.GONE);
}
#Override
public void onError()
{
pb.setVisibility(View.GONE);
Log.e(TAG,"OnError");
Picasso.with(getApplicationContext())
.load(url) // image url goes here
.resize(width, width)
.placeholder(imageview.getDrawable())
.into(imageview);
iv_reDownload.setVisibility(View.VISIBLE);
}
});
To my knowledge Picasso currently doesn't support this "feature". Glide library, which has very similar usage, does have a function that enables easy usage of "fast preview", that is low resolution thumbnail which is loaded faster than full size image.
Function is thumbnail(float) and accepts scale factor as argument, where for example 0.1f is one-tenth of original image size.
Case usage with Glide could be something like this.
Glide.with(mFragment)
.load(wFile.getUriForThumb())
.override(length, length)
.thumbnail(.05f)
.placeholder(R.drawable.placeholder_image_128px)
.into(holder.mImageThumb);
Where method override is an close equivalent to resize(int,int) in Picasso.
For complete comparison between two libraries you can check this nice guide: Glide vs. Picasso
Blur Transformation in Picasso, Reference: https://futurestud.io/tutorials/picasso-image-rotation-and-transformation
#Override
public Bitmap transform(Bitmap bitmap) {
// Create another bitmap that will hold the results of the filter.
Bitmap blurredBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
// Allocate memory for Renderscript to work with
Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
Allocation output = Allocation.createTyped(rs, input.getType());
// Load up an instance of the specific script that we want to use.
ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setInput(input);
// Set the blur radius
script.setRadius(10);
// Start the ScriptIntrinisicBlur
script.forEach(output);
// Copy the output to the blurred bitmap
output.copyTo(blurredBitmap);
bitmap.recycle();
return blurredBitmap;
}
finally, i solved my problem with the help of this Library named as picasso-transformations , i added the below dependencies in my project
compile 'jp.wasabeef:picasso-transformations:2.2.1'
not only it support Picasso but also it support Glide or Fresco too
and i called the BlurTransformation class inside picasso.
here is complete example
pb.setVisibility(View.GONE);
Picasso.with(getApplicationContext())
.load(thumbUrl) // thumbnail url goes here
//.placeholder(R.drawable.progress_animation)
.resize(width, width)
.transform(new BlurTransformation(getApplicationContext(), 25, 1))
.into(imageview, new Callback()
{
#Override
public void onSuccess()
{
pb.setVisibility(View.GONE);
Log.e(TAG,"OnSuccess");
Picasso.with(getApplicationContext())
.load(url) // image url goes here
.resize(width, width)
.placeholder(imageview.getDrawable())
.into(imageview);
iv_reDownload.setVisibility(View.GONE);
}
#Override
public void onError()
{
pb.setVisibility(View.GONE);
Log.e(TAG,"OnError");
Picasso.with(getApplicationContext())
.load(url) // image url goes here
.resize(width, width)
.placeholder(imageview.getDrawable())
.into(imageview);
iv_reDownload.setVisibility(View.VISIBLE);
}
});
I have tested both Glide and Picasso for fast loading if you resize your image in Picasso the it will improve your loading speed, here is the code
Glide.with(context)
.load(sArr.get(position).getThumbnail())
.thumbnail(0.01f)
.override(100,100)
.centerCrop()
.placeholder(R.drawable.default_picture)
.into(holder.imgItem);
Code for Piccaso
Picasso.with(context)
.load(sArr.get(position).getThumbnail())
.resize(100,100)
.placeholder(R.drawable.default_picture)
.into(holder.imgItem);
and Picasso win !!
Not sure if this will work but I have seen before that you can use:
.add(new ImageTransform(IMAGE_URL, new BlurTransformation(this)));

How to load only part of image with Glide from Firebase?

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 to get image width and height from a network url

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());
}
});

Categories

Resources