In My app I have to do some resolution specific work and I have to get the resolution and send it to server accordingly. Now My server have images which entertains the following resolutions...
320 * 480
480 * 800
800 * 1200
720 * 1280
1080 * 1920
1440 * 2560
I am getting resolutions in the following way
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
so As we know there is so may resolutions so there would be some devices whose resolutions slightly different from the set of resolutions I have mentioned above such as I have a device which has 800px * 1172px
waht I want
So tell me how can I compare the device resolution with the set of
resolution and send the resolution which maintain the resolution
categorized images.
also if the device has resolution which is not exist in my pre determined set of resolutions , then I want to send the resolution closer to it.
Please help me , I do not know how to do it.
800px * 1172px
you are aware that this is actually a 800x1200 display? The way you get the screen dimensions takes the device's on-screen navigation bar into account. See this and similar questions if you need to take the navigation bar into account.
I want to send the resolution closer to it.
Possible solution that uses arrays of your supported dimensions and finds the closest value to your passed width/height:
final int[] supportedHeight = {320, 480, 720, 800, 1080, 1440};
final int[] supportedWidth = {480, 800, 1200, 1280, 1920, 2560};
public static int getClosest(int n, int[] values){
int dst = Math.abs(values[0] - n);
int position = 0;
for(int i = 1; i < values.length; i++){
int newDst = Math.abs(values[i] - n);
if(newDst < dst){
position = i;
dst = newDst;
}
}
return values[position];
}
Log.d("h", getClosest(1337, supportedHeight)+""); //1440
Log.d("w", getClosest(1172, supportedWidth)+""); //1200
Related
I attempted to make a simple app to see if I could make an element a fixed size, in this case 4cm wide, regardless of device. To do this I tried to use the screen xDPI, use that as an inch, multiply that by a constant to get 4cm. It doesn't appear this is working after trying it on a few different devices.
final WindowManager w = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
final Display d = w.getDefaultDisplay();
final DisplayMetrics m = new DisplayMetrics();
d.getMetrics(m);
//find the correct x density and multiply it by 1.5748 to get 4cm
float density = m.xdpi;
density = (float) (density * 1.5748);
int intDensity = (int)Math.ceil(density);
//now set the view to be the dimensions that you want, it must be inside of it's own layout?
box.setLayoutParams(new LinearLayout.LayoutParams(intDensity,100));
Any suggestions?
I had developed an application. Which is suitable for large screen tablet and medium screen mobile devices.
But in Micromax Canvas series it takes resoulation as a tablet because those type of devices are of large resolution.
My device have screen of around 4" But when i run at code at my device it consider the size as 5.81". May be it is because of high resolution?
Can anyone tell the solution?
My code for checking the screen size is.
int orientation = getWindowManager().getDefaultDisplay().getOrientation();
screen_size = global.getScreenSize();
Toast.makeText(getApplicationContext(), "Screen Size:" +screen_size, 500).show();
if(screen_size>=5.1)
{
if (orientation==0)
setContentView(R.layout.login_large);
else if (orientation==1)
setContentView(R.layout.login_large_land);
else if (orientation==2)
setContentView(R.layout.login_large);
else if (orientation==3)
setContentView(R.layout.login_large_land);
}
When i run the application it runs succesfully in tablet and Samsung devices but it doesnt run on Micromax Canvas series.
My Global Variable coding is
public double getScreenSize()
{
double size = 0;
try
{
DisplayMetrics dm = getApplicationContext().getResources().getDisplayMetrics();
float screenWidth = dm.widthPixels / dm.xdpi;
float screenHeight = dm.heightPixels / dm.ydpi;
size = Math.sqrt(Math.pow(screenWidth, 2) + Math.pow(screenHeight, 2));
}
catch(Throwable t){}
return size;
}
There's a couple of things here I think you can do here that will both improve code quality and most likely fix the issue you are seeing:
Instead of detect orientation and load in the XML file, you should place the same XML file in different folders (layout/layout-land)
Read through the documentation here, you might decide to go with sw600dp instead of 'large'
It's bad practice to do it in Coding Way because some of device doesn't returns exact physical screen size but here is your solution to the problem
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(dm.widthPixels/dm.xdpi,2);
double y = Math.pow(dm.heightPixels/dm.ydpi,2);
screen_size = Math.sqrt(x+y);
Log.d("debug","Screen inches : " + screen_size);
then you could do what you want
if(screen_size>=5.1)
{
if (orientation==0)
setContentView(R.layout.login_large);
else if (orientation==1)
setContentView(R.layout.login_large_land);
else if (orientation==2)
setContentView(R.layout.login_large);
else if (orientation==3)
setContentView(R.layout.login_large_land);
}
I'm trying to dynamically build a layout, so it scales and looks more or less identical on all screen sizes and resolutions. I do this by programmatically placing my views inside a RelativeLayout and scaling everything according to a certain factor.
I noticed something strange when moving an ImageView around though, the bigger the values on margins get, the smaller the ImageView gets. This gets really annoying when trying to get things at right place and size.
private void initializeObjects() {
Display display = getWindowManager().getDefaultDisplay();
Point screenSize = new Point();
display.getSize(screenSize);
float scale = screenSize.x / ( (screenSize.x < screenSize.y) ? 480 : 800 ); // Scale according to Galaxy S II resolution
RelativeLayout screenLayout = (RelativeLayout) findViewById(R.id.main_layout);
mRobotView = new ImageView(getApplicationContext());
mRobotView.setImageResource(R.drawable.soomla_logo_new); // a 743 x 720 png
mRobotView.setScaleX(scale * 100 / 743); // Make sure the image is 100 "units" in size
mRobotView.setScaleY(scale * 100 / 720);
mRobotView.setPivotX(scale * 100 / 743); // reset the image origin according to scale
mRobotView.setPivotY(scale * 100 / 720);
RelativeLayout.LayoutParams robotParams = new RelativeLayout.LayoutParams(743, 720);
robotParams.leftMargin = 0xDEADBEEF; // The bigger these get the smaller the view gets
robotParams.topMargin = 0xDEADBEEF;
screenLayout.addView(mRobotView, robotParams);
}
Any idea what's causing this?
Use
android:layout_height="720px"
android:layout_height="743px"
in the main_layout.xml to have the image not shrinked
I have countdown functionality in my app.
Every second a timer sets the current time to a TextView.
So strings are: "56:05","56:04","56:03","56:02"...
I want to make the text size as big as possible
Therefore I've written the following code.
private void measureAndSetText(String text) {
Paint pMeasure = new Paint();
Integer iWidth = _tvContent.getWidth();
Float maxTextSize = 1000f;
pMeasure.setTextSize(maxTextSize);
pMeasure.setFakeBoldText(true);
Float fCurrentWidth = pMeasure.measureText(text);
while (fCurrentWidth > iWidth) {
pMeasure.setTextSize(maxTextSize -= 1);
fCurrentWidth = pMeasure.measureText(text);
}
_tvContent.setText(text);
_tvContent.setTextSize(TypedValue.COMPLEX_UNIT_PX, maxTextSize);
}
This code seems to work on my Note2, Lg Optimus 4x HD, Galay ACE and some others.
But not on my Xperia Z in landscape mode.
I guess the reason is the Full Hd Display but I don't understand why.
On the Xperia Z just the ":" sign is displayed in landscape mode. So I think the text is wrapped but I don't know why.
It would make sense to me if the text size I set to it is higher than the screen height (in landscape mode this is actually screen width -> 1080) but this isn't the case.
When I try to set a longer text -> "asdfasd asdf" it is correctly displayed.
Can someone point me to the problem?
Cheers,
Stefan
UPDATE:
I figured out that my iWidth variable which holds the width of my TextView (_tvContent.getWidth()) has the value 1770.
How can that be since my Xperia Z has just 1920x1080???
So I thought that it may be a failure in TextView.getWidth() and added the following code:
Display display3 = getWindowManager().getDefaultDisplay();
Point size3 = new Point();
display3.getSize(size3);
int width = size3.x;
Display display1 = getWindowManager().getDefaultDisplay();
int width1 = display1.getWidth(); // deprecated
int height1 = display1.getHeight(); // deprecated
Display display = getWindowManager().getDefaultDisplay();
Integer iWidth = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
Results:
Point(1080, 1776)
Display id 0: DisplayInfo{"Integrierter Bildschirm", app 1080 x 1776, real 1080 x 1920, largest app 1794 x 1701, smallest app 1080 x 1005, 60.0 fps, rotation 0, density 480, 442.451 x 443.345 dpi, layerStack 0, type BUILT_IN, address null, FLAG_SECURE, FLAG_SUPPORTS_PROTECTED_BUFFERS}, DisplayMetrics{density=3.0, width=1080, height=1776, scaledDensity=3.0, xdpi=442.451, ydpi=443.345}, isValid=true
Is this a Bug on site of Sony?
After I implemented my own TextView with its own OnDraw I've seen that there is an error in LogCat: ERROR/OpenGLRenderer(2503): Font size to large to fit in cache.
After a short research I figgured out that this is really a Bug in Android.
Addding this line to my custom TextView did the trick for me:
this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
I am currently trying to create a grid of multiple squares (Should be a minesweeper grid one day).
Now my solution of getting every mine a specific position is by using this loop:
for(int i = 0; i < cols; i++)
{
for(int j = 0; j < rows; j++)
{
mines[i][j].setCoordinates(xCounter, yCounter);
xCounter+=150;
}
yCounter+=150;
xCounter = 0;
}
now 150 should display the width or the height of one square because if one mine is on (0,0) the next should be on (minewidth,0) to fit nice and snuggly.
But now my question is, how can I be independend of this 150?
I got to this number by trying out which number makes the grid without spacing. But when I use a lower density device the images are smaller but the distance to each other would remain 150 pixels.
Now how can I get something generally to be independent of this constant?
First I thought I could use the .getWidth() method to get the width of the Bitmap that is stored in my "Mine" class. But this didn't work out so...
Has anyone got an idea to create a grid independent of which density the device has?
You can try to get the width and height of your screen(in dp) and use a factor to determine the width for each grid..
You can add the below code in your application class,
DisplayMetrics displayMetrics = applicationContext.getResources().getDisplayMetrics();
int absoluteWidthPx = displayMetrics.widthInPixels;
int abduluteHeightPx = displayMetrics.heightInPixels;
float scalingFactor = displayMetrics.density;
Now you can find the dp values for width and height of the screen,
/** pixels = densityIndependantPixels * (dotsPerInch/160), based on this **/
int dpWidth = (int)(absoluteWidthPx /scalingFactor);
int dpHeight = (int)(abduluteHeightPx /scalingFactor);
Once you have the width and height in dp you can try using some factors to determine the width for each grid.