I got a horizontally oriented LinearLayout with an ImageView and a TextView. TextView has a text of 1- or 2-digits number.
What I need is to set text size of a TextView to be exactly same in a hight of an ImageView's image.
I know I can get ImageView's Image height , but how can I make textSize as high accordingly?
View.getHeight():
Return the height of your view. Returns
Returns
The height of your view, in pixels.
TextView.setTextSize(float):
Set the default text size to the given value, interpreted as "scaled
pixel" units. This size is adjusted based on the current density and
user font size preference.
Parameters
size The scaled pixel size.
This basically means you need to convert the height (px) to text height (sp):
float density = getResources().getDisplayMetrics().scaledDensity;
float textHeight = imageHeight / density;
Give constant height-width to both(image view and textview)... or try scaleType="fitxy"...
You can try:
float height = imageView.getHeight();
TextView tv = new TextView(context);
tv.setTextSize(height)
I'm not sure this will do exactly what you want, but it is the most obvious starting point I see.
well, I've found myself the following :
first we need to find a real height of an ImageView's Image - it's not as high as the ImageView, so it's explained well here : How to get the width and height of an android.widget.ImageView?
next we set the height of the text :
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
imageView.getMeasuredHeight()*(imageView.getMeasuredHeight()/
imageView.getMeasuredWidth()));
which means we are setting textSize in raw pixels, then we take height and multiply it with the picture's ratio of height to width so TextView's text size will be exactly high as the image
Related
I'm using a (horizontal) PagerView which shows 1 page on the screen, the user can switch pages by swiping left or right.
Every page holds several views in a RelativeLayout, one of them is a description text in a TextView (its size is calculated with weight).
I want the description text to be in the same font size for all pages.
This needs to be according to the longest description (the TextViews in all the pages are in the same height).
I have an array of all the descriptions in advance but I don't have rendered TextViews since they are in different pages and android won't draw it until scrolled to the page.
Any idea how the correct font size could be calculated?
Found the solution so just sharing.
This is easily implemented using StaticLayout. When creating StaticLayout you assign its width and can check its height (it will automatically start the text in a new line if it's too long).
Just iterate over your "texts array", for every line of text create a StaticLayout with your desired width and make sure it's height fits inside your TextView height, if not, decrease the font size and recheck. You'll eventually get the largest font size that fits the TextView size.
Get the 1st textview text size & typeface. Then set that size and typeface to other textviews. Or you can get height & width of the textview & set that to others.
float size = textView1.getTextSize(); //get the size of the textview text
textView2.setTextSize(size) //set the size for other textview's texts
//if you want look in same size, all text must have same TypeFace(Font). To do that use.
textView1.getTypeFace();
textView2.setTypeFace();
//or you can get hight & width of the textview.
textView.setText("GetHeight");
textView.measure(0, 0); //must call measure!
textView.getMeasuredHeight(); //get height
textView.getMeasuredWidth(); //get width
//set textview height & width
LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.height = 70;
params.width = 100;
textView.setLayoutParams(params);
Having some imageViews in a list row.
The count of the imageView in the row are different, i.e. could be three or two imageView.
For the width it could be specified using
layout_width="0dp"
layout_weight="1dp"
for every imageView in the row. But the problem is the height, it should be 90% of the width of the imageView if the row has two imageView, and 80% of the width is the row has three imageView.
Is there way to do that in xml?
If not, where is the best place to do calc and set the height without too much overhead on the drawing?
Thanks!
Not sure if there is a way to do in xml. But this works in code, do it in the adapter::getView(), after get the corresponding imagView, change its height layoutParam but keep the width one.
Point size = new Point();
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getSize(size);
int screenWidth = size.x;
int columnNumber = (isItThree ? 3 : 2);
double ratio = (isItThree ? 0.8 : 0.9)
int desiredImageHeight = screenWidth/colunmNumber * ratio;
ViewGroup.LayoutParams imglayoutParams = imgView.getLayoutParams();
imglayoutParams.height = desiredImageHeight;
imgView.setLayoutParams(imglayoutParams);
In order to maintain the ratio between height and width the best practice is:
Defining one of its property with a value in dp and another with wrap content.
In your case:
android:layout_width="20dp"
android:layout_heignt="wrap_content"
By doing this the height should be adjusted according to the ratio of given width.
I'm creating a bitmap wich i need to use as background of a 320 width screen. The problem is that the bitmap is not getting the width i want. It is getting two empty spaces on the left and on the right. It is because it is fitting the height of the screen, but i dont want that, i want to force the bitmap to have the width i want. Doesn't matter if a portion of the height of the bitmap is out of the screen.
i need to force the width when i am adding the image to the layout, i mean that the image must be keep the aspect ratio, but the image must be higher than the height of the screen, and the image must be shown incomplete if the heigh of the image is higher than the height of the screen. Now this is not happening, because the layout is forcing the width to respect the image and show all the height of the image in the height of the screen, then i think the problem is on the layout rules
im creating the bitmap with:
View view;
Bitmap aux = Util.loadImage( filename ); //image loaded but with his original width
Bitmap image = Util.scaleBitmap(aux, 320); //scaling to 320
((ImageView)view).setImageBitmap( resource.image );
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams( 320 , h );
rlp.addRule( RelativeLayout.CENTER_HORIZONTAL );
rlp.addRule( RelativeLayout.ALIGN_PARENT_TOP );
layout.addView( view , rlp );
What am i doing wrong?
Try using this:
((ImageView)view).setScaleType(ImageView.FitCenter);
To see more options see http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType
Since you don't care about the height you should use CENTER_CROP.
Define the scale type and then set the image.
CENTER_CROP - Scale the image uniformly (maintain the image's aspect
ratio) so that both dimensions (width and height) of the image will be
equal to or larger than the corresponding dimension of the view (minus
padding).
I'd like to scale an image up to take up the entire size of an ImageView. This is subtly different than using scaleType=fit_center because fit_center will leave bands around the image if the image aspect ratio does not exactly match the ImageView's aspect ratio. Instead, I would like the image to get centered and scaled up to completely fill the enclosing view, with any excess chopped off.
I'm able to accomplish this by computing my own custom image matrix during onCreate():
final Display display = getWindowManager().getDefaultDisplay();
final float screenWidth = display.getWidth();
final float screenHeight = display.getHeight();
final float imageWidth = splashView.getDrawable().getIntrinsicWidth();
final float imageHeight = splashView.getDrawable().getIntrinsicHeight();
final Matrix splashMatrix = new Matrix();
final float scale = Math.max(screenHeight/imageHeight,screenWidth/imageWidth);
splashMatrix.postScale( scale, scale );
splashView.setImageMatrix(splashMatrix);
This works fine, but it seems like there must be an easier away. Does anyone know of a way to scale up an image in an ImageView while both preserving the aspect ratio of the image and fully filling in the ImageView?
(Note: in my case my ImageView is taking up the full screen, so I use getWindowManager().getDisplay() to find the desired image size. You can't use splashView.getWidth()/getHeight() because the view hasn't been laid out yet and doesn't have a size)
You can use android:scaleType="centerCrop".
Keeps the aspect ratio and scales the image just like you want it.
For more information please go through the below link
http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType
In some cases all you need is
android:adjustViewBounds="true"
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
This worked for me to fit the image inside the whole image view, plus it make sense that it says "ScaleType.FIT_XY" to fit the X and Y axis of the imageView.
From the xml file it would be:
android:scaleType="fitXY"
Try scaleType attribute for your ImageView. http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType
I am very late but hope it helps in the future. Here is my solution to how to stretch the image to full screen or image view without scaleType="fitXY" as it causes damage to the image. Use the following library.
[A simple imageview which scales the width or height aspect with the given ratio]
https://github.com/santalu/aspect-ratio-imageview
To Find the aspect ratio of the device screen use the following Code.
DisplayMetrics metrics =this.getResources().getDisplayMetrics();
float ratio = ((float)metrics.heightPixels / (float)metrics.widthPixels);
you must set android:scaleType="centerCrop" to fill entire screen
but you noticed that scaletype property worked together with android:layout_width and android:layout_height property.
you must set to match_parent
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.