ImageView Disappears when trying to Set the Width and Height programmatically - android

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);

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 scale imageview image to specific size?

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;

Getting screen size and use in drawbit map

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....

Android Programming: How to maintain image size on different displays?

e.g. I want a given Bitmap (100x200px) to always have the following size:
height = 1/10 of the screens height
length = 1/20 of the screens length,
no matter how large the screen is and what resolution it has. How can I do this without creating versions of my bitmap for each of the drawable folders (I've seen examples where all the pictures where only stored in the "drawable" folder, so there must be away without creating 4 instances of each picture used)?
Thanks for the help!
Load your bitmap into an ImageView and set the ScaleType of the ImageView to 'FIT_XY' and it will scale your image to whatever size your ImageView is. You can set the ImageView size to be relative to your screen size by getting the screen dimensions from the WindowManager
For example:-
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
float newWidth = 0.05f * screenWidth;
float newHeight = 0.10f * screenHeight;
imageview.setScaleType( ImageView.ScaleType.FIT_XY );
imageview.setLayoutParams(new LinearLayout.LayoutParams( (int)newWidth, (int)newHeight ));
final int width = getWindowManager().getDefaultDisplay().getWidth();
final int height = getWindowManager().getDefaultDisplay().getHeight();
then use bitmapfactory to scale the image

Categories

Resources