Use image sizes - android

Is there a way to use an image size when setting the sizes of a view? I want to create a TextView item with the exact height of a resource image, but I don't want to use the image as a background for the text view.

If you can get the height of the image, you can use TextView's setHeight. If you read in the image as a Bitmap, you can get the image's height with getHeight.

Related

Android Picasso load image to Bitmap variable cropping it to square

I need to load some images from URI to Bitmap variables and perform some operation with them togheter. I need the bitmaps to be squared images with fixes size, scaled down and cropped. By now I use this code:
return Picasso.with(c).load(imageUri).resize(size, size).get();
but, obviously, the image will be resized without keep its aspect ratio.
I want to resize the image with these requirements:
the smaller dimension (width or height) should be equals to size
the greater dimension should be cropped to size, keep image centered
The key is using centerInside after resize. See link
Picasso.with(c).load(imageUri).resize(size, size).centerInside().get()
set your imageview height and width fix inside xml and then set image to imageview like
Picasso.with(YourActivityName.this)
.load(imageUri)
.into(imageview_id);

Issue on setting the background image on group in expandable list view

I have a ExpandableListView and i am setting the background image when the group is expanded,
but issue occur that when i set the background image size is changed to current background of group,
any help ??
see below image
I think there is different in your Image size, so its changing height according your Image size as you would take WRAP_CONTENT in Height.
Do fix your height or make sure that both Image are with same height and width.

Imageview Background PNG Issue when set Wrap content

I have a LayoutView in a ScrollView and want to set a my glass background PNG image that should be scaled automatically to wrap content.
In these layout view, there is a text view and the contents of this text area is variable.
Usually without any PNG background the size of layout view is not extended if isn't necessary to view all the text set, but when I set PNG background the layout view automatically enlarged to the size of the PNG if I set wrap content on layout view.
How to set a custom background image scalable to fit the size of layout view that doesn't influence the wrap content behavior?
As per your requirement i would suggest you- take your background image of Height= approx 50 pixel width= any
- convert this image into nine patch follow this link- http://developer.android.com/reference/android/graphics/NinePatch.html
by using nine patch images your background will be re sized/adjust as per the dynamic data.
Hope it helps.
Thanks
In my opinion, you have to:
Set scaleType of Image view = "fitXY"
Set layout_height, layout_width of layout = "wrap_content"

What is the difference between ImageView.setBackgroundResource and ImageView.setImageResource?

I have seen these different approaches in setting images but I don't get the difference.
Why there two methods?
setBackgroundResource is for setting the background of an ImageView.
setImageResource is for setting the src image of the ImageView.
Given:
ImageView iv = new ImageView(this);
Then:
iv.setBackgroundResource(R.drawable.imagedata);
Will fit the image for the entire background. That means it will stretch the image to fill that background entirely even if the image size is too small.
imageView.setImageResource(R.drawable.imagedata);
Will occupy only the size of the image in ImageView.
For that you want to also set
android:layout_width="wrap_content"
android:layout_height="wrap_content"
for your ImageView. If the size of the image is smaller than the ImageView the remaining border will be left blank and the background will be shown.
SetBackdroundResource is for a drawable or color you want to set at the background of the imageview and your setImageResource is like to display on it.
so setImageResource is for add any resource to your imageview's front side. try this example and look at the difference. Android Gallery, ImageView Example
. This is a two layer effect,backside (setBackgroundResource) and frontside (setImageResource).
The method setBackgroundResource() belongs to all Views. The method setImageResource() only belongs to ImageView. You can set them both:
imageView.setBackgroundResource(R.drawable.sky);
imageView.setImageResource(R.drawable.balloons);
The setBackgroundResource() method will cause the image's width and height will be stretched to fill the size of the view. The setImageResource() method will let its image keep its aspect ratio.
My fuller answer is here.
setBackgroundResource sets the background image of an ImageView. The XML attribute is: android:background
setImageResource sets the image displayed in an ImageView. The XML attribute is: android:src

android image view problem

I have one question. I want to set one image to image button. I have one button image of size 90 x 48. I want to set this image on Image button.
Problems:
I need to set image on background parameter or src parameter?
If i set image on background parameter its not showing its actual size(smaller than actual size) if i have set the Layout width = wrap content and Layout Height= wrap content. If i give Layout width = 98dip and Layout Height= 48dip then its showing the same size. Is it a coorect way?
thanks
set image as background Parameter sometimes src makes problem
if you are set width and height as wrap_content then android will check the screen size and try to adjust to your screen if screen size is big then android displays your image as its actual height and width and if screen size is less then it will compress your image
so best way is to set height and width is as wrap_content if you have to use portrait and landscape mode both
Yes.
If you use Layout width = wrap content and Layout Height= wrap content, image will be as actual size. Maybe just the density of your monitor is less than the density of your phone?
Remember dip != pixels.

Categories

Resources