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!
Related
Here is my code
onCreate()
{
img = (ImageView)findViewById(R.id.img);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int w = size.x;
int h = w * (9/16);
img.getLayoutParams().height = h;
img.getLayoutParams().width = w;
img.requestLayout();
//The rest of the code spawns a new thread and downloads an image for the image view.
}
and yeah I tried using the FrameLayout Params too (as my ImageView is in a FrameLayout), something like this img.setLayoutParams(new FrameLayout.LayoutParams(w,h)), didn't work
Well what Im trying to do is irrespective of the Image/Image SIZE being downloaded, I want to create an ImageView with w:h = 16:9 ratio. What is the problem with my logic? I tried doing an img.requestLayout() too, didn't work, moved the code to onAttachWindow(), didn't work. Whats the problem here? Would really appreciate any help. Thanks.
It seems the only problem with your code is this line:
int h = w * (9/16);
The (9/16) is being performed as integer division, so it's resulting in 0. Change it to:
int h = (int)(w * 9f / 16f);
I created an imageview image, but I want to scale it to be no greater than a fifth of the width and a sixth of the height of the android device's screen size.
Here I get the width and height of the screen:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
here I create my imageview:
ImageView imageView = new ImageView(this);
imageView.setImageResource(icons[0]);
and here I try to scale my imageview:
imageView.getDrawable().setBounds(0, 0, width/5, height/6);
This last part is the stickler. After typing this in I get no errors and my program runs normally - but the image is not scaled. Basically that last line of code seems to have no effect and I have no idea why, any pointers?
Rest of my code:
imageView.setAdjustViewBounds(true);
int mh = imageView.getDrawable().getIntrinsicHeight();
int mw = imageView.getDrawable().getIntrinsicWidth();
imageView.setX(width/2 - Math.round(width/5));
imageView.setY(height/2 - Math.round(height/6) - mActionBarSize);
relativeLayout.addView(imageView);
setContentView(relativeLayout);
do this :
imageView.getLayoutParams().height = (int) Math.round(height/6);
imageView.getLayoutParams().width = (int) Math.round(width/5);
imageView.requestLayout(); // If you're setting the height/width after the layout has already been 'laid out'
see this link to learn more about requestLayout():
Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree.
But ususally the view is calling it automatically, you don't have to care about that... (So for me I use it to force the view to do what I want to do)
Use this
ImageView imgView = (ImageView) findViewById(R.id.pageImage);
imgView.getLayoutParams().height = 100;
imgView.getLayoutParams().width = 100;
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....
The problem with properly handling multiple screen sizes on Android has been talked all over thousands of times. However I couldn't find a solution to m problem. In a nutshell I need to align my custom progress bar over an imageView. I've got 3 set of drawables for the imageView - ldpi(240x400), mdpi(320x480), hdpi(480x800). I align my custom view in Java with the following code:
//get screen density
float density = getBaseContext().getResources().getDisplayMetrics().density;
//set the progress bar position according to screen density
if ( density == 1.0f)
{
ImageView micImage = ((ImageView) findViewById(R.id.imageViewClk));
Drawable drawing = micImage.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
// Get current dimensions
int width = bitmap.getWidth();
int height = bitmap.getHeight();
LayoutParams params = new LayoutParams((int)(height/13.94), (int)(height/13.94));
params.setMargins((int)(width/2.30), 0, 0, (int)(height/2.75));
params.addRule(RelativeLayout.ALIGN_LEFT,R.id.imageViewClk);
params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.imageViewClk);
myCustomTwistedProgressBar.setLayoutParams(params);
}else if ( density == 1.5f ){
ImageView micImage = ((ImageView) findViewById(R.id.imageViewClk));
Drawable drawing = micImage.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
LayoutParams params = new LayoutParams((int)Math.round(height/14.13), (int)Math.round(height/14.13));
params.setMargins((int)Math.round( width/2.27), 0, 0, (int)Math.round(height/2.91));
params.addRule(RelativeLayout.ALIGN_LEFT,R.id.imageViewClk);
params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.imageViewClk);
myCustomTwistedProgressBar.setLayoutParams(params);
}else if ( density == 0.75f ){
ImageView micImage = ((ImageView) findViewById(R.id.imageViewClk));
Drawable drawing = micImage.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
// Get current dimensions
int width = bitmap.getWidth();
int height = bitmap.getHeight();
LayoutParams params = new LayoutParams((int)(height/14.88), (int)(height/14.88));
params.setMargins((int)(width/2.27), 0, 0, (int)(height/2.69));
params.addRule(RelativeLayout.ALIGN_LEFT,R.id.imageViewClk);
params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.imageViewClk);
myCustomTwistedProgressBar.setLayoutParams(params);
}
Everything worked fined on different screen sizes however when I tried to check on 480x854 resolution the vertical alignment of the custom view was incorrect. Checked with 480x800 on the same screen size and it again works. I than went for a big jump and checked in GalaxyTab and the horizontal and vertical alignments were wrong. Now my first though was that the bitmap width and height were the one of the image not the actual resized imageview. So I spent a lot of time on trying to get the real size of the imageview and even went for viewTreeObserver but the results were all the same - the correct, unchanged (unscaled?) bitmap size. So being positive that the problem is not here I couldn't get through further. Does anyone have an idea why the alignment is not working correctly?
PS: as for the image view in layout xml file I have 2 configurations for long and notlong but this image has the same description in both:
<ImageView
android:src="#drawable/cloking"
android:id="#+id/imageViewClk"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/imageViewProcess"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:layout_marginTop="60dp"
android:scaleType="fitXY">
</ImageView>
Android will scale the image but it will maintain aspect ratio for the image. You can't control aspect ratio with layout settings (as far as I know). I would solve that problem by choosing few screen ratios that I want to support and making few more resources (images that would have aspect ratios that you support). Code would look like this:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
ImageView image = (ImageView) findViewById(R.id.imageViewClk);
if(width/height == aspectRatio1)
{
image.setImageResource(R.id.imageAspect1);
} else if( width/height == aspectRatio2...
So I need to change the size of an image depending on the area of the screen. The image will have to be half of the screen height, because otherwise it overlaps some text.
So Height= 1/2 Screen Height.
Width = Height*Aspect Ratio (Just trying to keep the aspect ratio the same)
I found something that was:
Display myDisplay = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width =myDisplay.getWidth();
int height=myDisplay.getHeight();
But how would I change image height in java? or even XML if possible? I can't seem to find a working answer.
You can do this with LayoutParams in code. Unfortunately there's no way to specify percentages through XML (not directly, you can mess around with weights, but that's not always going to help, and it won't keep your aspect ratio), but this should work for you:
//assuming your layout is in a LinearLayout as its root
LinearLayout layout = (LinearLayout)findViewById(R.id.rootlayout);
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.image);
int newHeight = getWindowManager().getDefaultDisplay().getHeight() / 2;
int orgWidth = image.getDrawable().getIntrinsicWidth();
int orgHeight = image.getDrawable().getIntrinsicHeight();
//double check my math, this should be right, though
int newWidth = Math.floor((orgWidth * newHeight) / orgHeight);
//Use RelativeLayout.LayoutParams if your parent is a RelativeLayout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
newWidth, newHeight);
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
layout.addView(image);
Might be overcomplicated, maybe there's an easier way? This is what I'd first try, though.