Getting screen size and use in drawbit map - android

This is not my activity! this is separate class file
i am using Surface view extended to my class file . i wanted to display a small bit map to my canvas my bit map is very small than my phone screen so i used a createscaledbitmap to fit it in my phone screen with this code here
Bitmap o2 = Bitmap.createScaledBitmap( hud, 800, 490, false);
it worked great in my phone but when i tested in a bigger phone i have my bitmap pasted only 3/4 of the screen .. so i want to make my bitmap to fix the phone screen size
Bitmap o2 = Bitmap.createScaledBitmap( hud, phonescreenwidth, phonescreenheight, false);
is there a function to get the phone display size and use in my code ?
like
int phonescreenwidth = surfaceView.getwidth ();
and there is a function called protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
i dont know how to use it . if someone know please tell me how too :)
Thank you all

BY this way you can get width and height of your device and pass it in your method..
WindowManager manager = (WindowManager) getApplicationContext().getSystemService(Activity.WINDOW_SERVICE);
int width, height;
LayoutParams params;
if (Build.VERSION.SDK_INT > VERSION_CODES.FROYO) {
width = manager.getDefaultDisplay().getWidth();
height = manager.getDefaultDisplay().getHeight();
} else {
Point point = new Point();
manager.getDefaultDisplay().getSize(point);
width = point.x;
height = point.y;
}

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Refer here http://developer.android.com/guide/practices/screens_support.html#testing
and
Get screen dimensions in pixels

try this to get Screen size,,
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
then try passing context from ur baseActivity to the current activity
and then try context.getWindowManger....

Related

Set ImageView max height using code

I want to set Max Height to Imageview that it can have when the image is loaded. Initial I've set android:height="wrap_content" in XML to make room for other views. But after the image is loaded sometimes it covers the whole screen of mobile and i can't see other views and i want to limit the height to some certain value. Please!! any help is appreciated.. cheers
A structure describing general information about a display, such as
its size, density, and font scaling.
DisplayMetrics metrics = getResources().getDisplayMetrics();
int DeviceTotalWidth = metrics.widthPixels;
int DeviceTotalHeight = metrics.heightPixels;
/*
*
* Adding Height Respect To Device
* */
ImageView ImageViewObj=(ImageView)findViewById(R.id.ImageViewId);
ImageViewObj.getLayoutParams().height= (int) (DeviceTotalHeight);
Get the screen height, and use it to set some ratio like Sunny said.
/* get the screen height in pixels */
public static int getScreenHeight(WindowManager windowManager) {
Display display = windowManager.getDefaultDisplay();
Point point = new Point();
display.getSize(point);
return point.y;
}
/* take an imageview, and set its height to desired float ratio */
public static void setImageHeight(Activity activity, ImageView imageView, float ratio) {
int h = getScreenHeight(activity.getWindowManager());
imageView.getLayoutParams().height = (int)(h*ratio);
}
Then in onCreate(....), try doing
ImageView imageView = (ImageView) findViewById(R.id.whatever);
setImageHeight(imageView, 0.4f); /* 40% */

Android Layout- adjusting bitmaps to fill screen size- weird phenomenon

I'm trying to adjust 3 bitmaps to fill screen height (and width) with relative layout.
I've solved most of the problem but I still can't figure out why it works! I won't be able to relax until I do... so please try and help me understand the following:
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
wm.getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
ImageView imgTop= (ImageView) findViewById(R.id.boardTop);
ImageView imgMid= (ImageView) findViewById(R.id.boardMid);
ImageView imgBot= (ImageView) findViewById(R.id.boardBot);
top = BitmapFactory.decodeResource(getResources(), R.drawable.board_top);
mid = BitmapFactory.decodeResource(getResources(), R.drawable.board_mid);
bot = BitmapFactory.decodeResource(getResources(), R.drawable.board_bot);
top = Bitmap.createScaledBitmap(top, top.getWidth(), (int) (screenHeight*0.2), false);
mid = Bitmap.createScaledBitmap(mid, mid.getWidth(), (int) (screenHeight*0.6), false);
bot = Bitmap.createScaledBitmap(bot, screenWidth, (int) (screenHeight*0.2), false);
imgTop.setImageBitmap(top);
imgMid.setImageBitmap(mid);
imgBot.setImageBitmap(bot);
First I've used the code above and got that (I've hidden the pictures):
Then I've changed the 3 "createScaledBitmap" lines to the following:
top = Bitmap.createScaledBitmap(top, screenWidth, (int) (screenHeight*0.2), false);
mid = Bitmap.createScaledBitmap(mid, screenWidth, (int) (screenHeight*0.6), false);
bot = Bitmap.createScaledBitmap(bot, screenWidth, (int) (screenHeight*0.2), false);
(I've changed them so that all of them recieved the screenWidth value).
After the change above, I've recieved this (I've given each bitmap a different color so it's easier to see):
So it somehow fixed the bitmaps to fill all the screen height, though it still didn't grant the bottom bitmap the full screen width (as it should because I've given it the same values as the 2 other bitmaps above- also in the XML file).
So how come this happened? I believe I don't know how those bitmap classes really work. Please help me understand thoroughly what happened here.
Thanks a lot!

