How to get image width and height dynamically while zooming in android? - 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.

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% */

How to scale a image (dynamic imageviews)

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;

Android image scale type to fit width and scaleY = scaleX

I have a layout with a background image. The image width should be the same as the screen width (match_parent). But it's ugly, the OS don't stretch the picture in height.
I heard about ImageView scaleType, so I have the background image in an ImageView instead of a layout background, but I can't config to be what I want.
How can I set to the layout or ImageView to scale the image (in width) to fit the width of the screen AND scale the height with the same scale?
I want the second screen in the picture:
UPDATE:
I'm trying from code, but I get 0 width + I can't believe this can't be done from xml with an ImageView.
Drawable imageDrawable = getResources().getDrawable(imageResource);
contentContainer.setBackgroundDrawable(imageDrawable);
Rect rect = imageDrawable.getBounds();
int originalWidth = rect.width();
int originalHeight = rect.height();
Display display = getActivity().getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = originalHeight * (width/originalWidth);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
contentContainer.setLayoutParams(layoutParams);
contentContainer.post(new Runnable() {
#Override
public void run() {
contentContainer.setScaleY(contentContainer.getScaleX());
Rect rect = contentContainer.getBackground().getBounds();
int originalWidth = rect.width();
int originalHeight = rect.height();
Display display = getActivity().getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = originalHeight * (width/originalWidth);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
contentContainer.setLayoutParams(layoutParams);
}
}
Use FrameLayout as parent and then add imageview as child.
Give the width match_parent and height fill_parent to the imageView and another ways is apply the android:layout_weight attribute try this may be it's work.
you can do this at runtime by measuring the width of the device like this:--
super.onCreate(savedInstanceState);
setContentView(<layout-resoure-id);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int imageSize = 0;
if (width > height)
imageSize = height;
else
imageSize = width;
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.getLayoutParams().width = imageSize;
imageView.getLayoutParams().height = imageSize;
imageView.setImageResource(R.drawable.pattu);
your xml:--
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitXY"/>
</LinearLayout>
Enjoy...!
Have you tried playing with scaleType in you layout file?
<ImageView
...
android:scaleType="fitStart"
/>
This should scale your image to fit at the top of your layout, stretching or shrinking if necessary. Haven't had a chance to test (and I can't remember the height/width settings--sorry!), but this could be a solution.
ImageView imageView = (ImageView) findViewById(R.id.your_image_id);
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
int imageSize = height>width?width:height;
your configurations go here !
imageView.getLayoutParams().width = imageSize;
imageView.getLayoutParams().height = imageSize + (/* additional height logic*/);
you should put the imageView scaletype as fitXY and do this to stretch the image like the way you want!
<ImageView
...
android:scaleType="fitXY"
/>

Android: How to find scale ratio of background image

OK, so I have an image that I am using in an ImageView to fill the screen. I am keeping the aspect ratio by using scaletype="centerCrop", as seen here:
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="#drawable/full_bg" />
In code, how do I go about finding the scale ratio, or scale factor, that resulted from the original image to the scaled image? I need to know so I can apply this same ratio to a button positioned strategically on top of the background. This is what I have tried to get the scale ratio:
ImageView view = (ImageView) findViewById(R.id.imageView1);
Log.i("Test", "x scale = " + Float.toString(view.getScaleX()));
This returns a 1.0 (unscaled) which isn't what I'm looking for. I also tried getting the dimensions of the scaled Bitmap instead of the ImageView, which also gives me the original image size (540px):
ImageView view = (ImageView) findViewById(R.id.imageView1);
Drawable drawing = view.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
Log.i("Test", "width = " + Integer.toString(bitmap.getWidth()));
Am I approaching this all wrong? I can't find anything to help me figure this out. Any help would be much appreciated!
Try this: I got this to change the image size depending on the size of the screen, but this could be easily changed by changing
int width = metrics.widthPixels;
to be
int width = bitmap.getWidth();
or whatever you want it to be scaled to. Note width is the size you're going to.
public static void changeImageSize(ImageView object, Context context){
DisplayMetrics metrics = new DisplayMetrics();
((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
BitmapDrawable bmap = (BitmapDrawable) object.getDrawable();
RelativeLayout.LayoutParams layout = (LayoutParams) object.getLayoutParams();
float bmapWidth = bmap.getBitmap().getWidth();
float bmapHeight = bmap.getBitmap().getHeight();
int newBmapWidth = (int) (bmapWidth*width/480);
int newBmapHeight = (int) (bmapHeight*height/800);
layout.width = newBmapWidth;
layout.height = newBmapHeight;
object.setLayoutParams(layout);
}

Android align top bitmap in imageview

Is there anyway so I can align bitmap in ImageView. I have an issue with my image view. I'm setting it's source via Java Code and after bitmap is resized it's centered in ImageView, but the thing that I want is to align the bitmap.
Declaring the imageView :
<RelativeLayout
android:id="#+id/collection_background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/actionbar" >
<ImageView
android:id="#+id/collection_image_background"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/stampii" />
// countinue ........
and here is how I'm setting the bitmap :
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inTempStorage = new byte[8*1024];
ops = BitmapFactory.decodeStream(cis,null,o2);
ImageView view = (ImageView) findViewById (R.id.collection_image_background);
view.setImageBitmap(ops);
Any ides how to do that?
Add android:scaleType="fitStart" to your ImageView. It is ok for me.
You can use android:scaleType to change the alignement in your ImageView.
You can use setLayoutParams, as shown in this answer.
EDIT
Your problem is that you've specified the view to be as wide as its parent, while the loaded bitmap may be considerably thinner.
Try to make the view only as wide as needed by using this attribute instead:
android:layout_width="wrap_content"
Actually you can scale your bitmap depending on your screen size and set it as source of your ImageView. You can use something like this :
int screenWidth = getWindow().getWindowManager().getDefaultDisplay().getWidth();
Log.e("","screen width : "+screenWidth);
//int screenHeight = getWindow().getWindowManager().getDefaultDisplay().getHeight();
int width = ops.getWidth();
Log.e("","bitmap width : "+width);
int height = ops.getHeight();
Log.e("","bitmap height : "+height);
float scale = 0;
if(width>height){
scale = (float) width / (float) height;
} else if(height>width){
scale = (float) height / (float) width;
}
Log.e("","scale : "+scale);
float newWidth = (float) screenWidth * scale;
Log.d("","new height : "+newWidth);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(ops, screenWidth, (int) newWidth, true);
Log.e("","new bitmap width : "+scaledBitmap.getWidth());
Log.e("","new bitmap height : "+scaledBitmap.getHeight());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(screenWidth, (int)newWidth);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
view.setLayoutParams(params);
view.setImageBitmap(scaledBitmap);
Try this and just hit me up if it's working or not.
Additionally to the answers above you can set it programmatically with scaleType:
image.scaleType = ImageView.ScaleType.FIT_START

Categories

Resources