Image view resizing - android

So I have been facing this problem for a while and still I cant find solution for it. Hopefully you guys can give me a hand in this. I have a cards game that a player can see his cards like any other cards game.
The problem is that a lot of people complained that cards are too small.When I run it on my devicr (galaxy s2) the cards size are good. By chance someone sent me a screenshot from his HTC and the cards are indeed smaller than What I have.
Please note that I have this 3 different density folders and my Images view are set at wrap content.
I Have 2 solutions in mind and I appreciate if you can help further
1) instead of wrap content, I user a predefined width for each screen size (not density).This will garaunter a specifc size (space it occupies)on large screens for example
2) Make the imageview size relative to the width of the screen.I dont know how to do it so any pointers are appreciated.
3) any suggestions?
please help a programmer stuck in this delemma

You can use DesplayMetrics to get the dimensions of the device at run time and use the width and height you get.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
ImageView iv = (ImageView)findViewById(R.id.yourImageViewId);
iv.setWidth(width);
iv.setHeight(height * 2/3);
or something like that. this way you will never get such issues.

Do you have images in different sizes for different densities? Which scale type are you using for ImageViews?
You can create different layouts for densities but I would start with experimenting with scale types - http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

Related

Getting the dimensions of an androids screen

Basically I'm trying to make an application for tablets.
There going to be a bunch of widgets added and removed by the user inside a table layout. It needs to be dynamic so that the widgets are sized differently based on the width and height of the table layout.
To do this, I'm trying to make a 3x3 grid in a tablelayout. As I'm sure you guessed, each square has 33% width and height of the tablelayout.
This is what I got:
DisplayMetrics metrics;
int totalScreenSizeH;
int totalScreenSizeW;
TableLayout contentTable;
LinearLayout contentLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Get the UI detail
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
totalScreenSizeH = metrics.heightPixels;
totalScreenSizeW = metrics.widthPixels;
contentTable=(TableLayout)findViewById(R.id.tableLayout1);
contentLayout=(LinearLayout)findViewById(R.id.contentLayout);
int tableWidth =contentTable.getWidth();
int tableHeight=contentTable.getHeight();
int layoutWidth=contentLayout.getWidth();
int layoutHeight=contentLayout.getHeight();
Log.v("Table Width",""+tableWidth);
Log.v("Table Height",""+tableHeight);
Log.v("Layout Width",""+layoutWidth);
Log.v("Layout Height",""+layoutHeight);
So just to explain this a bit better. The linearLayout contains the tablelayout which has 3 table rows. I figured that I would be able to get the width and height of these but for some reason, both are coming back as 0 in size. One I have the width and height dims, Im going to save that variable and the create the widgets based on those variables/3.
So 2 questions:
1: is this the right way to handle dynamically sized widgets. Or is this the wrong way to handle a problem like this?
2: Any ideas why those 2-4 variables are all coming back as 0?
I would advise against doing pixel-perfect designs on android, due to the vast amount of resolutions and densities you have to deal with.
My suggestion would be to look at android:layout_width/height="wrap_content" and android:layout_weight="INTEGER" that will help you make floating 3x3. You can also achieve this with a GridView
I linked to the source in a comment to the OP, but for the 3x3 Grid, follow some practices Roman has done with his custom DashboardLayout implementation (Apache License FTW). It will correct for horizontal and vertical spacing on various screen dimensions: https://gist.github.com/882650.
If it doesn't fit your use case, I highly suspect you can modify it to do so.

How to calculate the Resolution According to some specific resolution?

In My Application i have One button like this :
The Resolution of that button is 192x32. And when i put this button in to drawable-mdpi, it seems good to layout. Now for other screen resolution for multiple screen support which size of button i have to make to see the Good Layout Design according to other Devices screen ?
I mean for drawable-ldpi and drawable-hdpi, which resolution i have to make for this button ? How to do Such calculation for to make this button size to fit for all the screen size ?
Please help me for this.
Thanks.
No need to create multiple buttons for multiple screen support.
Instead create a single button and set the width and height at run time.This is achieved by getting the display width and height. Use the bellow code to get the display H & W values.
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
based in the above values set the button width and height at runtime.
Example:
Button bt=(Button)findViewById(R.id.button);
bt.setWidth(width);//screen width(fill_parent)
bt.setHeight(height/6);//1/6 of the screen height
The above code set the button width to screen(display) width size and height to 1/6 of the screen.
There is much information on this subject on the android developers website. In particular, there is a list of the various dpi levels and what range of DPIs that they correspond to.
Also, Rather than providing different images for different resolutions, you could make the image a nine-patch, and have it auto-magically expand to fit the button. Although if you want to keep the highlighting in the background proportional, it might be somewhat difficult to make it expand vertically.

