Android scaling imageview from setImageBitmap - android

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

Related

how to scale image and set background efficiently android

I want to start a new project and i was wondering how to set the background for my activity (layout) very efficiently
Here is what i tried in the onCreate method :
String imageUri = "drawable://" + R.drawable.perfectwall;
int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();
Bitmap reqBitmap = loadBitmap(imageUri, width, height);
Drawable drawable = new BitmapDrawable(this.getResources(), reqBitmap);
r.setBackground(drawable);
also i have a question why this is not working... when i launch the app on the emulator it does not change the background although the memory is getting higher..up to 12MB used.. it is just a white screen like i didn't set the background
Thank you very much !
Consider using libraries like Picasso and Universal Image Loader. They've been built precisely for what you want: to scale and load large images into imageviews efficiently. I've used both in the past and vouch for both of them.

Android avoid image scalling in Imageview for Image Mapping like Html

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.

Images scales defferently in setImageDrawable() and setImageBitmap()?

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.

android.widget.ImageView scaling produces ugly results

I'm trying to scale an image in an ImageView on Android, but no matter what settings I try, the scaled-down image always looks ugly. Here is an image produced by my code:
http://i.imgur.com/4yubR.png?1
The image on the left is pre-scaled using ImageMagick on my desktop computer. ImageView has no problem showing this image. The image on the right is the one that is actually scaled by the ImageView, and as you can clearly see, it looks quite ugly :/
The code that produces both of the above images is as follows:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
RelativeLayout body = new RelativeLayout(this);
for(int i = 0; i < 2; ++i)
{
ImageView iv;
iv = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(34, 34);
params.leftMargin = i*50;
body.addView(iv, params);
AssetManager am = this.getAssets();
try
{
BufferedInputStream buf;
if(i == 0)
buf = new BufferedInputStream(am.open("images/search_34x34.png"));
else
buf = new BufferedInputStream(am.open("images/search.png"));
Bitmap bitmap = BitmapFactory.decodeStream(buf);
BitmapDrawable bd = new BitmapDrawable(bitmap);
bd.setAntiAlias(true);
iv.setImageDrawable(bd);
buf.close();
}
catch(java.io.IOException e)
{
}
}
setContentView(body);
}
Please tell me if I am missing something. I just cannot believe that Android scales images this badly (actually, I find it hard to believe that this is the default behavior).
Also note that I do NOT want to pre-scale the images, as my app has to deal with images from unknown origin, so I have to scale on the fly.
I don't see where you are scaling down your bitmap. I think you are trying to jam the high resolution bitmap in to a ImageView with smaller dimensions.
Here is a document on scaling the bitmaps. The technique described works well with scaling the bitmaps on the fly also.
Maybe You have to set the scaleType inside Your xml Layout. For example
<ImageView
android:scaleType="center"
</ImageView>
There are following scaleTypes:
CENTER
CENTER_CROP
CENTER_INSIDE
FIT_CENTER
FIT_END
FIT_START
FIT_XY
MATRIX
You could see about here:
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
EDIT
Tried some stuff and now I get the same result even with other pics. I recognized, that when I am scaling down an image inside a mobile phone, all looks good. I used for example an imageView with 120x120 dp and a resource with 300x300px. If I use the same imageView on an tablet, even 120x120, and the same resource image with the same size, it looks very bad. I think the only way to get rid of this is to scale the image outside your app to the needed sizes before setting it to Your project. For Example, if You use an imageView with 200x200dp, scale the image near to this size. If You support multiple screen sizes, and you set the sizes of this imageView for a tablet to 500x500dp, scale Your image near to this size. Sure, this only will work good, if You got an Image that has a higher resolution than the biggest imageView.
I Know this no satisfying answer, but this is the only way I see for this problem.

Drawing a large number of ImageViews with the same Bitmap but different size

I am trying to draw multiple ImageViews inside a single LinearLayout.
All the ImageViews need to have a single bitmap.
The ImageViews will only vary in size.
The single bitmap will not be resized.
The simple way is to create one Bitmap per ImageView. But, this runs out of memory quickly.
final Bitmap placeholderBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
final Canvas canvas = new Canvas(placeholderBitmap);
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.placeholder_image), 0, 0, null);
imageView.setImageBitmap(placeholderBitmap);
linearLayout.addView(imageView);
I also tried setting the max and min height and width, no effect. The images remain the same size.
imageView.setImageResource(R.drawable.ic_no_image);
imageView.setMaxHeight(imageViewInfo.height);
imageView.setMaxWidth(imageViewInfo.width);
I believe working with Drawables is the right "Android" way to do it, but I can't find a way to dynamically create a Drawable with the right side and layering the shared bitmap into it.
Just use one Bitmap -- as you say they will all use the same Bitmap, so no duplication is necessary.
If you want the ImageViews to have different sizes and have the Bitmap scale itself to the size of the ImageView then use the setScaleType or android:scaleType attribute to set the scaling of the ImageView. For instance FIT_START maintains the aspect ratio of your image and tries to fill the ImageView starting from the top-left corner.
I don't see any need to create a Drawable, though if you need to you can just create one from a Bitmap using:
Drawable d = new BitmapDrawable(bitmap);
ImageView actually does this automatically when you call setImageBitmap(...).
ImageView will always resize the bitmap to it need proportions.
If you want to save memory, resize the bitmaps manually and set them into the ImageViews this should stop the ImageView from resizing it internally - to make sure, you can set the layout_width and layout_hieght of the image view to 'wrap_content'.
I think may be looking for a ClipDrawable.
You would set the android:drawable property of the clip XML to your bitmap
That or you can do it with the Java code also given in the example

Categories

Resources