Updating layoutParams not working - android

I'm trying to calculate the dimension for this image, which will be downloaded from the web using this lineimgLoader.DisplayImage(url, R.drawable.thumbnail_background, image);.
The problem is that orgHeight becomes zero and you can't divide by zero. But why is this orgHeight 0?
// Add the imageview and calculate its dimensions
//assuming your layout is in a LinearLayout as its root
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
ImageView image = (ImageView)findViewById(R.id.photo);
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(url, R.drawable.thumbnail_background, image);
int newHeight = getWindowManager().getDefaultDisplay().getHeight() / 2;
int orgWidth = image.getWidth();
int orgHeight = image.getHeight();
//double check my math, this should be right, though
int newWidth = (int) 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.updateViewLayout(image, params);
My imageView xml is like this:
<ImageView
android:id="#+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>

The views aren't yet laid in the onCreate() method so their dimensions are 0. Post a Runnable from onCreate() to get the proper values:
image.post(new Runnable() {
#Override
public void run() {
int newHeight = getWindowManager().getDefaultDisplay().getHeight() / 2;
int orgWidth = image.getWidth();
int orgHeight = image.getHeight();
//double check my math, this should be right, though
int newWidth = (int) 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.updateViewLayout(image, params);
}
});

You can also use the ViewTreeObserver to get the values as soon as the layout is done.
Ref - How can you tell when a layout has been drawn?

Related

Android studio, making a textview appear on a bitmap inside of an imageView

been struggling with a problem for a while. Basically, I've been wrapping my head around trying to get the most accurate width and height of a bitmap inside of an imageview.
How it is, I have a textview and a imageview in a relativeLayout. The imageview gets a bitmap from the gallery of the phone, then centers it in the imageview and keeps the image ratio when it scales the image up to fit the screen accordingly. My problem is that my textview is supposed to get a random position where that position is on-top of the bitmap. I've been trying to get the correct width and height of the bitmap for some math, but I can't quite get the correct width and height. My text always seem to get outside of the bitmap. Is there someone that could help me with this? It's been days..
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)
g.getLayoutParams();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
int dpHeight = (int) (displayMetrics.heightPixels / displayMetrics.density);
int dpWidth = (int) (displayMetrics.widthPixels / displayMetrics.density);
int widthMin = g.getWidth();
int widthMax = dpWidth;
int randomWidth = new Random().nextInt(widthMax - widthMin + 1) + widthMin;
int heightMin = g.getHeight();
int heightMax = dpHeight;
int randomHeight = new Random().nextInt(heightMax - heightMin + 1) + heightMin;
TextView test = (TextView) findViewById(R.id.textView11);
test.setText(String.valueOf(widthMax));
params.leftMargin = randomHeight;
params.topMargin = randomWidth - 40;

ImageView dynamic position

i'm using the following code to position ImageView
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
bg = (ImageView) rootView.findViewById(R.id.rectimage);
bg.getLayoutParams().width = width;
bg.getLayoutParams().height = 500;
MarginLayoutParams params = (MarginLayoutParams)bg.getLayoutParams();
params.setMargins(0, height - 510, 0, 0);
i want to place ImageView always at the bottom of screen but screenHeight - image_height is not working Any ideas?
Try to add
android:layout_alignParentBottom="true"
to your ImageView in your layout xml file

How can we set height and width dp for imageview in android?