Android SurfaceView width/height

I've made a couple of apps for android, but none used graphics that much. I'm now trying to make an actual game. I have some bitmaps that are on my canvas. My problem is I'm not sure where to initialize their positions. and in OnTouch events I want to make sure the bitmaps don't stray outside the screen.
For example, initializing one bitmap:
canvas.drawBitmap(glider.getGraphic(), 30, 20, null);
20 and 30 were arbitrary. But I want to be able to write something like screen.getWidth() so I know exactly where the bitmap is relative to the border of the screen. I can't find this in the developers reference.
If you're just looking to get the screen dimensions, take a look at the DisplayMetrics class. You can get an instance and the measurements like so:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
int screenWidth = metrics.widthPixels;
Keep in mind this is the absolute screen size. If you're doing a fullscreen game, this will work fine, but if not, you have to keep in mind the size of the notification bar, and possibly the title bar. It might be better in that instance to just get the height of the SurfaceView itself and use it for your calculations.

Handling different screen resolutions for background images in Android

My Android app (a text-based game) uses background images a lot in order to provide a better visual ambiance. For example, if the action in the game takes you into a tavern, then you get a background image of a tavern in the game. This is a vast improvement, graphically, over the boring black background that you would otherwise get.
It is also a problem, however, since android:background always stretches to the dimensions of the screen. The result is that the background images look very bad if the player switches between portrait and landscape mode. And to make matters worse, many devices have very different aspect ratios (e.g., 320x480 mdpi, 480x800 hdpi, and 480x852 hdpi) and even more variations are coming.
How do others solve this problem? Having individual images for the main resolutions/orientations is not an option for me, as this would result in the apk growing too large.
The first step is to get the screen height and width of the device itself, then stretch/shrink or pad your bitmap as needed. This SO answer has source that should help, copied below. Props to Josef for original answer.
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Here's generic source on getting the orientation.
int orientation = display.getOrientation();
or some more involved source from here (a little messy but looks correct).
public int getscrOrientation()
{
Display getOrient = getWindowManager().getDefaultDisplay();
int orientation = getOrient.getOrientation();
// Sometimes you may get undefined orientation Value is 0
// simple logic solves the problem compare the screen
// X,Y Co-ordinates and determine the Orientation in such cases
if(orientation==Configuration.ORIENTATION_UNDEFINED){
Configuration config = getResources().getConfiguration();
orientation = config.orientation;
if(orientation==Configuration.ORIENTATION_UNDEFINED){
//if height and widht of screen are equal then
// it is square orientation
if(getOrient.getWidth()==getOrient.getHeight()){
orientation = Configuration.ORIENTATION_SQUARE;
}else{ //if widht is less than height than it is portrait
if(getOrient.getWidth() < getOrient.getHeight()){
orientation = Configuration.ORIENTATION_PORTRAIT;
}else{ // if it is not any of the above it will defineitly be landscape
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
}
}
}
return orientation; // return value 1 is portrait and 2 is Landscape Mode
}
I would suggest you get the width and height of your current view. Right now I am not sure what functions will give you these values but I am sure it is possible. With that you can then calculate an aspect ratio. I'd make the images something like 1000x1000 and just show a certain part of the image, so that the aspect ratio will not be wrong.
I have the same problem with the camera. So my solution was to pick one of the two values, width or height, as fixed, and calculate the correct other value according to the aspect ratio. I'm not sure if there are functions already to just show a part of the image, but I am sure you could write something to copy just a part of the image and show that part.
The drawback is of course that you will be showing just a part of the background. Since the general aspect ratio of the phones is however somewhere between 1:1.3 and 1:1.8 I guess it wouldn't be a too big problem. I for one would rather see the part of an image in the correct way, than looking at an ugly stretched image.
Maybe you could create your background images as NinePatch images so that you are in better control of how it stretches?
Otherwise, you can just prevent your images from stretching (or at least keep the correct aspect ratio) by setting the scaleType attribute e.g. android:scaleType="center".

Creating a grid of scaled ImageViews [Android]?

I need to make a grid whose width and height is the same as the screen. I guess I would somehow use
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
to do this but I need to scale the images inside so that each ImageView is 1/10th of the width of the screen (with 10 images for example). Similarly for the image heights.
I have tried
a = (ImageView) findViewById(R.id.imageID);
a.setMaxHeight(metrics.heightPixels/numberOfImages);
But as you can imagine, this is very tedious with 130 images (and not to mention, it doesn't work!) It wouldn't be a problem if I didn't need to cater for lots of screen sizes!
Any help is appreciated! (Bit of an Android noob (-: )

Categories

Resources