Camera not stretching to width of android device - java

In my android camera app, Im trying to make it so that the camera layout view fills the width of the screen and then is proportional (to a square) for the height of the layout. But its cropping it too small for some reason:
example of what is happening..
The green arrow represents where it should go to.
Ive tried fill_parent, etc. So, how can I make it go to the end of the screen?
I believe this is the problem. I know I did this because I was not sure how to make it automatically stretch to the right proportion.
int width = 352;
int height = 288;
camPreview = new CameraSurfaceView(width, height);
myCamHolder.addCallback(camPreview);

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = width*288/352;
Maybe you don't need 288/352, but rather preview size.height/preview size.width

Related

How to get usable size screen size in android studio?

So I want to make an app compatible with multiple screen sizes, however the positioning of some buttons is done programmatically and it has to align with the background (timetable app, task aligned with proper day, start time and end time on a grid display). Hence, I need to get the screen dimensions.
Currently I'm testing this in Nokia 8.1 with height 2246px and width 1080px, however what I need is the actual usable height not the total height (minus the bar at the top and the bottom). After manually testing the usable height it seems to be 1887px, but I have not found a way to retrieve this number.
I tried these codes so far:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels; //2246px
int displayWidth = displayMetrics.widthPixels; //1080px
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels; //2034px
int displayWidth = displayMetrics.widthPixels; //1080px
WindowManager windowManager = (WindowManager) mainActivity.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y; //2034px
int width = size.x; //1080px
I thought maybe navigation bar is the problem
Resources resources = mainContext.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
int navigation_bar_height = resources.getDimensionPixelSize(resourceId); //126px
2034 - 126 = 1908px != 1887px (which is close but still to big, it goes off screen)
Furthermore, when I tried to make a new hardware profile with the phone dimensions (2246 x 1080), using 1887px goes off screen on the xml Design display, even though it shows up exactly right on my phone...
So yeah I'm lost, no idea how to get the real size or why the profile doesn't match. Please help.

screen coordinates interfere with the bar at the bottom of the screen

I'm learning about android game development and have made a few small games. The problem I encounter is that when I draw an object at the bottom of my screen it is hidden behind the bar at the bottom.
For example I made a simple pong game, but my paddle is hidden behind the bar.
I retrieve the screen size as follows:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int x = size.x;
int y = size.y;
My paddle is a rectF object with bottom coordinate equal to y.
Other little games i made encouter the same problem. Every time i draw an object at the bottom of the screen with the canvas and paint classes it interferes with the bar.
How can i solve this? Is there a way to retrieve the coordinates of that bar?
According to specifications, DisplayMetrics is probably what you should use instead of Display:
public void getMetrics (DisplayMetrics outMetrics)
Gets display metrics that describe the size and density of this display. The size returned by this method does not necessarily represent the actual raw size (native resolution) of the display.
You could use this:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;

Android app only runs after minimizing

I'm trying to make an Android snake game, and I want to have a grid that stretches in a square the width of the screen (Portrait).
I an using a drawable storing my png, and when i add the line grid.setBounds(0,0,screenWidth,screenWidth) my phone displays a white screen at first, though it works as expected once I minimize and reopen my application.
The app also works as expected if I put standard numbers in as parameters. This is on my nexus 6P on android 7. When I emulate it on an older device it works, but emulated on a newer device, I see the same issue. I get the screen width as follows:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
scale = screenWidth/1024.0;
If anyone could help that would be much appreciated.
Try to get the screen size in onWindowFocusChnaged method of activity.
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
scale = screenWidth/1024.0;
}

How to set margin for ImageView in pixels in android java?

I have a set of ImageViews whose BackgroundResource and LayoutParams will be defined in a separate xml file. The height, width, marginleft and margintop are given in pixels. The values are in respect to a 1024x600 pixel resolution screen. The code i'm using
ImageView ImgV= (ImageView)findViewById(R.id.ImgV_1);
RelativeLayout.LayoutParams the_dimens = new RelativeLayout.LayoutParams(50,50);
the_dimens.setMargins(800,50,0,0);
ImgV.setBackgroundResource(R.drawable.bulbon);
ImgV.setLayoutParams(the_dimens);
While the height and width seem to be rightly displayed in pixels, the margins are not. So how can I set the margin values to apply in pixels rather than whatever default units the below code takes.
the_dimens.setMargins(800,50,0,0);
The app is only for a 1024x600 device, so i'm not worried about resolution independent design.
Thanks.
check this
int dpValue = 5; // margin in dips
float d = context.getResources().getDisplayMetrics().density;
int margin = (int)(dpValue * d); // margin in pixels
You can do in this way:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);

unable to get display metrics

I am making a game that displays animated characters on the screen
Now I am at the stage where i want it to display correctly on other devices as well
increase the screen resolution on my own device and have it still display correctly
for this I want to get the scale factor so I can change all my pixel values to DP values
however the display metrics does not change no matter what I change the screen resolution to
for example
this.getHolder().setFixedSize(1024, 768);
scale = getResources().getDisplayMetrics().density;
DisplayMetrics dm = new DisplayMetrics();
((Activity) getContext()).getWindowManager().getDefaultDisplay()
.getMetrics(dm);
float xDpi = dm.xdpi;
float yDpi = dm.ydpi;
Now no matter what I change 'setfixedsize ' to, both xdpi and scale stay the same even though it does show the increased screen resolution on the display
You can get display metrics using below line.
DisplayMetrics metrics = getResources().getDisplayMetrics();
This is my code for display wallpaper may be its help full=====>
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point size = new Point();
display.getSize(size);
float w = size.x;
float h = size.y;
width = ((w)/1000);
height = ((h)/1000);
Log.d("width===>", String.valueOf(width));
Log.d("height===>", String.valueOf(height));
(OR)
metrics = Resources.getSystem().getDisplayMetrics();
float w=metrics.widthPixels;
float h=metrics.heightPixels;
width = ((w)/1000);
height = ((h)/1000);
if(width == 0.72f && height == 1.28f){
width=1.08f;
height=1.776f;
}
Log.d("width===>", String.valueOf(width));
Log.d("height===>", String.valueOf(height));

Categories

Resources