I would like to set height and width in dp for ImageView in android pragmatically.
How can I achieve this?
Set width & height with dp :
imageview.getLayoutParams().height = (int) getResources().getDimension(R.dimen.imageview_height);
imageview.getLayoutParams().width = (int) getResources().getDimension(R.dimen.imageview_width);
In your dimens.xml provide values for the keys :
<dimen name="imageview_width">50dp</dimen>
<dimen name="imageview_height">50dp</dimen>
Use display metrics to get the sale factor and then just some simple maths - example, if I want 200x150dp:
final float scale = getResources().getDisplayMetrics().density;
int dpWidthInPx = (int) (200 * scale);
int dpHeightInPx = (int) (150 * scale);
Then set the image view size:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(dpWidthInPx, dpHeightInPx);
imageView.setLayoutParams(layoutParams);
This may be simpler and should do the trick:
ImageView im = (ImageView)findViewById(R.id.image1);
LayoutParams params = im.getLayoutParams();
params.height = getActivity().getResources().getDimensionPixelSize(R.dimen.item_height);
params.width = getActivity().getResources().getDimensionPixelSize(R.dimen.item_width);
And in your dimens.xml
<dimen name="item_height">80dp</dimen>
<dimen name="item_width">80dp</dimen>
This may help you...
ImageView im = (ImageView)findViewById(R.id.image1);
LayoutParams params = im.getLayoutParams();
params.height = 100;
params.width = 100;
at first choose a desirable dip and assign it to a var.
int dipAmount=350;
Then read the height of your ImageView.
float scale = imageview.Resource.DisplayMetrics.Density;
Convert from px to dip.
int converter =(int) (350 * scale + 0.5f);
Set the height to imageview.
imageView.LayoutParameters.Height=converter;
final float scale = getContext().getResources().getDisplayMetrics().density;
int height_ = (int) (250 * scale + 0.5f);
int width_ = (int) (250 * scale + 0.5f);
250 is in dp
ViewGroup.LayoutParams params = ImageView.getLayoutParams();
params.height = height_;
params.width = width_;
ImageView.setLayoutParams(params);
Try this:
image_view.getLayoutParams().height = 20;
image_view.getLayoutParams().width= 20;
Give me your feedback if it's acceptable. It's work for me.

Design dynamic hotspot on Image in Android

I have to develop a UI like below:
I want to show this type of image and show hotspot on that image. The position of hotspot will be dynamic, as per x,y and radius provided the circle will be drawn on the original picture. The user can click on the hotspots and onclick action will be defined on the specific hotspot on which the user will click.
What is best process to develop this type of UI?
Make your main layout a RelativeLayout and then you can add programmatically a ImageView with an onClickListener to your layout with the following code:
private void addImageView(RelativeLayout mainLayout, int x, int y, int width, int height, OnClickListener onClickListener){
ImageView imageView = new ImageView(this);
imageView.setAdjustViewBounds(false);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.height = height;
params.width = width;
imageView.setLayoutParams(params);
imageView.setScaleType(ImageView.ScaleType.FIT_XY); //remove this if you want to keep aspect ratio
imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher)); //here goes your drawable
params.leftMargin = x - width/2;
params.topMargin = y - height/2;
imageView.setOnClickListener(onClickListener);
mainLayout.addView(imageView);
}
to use it you call:
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.relativeLayout); //this is your main layout
addImageButton(mainLayout, 200, 300, 200, 200, new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
}
});
You can also use a ImageButton to achive the same porpose, although the image size will be affected by button border:
private void addImageButton(RelativeLayout mainLayout, int x, int y, int width, int height, OnClickListener onClickListener){
ImageButton imageButton = new ImageButton(this);
imageButton.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.height = height;
params.width = width;
imageButton.setLayoutParams(params);
imageButton.setScaleType(ImageView.ScaleType.FIT_XY);
imageButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
params.leftMargin = x - width/2;
params.topMargin = y - height/2;
imageButton.setOnClickListener(onClickListener);
mainLayout.addView(imageButton);
}
Try it.

How I can increase or decrease the size of an ImageView?

I would like to know how to enlarge an image that is already in the ImageView.
I have this:
m_imageView.setImageBitmap(imagen);
int newHeight = getWindowManager().getDefaultDisplay().getHeight() / 2;
int orgWidth = m_imageView.getDrawable().getIntrinsicWidth();
int orgHeight = m_imageView.getDrawable().getIntrinsicWidth();
int newWidth = (int) Math.floor((orgWidth * newHeight) / orgHeight);
//Use RelativeLayout.LayoutParams if your parent is a RelativeLayout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
newWidth, newHeight);
m_imageView.setLayoutParams(params);
//m_imageView.setScaleType(ImageView.ScaleType.FIT_START);
But it does nothing, the image remains the same.
Thank you for your help.
did you invalidate the view? with invalidate() or postInvalidate() on your activity?

Categories

Resources