imageView.setImageResource(int resId) vs. imageView.setImageBitmap(Bitmap bitmap); - android

The image file is identical. Once it comes from the drawable and one from the file system.
Here is the code.
<ImageView
android:scaleType="fitEnd"
android:id="#+id/partner_logo"
android:layout_width="51dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:contentDescription="#string/parter_logo"
/>
I would like to set the partner logo dynamically, so I load id from the webserver and then
set with imageView.setImageBitmap(Bitmap bitmap);
with the ImageResource it looks good and as I expect but in Bitmap case it is too small.
Here the picture to compare
The Logo (black arrow points) was set via setBitmap. It seem not to be up-scaled
--
The Logo (read arrow points) was set via setImageResource. It looks as I would like it too look like.
So the question is why there is a difference here? Works the scaleType in case of bitmap?

Because setImageResource() automatically scale your image.
If you want to use setImageBitmap() you need to scale up your bitmap to your desired size or using setAdjustViewBounds(true) that keep your aspect ratio.
Bitmap.createScaledBitmap(yourbitmap, width, height, false);
http://developer.android.com/reference/android/graphics/Bitmap.html

Related

larger bitmap how to fit into android device screen

I have confusion for the loading of the bitmap into android device screen or any digital device for example LED TV screen here my android mobile screen resolution like 768x1280 pixels and I want to load bitmap resolution like 3000x2000 pixels here may be two type of possibility arise first one directly load bitmap without and resizing of the original bitmap second one form original bitmap to take the resizable bitmap and load into device screen but question arise into the first case how to load larger bitmap into small device screen can anybody help me for this confusion.
For reference consider this image :
Here this image have resolution 3000x2000 pixels and mobile screen resolution 768x1280 pixels.
Use "picasso" plugin:
picasso page
Example:
Picasso.with(context).load("link or #drawable/...").into(imageView);
You can add .fit() and image will fill imageView.
Picasso.with(context)
.load("link or #drawable/...")
.fit()
.into(imageView);
Rotate image to 90 degree(either outside or inside XML), Then Fit with scaleType attribute.
Firstly either you can add rotated image to your resource or rotate it with XML attribute, like this.
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rotation="90"
android:src="#mipmap/your_image" />
Then, Scale it with scaleType attributes (fitCenter, centerInside or centerCrop)
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rotation="90"
android:scaleType="fitCenter"
android:src="#mipmap/your_image" />
Note: If you only give scaleType to your ImageView without rotation then your image will be stretched.

Image View doesn't show clear image defined in xml

In my code I want to set a default image for my imageview. All my images are of 174px X 174px. So, I want the default image to be same size. This is my xml for image view
//....code
<ImageView
android:id="#+id/ivCover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="Default Image"
android:src="#drawable/default_cover" />
//....some other code
Now, my default_cover.jpg is also 174px X 174px. But it shows a smaller image. I've tried all
android:scaleType="centerInside"
android:scaleType="center"
android:scaleType="matrix"
even when most of them were senseless for me but still I tried all of 'em & none of them works. Then I specified the height & width of image by specifying
android:layout_width="174px"
android:layout_height="174px"
(I know using dp is better than px but because this image size is fixed & I was just testing it). So it shows
[default image]
while when loaded from an external url using
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(url_source_for_image).getContent());
ivCover.setImageBitmap(bitmap);
desired image
[]
The two pictures may not look very different here but the difference is very clear on my android device. And please don't say that I didn't try other solutions..
Here is a list of links of so itself..
ImageView fit without stretching the image
Fit image into ImageView, keep aspect ratio and then resize ImageView to image dimensions?
ImageView one dimension to fit free space and second evaluate to keep aspect ration
Resizing ImageView to fit to aspect ratio
Maximum width and height for ImageView in Android
Image in ImageView is stretched - Android
Android: How to prevent image from being scaled in ImageView or ImageButton?
Sounds like the answer is to use drawable-nodpi. For future reference, if you have the same named image in both the ldpi/hdpi/etc and in no-dpi, I think the no-dpi one will be ignored.

Android bitmap rotate canvas crops/cuts image

