I'm trying to create a scaled bitmap which fits in the Custom View but whenever I perform the operation the width gets cropped while the height of the bitmap fits into the custom view.
bitmapq = Bitmap.createScaledBitmap(bitmap,(custHeight*bitmap.getWidth())/bitmap.getHeight(),custHeight, true);
Custom View height - 1089.
Custom View width- 1180.
Width of bitmap - 2368.
Height of bitmap - 4208
If you'd like the scaled bitmap to be of the exact size of your custom view regardless of its aspect ratio, you can use this:
bitmapq = Bitmap.createScaledBitmap(bitmap, cusWidth, custHeight, true);
In this way, the scaled bitmap exacly fits your custom view's boundaries.
Related
I have a custom view in an Android project and in that custom view, I want to draw a bitmap that's about 1/3th the size of the view element.
I use the following code
Bitmap bigIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
Bitmap icon = Bitmap.createScaledBitmap(bigIcon, newWidth, newHeight, false);
width newWidth and newHeight being 1/3th of the view's width and height.
The problem is now that if I want to get the view's width or height, I'd have to get it in the onMeasure or onDraw method. And I'd really prefer not to scale it every single frame (the bigIcon is loaded only once in the constructor). The createScaledBitmap needs also to be called only once but the constructor is too soon since the View is not yet created.
So the question: how can I access the view's width and height (knowing that the view's size doesn't change)?
I think you can accomplish what you want by overriding your custom View's onSizeChanged method.
Here, you can calculate the dimensions of your bitmap based on the View's dimensions only when the size has actually changed.
I need to load some images from URI to Bitmap variables and perform some operation with them togheter. I need the bitmaps to be squared images with fixes size, scaled down and cropped. By now I use this code:
return Picasso.with(c).load(imageUri).resize(size, size).get();
but, obviously, the image will be resized without keep its aspect ratio.
I want to resize the image with these requirements:
the smaller dimension (width or height) should be equals to size
the greater dimension should be cropped to size, keep image centered
The key is using centerInside after resize. See link
Picasso.with(c).load(imageUri).resize(size, size).centerInside().get()
set your imageview height and width fix inside xml and then set image to imageview like
Picasso.with(YourActivityName.this)
.load(imageUri)
.into(imageview_id);
I have a bitmap that is being rendered inside of an imageview.
The scaling is set so the bitmap maintains it's aspect ratio. As such, there is padding on the top and bottom relative to the imageview.
What I'd like to do is get that padding. I'm assuming this is possible no?
This is my xml code:
<ImageView
android:id="#+id/ColImgPath"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:background="#drawable/border"/>
and this my java code, that it is inside listview:
ImageView imageView = (ImageView) convertView.findViewById(R.id.ColImgPath);
imageView.setPadding(5, 5, 5, 5);
imageView.setVisibility(View.VISIBLE);
try {
Bitmap bmp = ((Bitmap) MyArrList.get(position).get("ImageThumBitmap"));
int bmpx = bmp.getWidth();
int bmpy = bmp.getHeight();
Log.i("INFO","X: "+bmpx+" Y: "+bmpy);
Bitmap photobitmap1 = Bitmap.createScaledBitmap(bmp, bmpx/2,bmpy/2, true);
imageView.setImageBitmap(photobitmap1);
//imageView.setImageBitmap((Bitmap) MyArrList.get(position).get("ImageThumBitmap"));
} catch (Exception e) {
// When Error
imageView.setImageResource(android.R.drawable.ic_menu_report_image);
}
The problem is that I want to put the bitmap resized into bitmap but the imageview always appears squared.
The size of the bitmap is: X: 4160 Y: 3120 and the screen size is : WIDTH= 1080 HEIGHT= 1776
I want to put the width fill parent and resize the height keeping the aspect ratio. I tried some xml alternatives like `ScaleType.
Someone knows the way to put correctly the bitmap?
Does fitXy not work for the scale type?
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
It's an ImageView so you need to use android:src="#drawable/border" instead of android:background. Also try android:scaleType="fitXY" or cropCenter.
There are different Scale Type Available in Android ImageView
CENTER_INSIDE - used to set Bitmap with keeping bitmap original aspect ratio.
CENTER_CROP - used to set Bitmap in imageview to cover imageview without broken aspect Ratio , rather bitmap set from negative x , y value to cover imageview area.
FIT_XY - used to set whole bitmap into available ImageView height and width means bitmap takes Imageviews Aspect Ratio.
OR
if you want to resize bitmap then take height or width which you want to set and take ratio of height / width if take width or take width/ height if you have next height and multiply repetitive width and height to this ratio .It will change height and width of bitmap but keep Aspect Ratio of original Bitmap .
I'm creating a bitmap wich i need to use as background of a 320 width screen. The problem is that the bitmap is not getting the width i want. It is getting two empty spaces on the left and on the right. It is because it is fitting the height of the screen, but i dont want that, i want to force the bitmap to have the width i want. Doesn't matter if a portion of the height of the bitmap is out of the screen.
i need to force the width when i am adding the image to the layout, i mean that the image must be keep the aspect ratio, but the image must be higher than the height of the screen, and the image must be shown incomplete if the heigh of the image is higher than the height of the screen. Now this is not happening, because the layout is forcing the width to respect the image and show all the height of the image in the height of the screen, then i think the problem is on the layout rules
im creating the bitmap with:
View view;
Bitmap aux = Util.loadImage( filename ); //image loaded but with his original width
Bitmap image = Util.scaleBitmap(aux, 320); //scaling to 320
((ImageView)view).setImageBitmap( resource.image );
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams( 320 , h );
rlp.addRule( RelativeLayout.CENTER_HORIZONTAL );
rlp.addRule( RelativeLayout.ALIGN_PARENT_TOP );
layout.addView( view , rlp );
What am i doing wrong?
Try using this:
((ImageView)view).setScaleType(ImageView.FitCenter);
To see more options see http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType
Since you don't care about the height you should use CENTER_CROP.
Define the scale type and then set the image.
CENTER_CROP - Scale the image uniformly (maintain the image's aspect
ratio) so that both dimensions (width and height) of the image will be
equal to or larger than the corresponding dimension of the view (minus
padding).