I am writing an application on android. I am downloading an image from the server and trying to set it as an ImageView background using setBackgroundDrawable.
Drawable drawable = Drawable.createFromPath(path)
imageView.setBackgroundDrawable(drawable);
It works but the image is resized (it is getting smaller). Does anyone have an idea about how to set an image view background without changing its size?
Edit:
I solved the problem by using:
File imgFile = new File(path);
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView.setImageBitmap(myBitmap);
imageView.setScaleType (ImageView.ScaleType.MATRIX)
Set image scale type to Matrix it will scale using the image matrix when drawing.
Use these two properties:
android:scaleType
android:adjustViewBounds
Study about them in : ImageView
Related
Hi Am trying to implement image mapping as like html in android. For that i referred this project. Its work good when i access the image from drawable-nodpi folder. But when i access from other drawable folder the image get scaled as per the android concepts. Here i used drawable folder for checking purpose only. Actually i get image from back end service.
I also tried with bitmap by setting the no density.
Bitmap bm;
bm=BitmapFactory.decodeResource(getResources(), R.drawable.blueprint);
bm.setDensity(Bitmap.DENSITY_NONE);
mImageMap.setImageBitmap(bm); //mImageMap is customized view which extends Imageview
But it's not working. Am also tried as per this post. Its not working.Is there any way to load image to image view without scaling?
you can use Bitmap.createScaledBitmap with the orignal mesaures of the picture..
int width = orignal bitmap width
int height = orignal bitmap height
Bitmap bm;
bm = getBitmap... from network
bm = Bitmap.createScaledBitmap(bm,width,height,true);
this will create a image scaled properly and just place it in the ImageView.
I am new in android development. I have an issue regarding the image scaling in my app. When i set images using setImageDrawable() images look proper like in screenshot:
But if i set image using setImageBitmap() method it does not look proper. Some black color occurs around it like in screenshot bellow:
I have tried it in multiple applications and i observed that in some applications the image size is becoming smaller than the size ofter setting image through setImageDrawable() or setImageResource().
the reason for setting images through setImageBitmap() is that i want to set images to the imageviews which are kept in assets folder and not in drawable.
Following is the code i used to create a bitmap and set it to imageview:
BitmapFactory.Options opts = new Options();
opts.inPurgeable = true;
try
{
Bitmap preview_bitmap = BitmapFactory.decodeStream(assetManager.open(drawableFolder+"/"+tmpArr[0]),null,opts);
view.setImageBitmap(preview_bitmap);
}
catch (Exception e)
{
LNLog.writeTOErrorToLog(e.getMessage());
}
So, please can anyone tell me where i'm i going wrong or missing something to set.
I tried to set Options but did not worked. I want to set images as in first screenshot that is how they looks by using setImageDrawable().
Thanx in advance.
Note: in screenshots images are referent in text on it only.IF i set hindi images i.e. the images on which text is in hindi language using setImageDrawable() then they looks proper as in like in screen shot one.So , no issue of image files.
You can get rid of the black borders by using
android:adjustViewBounds="true" in xml
or
imageView.setAdjustViewBounds(true) in Java.
EDIT:
Then try using drawable only
Drawable d = Drawable.createFromStream(assetManager.open(filePath), null);
view.setImageDrawable(d)
This worked for me :
try
{
view.setAdjustViewBounds(true);
int height = view.getDrawable().getIntrinsicHeight();
int width = view.getDrawable().getIntrinsicWidth();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPurgeable = true;
opts.inScaled = true;
Bitmap preview_bitmap = BitmapFactory.decodeStream(assetManager.open("drawable-mdpi/FAQ_hi_in.png",null,opts);
Bitmap bmp = Bitmap.createScaledBitmap(preview_bitmap, width, height, true);
view.setImageBitmap(bmp);
}
In this code I have taken the height and width of actual image and then scaled my bitmap to that height and width. Then I converted this scaled bitmap to drawable and set it into my ImageView, thats it. It resolved my problem of black border around image.
I have an ImageView. I am using imageView.setImageBitmap to set my image as background to ImageView. But it sets my image to ImageView's source (i.e. android:src) , but it doesn't set my image to ImageView's background (i.e. android:background).
When I use imageView.setImageBitmap, it looks like as if I used imageView.setImageResource not imageView.setBackgroundResource.
How can I handle this if I want to set my image to background using imageView.setImageBitmap. I know by I can do this by making custom ImageView. But is it possible without custom ImageView? If its possible, please let me know how to do it.
I have tested and it's done
I think you are getting Bitmap
So you have to convert Bitmap to BitmapDrawable
like
BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap)
then just set bitmap with below function
imageView.setBackground(ob);
by this way you can do it..
Try following code to set your own bitmap as background.
Bitmap bitmap = ...;
ImageView imageView = ...;
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setBackground(drawable);
try this..
Create drawable using bitmap & use setBackgroundDrawable(Drawable d) method for imageview.
Drawable d = new BitmapDrawable(getResources(),bitmap);
imageview.setBackgroundDrawable(d);
Please Use the following line to set image background.
imageview.setBackground(getResources().getDrawable(R.drawable.uporder));
Here uporder is an image resource present in your drawablefolder.
I prefer to use this because is not deprecated and it works in the lowers versions of android.
ImageView imageView;
Bitmap bitmap;
Drawable drawable = new BitmapDrawable(getResources(),bitmap);
imageView.setImageDrawable(drawable);
call setBackgroundDrawable withe BitmapDrawable param
I have this code:
<ImageView
android:id="#+id/listitem_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
and
imageview_logo.setImageBitmap(bit); // comes from assets
imageview_logo.setAdjustViewBounds(true);
imageview_logo.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageview_logo.setBackgroundColor(0x00000000);
imageview_logo.setPadding(0, 0, 0, 0);
imageview_logo.setVisibility(v.VISIBLE);
When loading the image this way, no scaling seems to have been done. However, if I load an image through setImageDrawable() r.res. the image inside the ImageView is resized. However, I need to use setImageBitmap() since I load my images through assets folder.
Am I missing some setting here? That will make Android resize and scale the bitmap, so it uses the full width of the ImageView? I guess I can do it myself in code, but offhand I would think what I want to do would be supported just by setting some properties.
Can't trust system for everything specially android which behaves very differently sometimes.
You can resize the bitmap.
height = (Screenwidth*originalHeight)/originalWidth;
this will generate the appropriate height.
width is equal to screen width as you mentioned.
Bitmap pq=Bitmap.createScaledBitmap(pq,Screenwidth,height, true);
As I needed a bit of time to figure out the complete code to make this work, I am posting a full example here relying on the previous answer:
ImageView bikeImage = (ImageView) view.findViewById(R.id.bikeImage);
AssetLoader assetLoader = new AssetLoader(getActivity());
Bitmap bitmap = assetLoader.getBitmapFromAssets(
Constants.BIKES_FOLDER + "/" + bike.getName().toLowerCase()
.replace(' ', '_').replace('-', '_') + ".png");
int width = getActivity().getResources().getDisplayMetrics().widthPixels;
int height = (width*bitmap.getHeight())/bitmap.getWidth();
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
bikeImage.setImageBitmap(bitmap);
With bit as your Bitmap image, you can directly set yout imageView and then crop it using the properties of the imageView as:
imageview_logo.setImageBitmap(bit);
imageview_logo.setScaleType(ImageView.ScaleType.CENTER_CROP);
I updated my build to build against Android 1.6, and now my bitmaps are scaled down on high density screens. I do NOT want this behavior. I gave this a shot:
http://blog.tomgibara.com/post/190539066/android-unscaled-bitmaps
but the images are STILL scaling, that is UNLESS I set them to a specific height & width. If I use wrap_content they are scaled down.
I have an image loader using the unscaled bitmap loader to create a drawable like so:
Bitmap bm = UnscaledBitmapLoader.loadFromResource(imageBufferInputStream);
drawable = new BitmapDrawable(bm);
which I later assign to an ImageView like so:
imageView.setImageDrawable( copyBitmapDrawable( (BitmapDrawable) drawable) );
In order to have an image not scaled when loading it from a resource (e.g. with BitmapFactory.decodeResource) you have to locate it in res/drawable-nodpi instead of the usual drawable, drawable-ldpi and so on.
Using
new BitmapDrawable(this.getResources(), bmp);
instead of
new BitmapDrawable(bmp);
should solve the issue.
Wrapping the bitmap with a Drawable is the problem. The Drawable scales the Bitmap. Instead of using a Drawable, assign the Bitmap to the ImageView directly using imageView.setImageBitmap(Bitmap).
Use ImageView.ScaleType. The CENTER constant preforms no scaling, so I guess that's what you are looking for.
By the way, do you use pixels or dips as size units? Using dips (density-independent-pixels) is a means of standardizing display on multiple resolutions. You'll find a couple of tips here.