How to get screen resolution of your android device..? [duplicate]

This question already has answers here:
How to get screen dimensions as pixels in Android
(46 answers)
Closed 9 years ago.
Can i get resolution of android phone..?
if yes then how..?
it will really helpful for me..
Thank you..
If you want the display dimensions in pixels you can use getSize:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
This method was introduced in Android API 13, so if you want to get display metrics for previous APIs use:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;
If you're not in an Activity, you can get the default Display via WINDOW_SERVICE. Also be sure to set your minSdkVersion to at least 4 in AndroidManifest.xml to enable calls to display.getWidth().
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
Edit: PhoneGap
Use the following to figure out the display size in pixels
function getDeviceDimention() {
console.log("Device Dimention using PhoneGap");
console.log("Width = " + window.innerWidth);
console.log("Height = " + window.innerHeight);
}
For Cordova / Phonegap, I've found that using
var width = screen.width
var height = screen.height
is more reliable than using
var width = window.innerHeight
var height = window.innerWidth
in order to obtain the resolution of the current display. The latter is unfortunately inaccurate if your mark-up either overflows or does not take up all of the space available on the screen.
For phonegap you can use this
You can use device-width and device-height
<meta name="viewport" content="width=device-width; user-scalable=no" />
You can even get device height and width using jquery & jquery mobile like this
var width = $(window).width();
var height = $(window).height();
NOTE:- Do this when device is ready.
You can call
Display display = getWindow().getWindowManager().getDefaultDisplay();
Point outSize = new Point();
display.getRealSize(outSize);
outSize.x is the width and outSize.y is the height. Hope it help.
like this
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
look at this answer Android: How to get screen dimensions
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
Good luck!
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
good luck
행운을 빈다.
u can get screen resolution like this
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height=display.getHeight();

Android Scale Image to largest size

Hi i'm working on an application that will be loading images and i'm looking to scale each image to it's largest possible size for instance if the image is a landscape image (if width is larger than height) i would like to stretch the width to fill the width of the phone and scale height to keep it's aspect ratio. If the height is larger than the width i.e. a portrait image the image should be scaled to fit the height of the phone and width should then adjust to keep the aspect ratio i've had a bit of trouble getting this to work here's what i've done so far though any help would be greatly appreciated.
final ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setBackgroundResource(android.R.color.black);
//TODO need to get actual size of drawable not view size which is 0
ViewTreeObserver vto = i.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
int w = i.getMeasuredWidth();
int h = i.getMeasuredHeight();
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
if(w <= h){
//TODO need to think about landscape
h = height - convertDpToPixel(50, context);
w = w*(width);
}else{
h = h*(height);
w = width;
}
//TODO set imageview to w and h
return true;
}
});
for get Display width , height
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;
For Resize Imageview
ImageView img=(ImageView)findViewById(R.id.ImageView01);
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.sc01);
Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true);
img.setImageBitmap(resizedbitmap);
Increase either the height or the width to their maximum, depending on which is already greater.
Now, let's say you're working with a landscape image:
You've already expanded the width to it's maximum size, the display's width
The side you expanded is the width of the image, so assuming the display width = width and the image's width = w:
width / w = r
So r would be the ratio between the display width and the image width. Now you need to use this to increase your height.
Simply multiply the height by the ratio, and set the image's new height to the resulting number.
If you end up with a portait image... well, I think you'll be able to figure it out. It's pretty much the same thing.
After all that, I think it would just be a matter of positioning the image in the center of the screen.

Android: Get the screen resolution / pixels as integer values

this is a really simple question on which I've found no answer :/
How can I quickly access the screen resolution (width, height) as integer values?
I've tried this one, but it always shows zero on my emulator:
DisplayMetrics dm = new DisplayMetrics();
int width = dm.widthPixels / 2;
In my case I want to dynamically create a table with tableRows, each containing two cols. This cols all shall fill half of the screen in width.
Can someone give me a hint?
The trashkalmar answer is correct.
However, the results will be specific to the activity context. If you want the whole device screen resolution:
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
wm.getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
Note that the results also depend on the current device orientation.
You can get screen metrics in this way:
Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();
1.Simply use This inside activity to get screen width and height pixels.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay()
.getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
2.This can also be used But requires Api level inlined check
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display.getSize(size);
int width2 = size.x;
int height2 = size.y;
} else {
int width2 = display.getWidth();
int height2 = display.getHeight();
}
3.Use This when you are not in activity but having context
WindowManager wm = (WindowManager) context.getSystemService(
Context.WINDOW_SERVICE);
Display display1 = wm.getDefaultDisplay();
Point size1 = new Point();
int width1;
int height1;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display1.getSize(size1);
width1 = size1.x;
height1 = size1.y;
} else {
width2 = display.getWidth();
height2 = display.getHeight();
}
I use the following:
int scrWidth = getWindowManager().getDefaultDisplay().getWidth();
int scrHeight = getWindowManager().getDefaultDisplay().getHeight();
No muss, no fuss, just 2 class variables
The easiest way will be to implement onSizeChanged. You are probably getting a value of 0 because the view hasn't initialized yet. You can't call the above code in the View constructor for example.

Categories

Resources