bmpAndroidMarker = BitmapFactory.decodeResource(context.getResources(), R.drawable.t_move2);
bmpAndroidMarkerResult = Bitmap.createBitmap(bmpAndroidMarker.getWidth(), bmpAndroidMarker.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bmpAndroidMarkerResult);
tempCanvas.rotate(direction+45, bmpAndroidMarker.getWidth()/2, bmpAndroidMarker.getHeight()/2);
tempCanvas.drawBitmap(bmpAndroidMarker, 0, 0, null);
This it the code I have written (borrowed). The icon is generated within an imageview, inside a listview.
My problem is that on rotating this 'arrow', it seems that it 'clips' part of the far edges off, as if it were keeping the original bitmap's dimensions. I can't figure out how to allow it to 'overflow' and render the correct size image.
Is there some way of doing this?
You're rotating the Image by 45 degrees so the resultant Bitmap should be the width of the original plus the originals diagonal width (Pythagoras' Theorem should be able to help).
AFAIK you'll need to do that Maths yourself when creating the result Bitmap as that's the canvas which is being drawn upon, rotating it's contents will not rotate the container.
Try wrapping your ImageView in a FrameLayout and setting the frame layout's width and height as the size of the image you are trying to render. You can do this in the view's layout file like ...
<FrameLayout
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_width="YOUR IMAGE WIDTH"
android:layout_height="YOUR IMAGE HEIGHT">
<ImageView
android:scaleType="matrix"
android:layout_width="YOUR IMAGE WIDTH"
android:layout_height="YOUR IMAGE HEIGHT"
android:src="#drawable/YOUR IMAGE FILE" />
</FrameLayout>
... or in a programmatic way by building your FrameLayout with your specified dimensions then adding your ImageView as a subview.
If your image is bigger than the screen you can use android:scaleType="matrix" to keep it from scaling and ensure your aspect ratio is retained.
This was originally answered by Pavlo Viazovskyy here. Credit where credit is due!

What is the difference between src and background of ImageView

I am a puzzled about using src or background for an ImageView.
I know the former means the content of this ImageView and the latter means the background of the ImageView.
But how to decide which one to use? I don't see the difference.
All views can take a background image.
The src to an ImageView has additional features:
different scaling types
adjustViewBounds for setting bounds to match image dimensions
some transformations such as alpha-setting
And more that you may find in the docs.
when you use android:background, image will be set to fit on ImageView area(i.e according to width and height of ImageView). It doesn't matter if the image is smaller or larger than ImageView.
when you use android:src, then image will display in its original size. No
automatic scaling, adjustments will happen.
Note: Using android:src, we can get additional benefit of adjustViewBounds property
If you set an image to be the background of your ImageView, then the image will scale to whatever size the ImageView is. Other than that, src is a foreground image and background is a background image. Pretty much as it implies.
The difference between XML attribute src and background in ImageView:
The background will stretch according to the length given by the ImageView component, and SRC will hold the size of the original image without stretching. SRC is the picture content (foreground), BG is the background, can be used at the same time.
In addition: ScaleType only works on SRC; BG can set transparency, for example, in ImageButton, you can use Android:scaletype to control how the image is scaled, sample code as follows:
<ImageView
android:id="#+id/img"
android:layout_height="60dip"
android:layout_width= "60dip"
android:src="#drawable/logo"
android:scaleType="centerInside"
android:layout_centerVertical= "true"/>
Feel free to ask doubt if you get any.

Android AnimationDrawable scaling

I have an ImageView that initially has a Bitmap Image:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
By default that imageview scales the bitmap to fill the width that is what I want.
But the problem is that when I change the source of the ImageView to an AnimationDrawable, that is build by several images of the same size of the original, then animation isn't scaled at all and returns to the original.
How can I do that scale the animation drawable in the same way that is scaled the bitmap?
I dealt with this programmatically by using ImageView setPadding(). I calculated the difference between the ImageView height and width and what I wanted to display and set my own padding so that the usable size of the ImageView exactly matched my animation.
It's brute force and not very flexible but I've not seen any better solution posted and this issue has come up many times.

Categories

Resources