How to scale a image (dynamic imageviews) - android

This is my code to create dynamic imageviews. Image get aligned really well but I need to make it small. How can I scale the image?
This is a in a LinearLayout:
for(int i=0;i<value1;i++) {
ImageView image = new ImageView(Home.this);
image.setImageResource(R.drawable.bulb);
image.setScaleX(1);
image.setScaleY(1);
image.setScaleType(ImageView.ScaleType.MATRIX);
rR.addView(image);
}
This is my code to create dynamic imageviews . Image get aligned really well but i need to make it small. How can i scale the image?

Please set the proper height and width of your dynamic image view.
Please try below code it's helpful to you.
for (int i = 0; i < 3; i++)
{
ImageView image = new ImageView(ImageScalling.this);
image.setImageResource(R.drawable.ic_launcher);
image.setLayoutParams(new LinearLayout.LayoutParams(200, 200));
image.setScaleType(ImageView.ScaleType.FIT_XY);
rR.addView(image);
}
I have set width and height is 200. If you want the show a small image then set less than 200.

For scaling imageview try this link
https://argillander.wordpress.com/2011/11/24/scale-image-into-imageview-then-resize-imageview-to-match-the-image/

You can set the size of Imageview according to screen size like 20% of height .
Display display = getWindowManager().getDefaultDisplay();
int height = display.getHeight();
int width = display.getWidth();
float mReqPercentages =20.0;
float mCalculatedWidth = (mReqPercentages / 100) * height;
image.getLayoutParams().height = (int) (mCalculatedWidth * 1.5);
image.getLayoutParams().width = (int) mCalculatedWidth;

Related

How to get image width and height dynamically while zooming in android?

I used the below method to find the image width and height dynamically.But,I got the same value while zoom in and zoom out an image.I also used like this to retrieve the iamge width:- float width=imag.getWidth(). For this also the same value is displayed.I set the image width as 300dp in my xml file.
image.setOnTouchImageViewListener(new OnTouchImageViewListener() {
#Override
public void onMove() {
RectF rect = image.getZoomedRect();
currentZoom = image.getCurrentZoom();
Drawable d = image.getDrawable();
Bitmap bmp = ((BitmapDrawable)d).getBitmap();
currwidth = bmp.getWidth();
currheight = bmp.getHeight();
calculate(currwidth);
MarginLayoutParams params = (MarginLayoutParams) animImag.getLayoutParams();
params.leftMargin =(int)calculate(currwidth); params.topMargin = (int)calculate(currwidth);
animImag.setLayoutParams(params);
}
});
I think you have to try image.getMeasuredWidth and height. for Proper size of Image.

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;

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

Aligning a view to an ImageView in different resolutions

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

Android Changing image size depending on Screen Size?

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.

Categories

Resources