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);
}
});
Related
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
}
});
}
I need to take an image from imageView and make it as a bitmap.
String s = getIntent().getStringExtra("ImageUri");
mImageViewFilter = (ImageView) findViewById(R.id.imageViewFilter);
Glide.with(this)
.load(s)
.into(mImageViewFilter);
Bitmap bitmap = ((BitmapDrawable)mImageViewFilter.getDrawable()).getBitmap();
You can see that into mImageViewFilter I am loading a photo with use of its URI and Glide library.
My next step is to take this photo which is in mImageViewFilter and make it as a bitmap. After the last line of code there is an exception. What am I doing wrong?
String s = getIntent().getStringExtra("ImageUri");
mImageViewFilter = (ImageView) findViewById(R.id.imageViewFilter);
To load image into ImageView use below code
Glide.with(this).load(s).into(mImageViewFilter);
So in order to get bitmap from ImageView (mImageViewFilter )
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
OR
You can add this before the code where you are trying to get Bitmap out of the ImageView
mImageViewFilter.setDrawingCacheEnabled(true);
In order to get the Bitmap out of the ImageView using DrawingCache, you first need to enable ImageView to draw image cache.
Bitmap bitmap = mImageViewFilter.getDrawingCache();
Actually you doing right thing. But the problem is Image loading is Asynchronous call.
And you called getBitmap() in right next line which will be called sequentially.
And it will give you null anyway cause image is not loaded yet .
Solution
if you want to do it in next direct step .You can use direct Bitmap callback.
Glide.with(this)
.asBitmap()
.load(path)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
// Use resource as bitmap save it for later.
}
});
First take it as bitmap, then take it again to apply to our ImageView :
Bitmap bitmap = Glide.with(this) //taking as bitmap
.load(s)
.asBitmap()
.into(100, 100) //width and height
.get();
Glide.with(this) //apply to imageview
.load(s)
.into(mImageViewFilter);
The straightforward way to obtain a bitmap from any View including ImageView is to get the drawing cache from it.
mImageViewFilter.buildDrawingCache();
Bitmap bitmap = mImageViewFilter.getDrawingCache();
But the above method may give you a low quality image.
I think you should directly decode the bitmap from the Uri using BitmapFactory
InputStream in = context.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(in);
in.close();
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);
}
});