How can I crop a bitmap for ImageView?
Alright, I will paste the comment as answer :) ->
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl1);
final Options bitmapOptions=new Options();
DisplayMetrics metrics = getResources().getDisplayMetrics();
bitmapOptions.inDensity = metrics.densityDpi;
bitmapOptions.inTargetDensity=1;
/*`final` modifier might be necessary for the Bitmap*/
Bitmap bmp= BitmapFactory.decodeResource(getResources(), R.drawable.ver_bottom_panel_tiled_long, bitmapOptions);
bmp.setDensity(Bitmap.DENSITY_NONE);
bmp = Bitmap.createBitmap(bmp, 0, 0, rl.getWidth(), bmp.getHeight());
Then in the code:
ImageView iv = (ImageView)v.findViewById(R.id.ver_bottompanelprayer);
if (iv != null){
iv.setImageBitmap(bmp);
}
Cheers :)
How to crop Bitmap Center like imageview?
Your question is a bit short of information on what you want to accomplish, but I guess you have a Bitmap and want to scale that to a new size and that the scaling should be done as "centerCrop" works for ImageViews.
From Docs
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).
As far as I know, there is no one-liner to do this (please correct me, if I'm wrong), but you could write your own method to do it. The following method calculates how to scale the original bitmap to the new size and draw it centered in the resulting Bitmap.
Hope it helps!
public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
// Compute the scaling factors to fit the new height and width, respectively.
// To cover the final image, the final scaling will be the bigger
// of these two.
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.max(xScale, yScale);
// Now get the size of the source bitmap when scaled
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;
// Let's find out the upper left coordinates if the scaled bitmap
// should be centered in the new size give by the parameters
float left = (newWidth - scaledWidth) / 2;
float top = (newHeight - scaledHeight) / 2;
// The target rectangle for the new, scaled version of the source bitmap will now
// be
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
// Finally, we create a new bitmap of the specified size and draw our new,
// scaled bitmap onto it.
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, targetRect, null);
return dest;
}
How to crop a bitmap from bottom part and show it imageview
Guys Use Transform form Picasso lib where you crop images based on gravity.
Use below code to cutoff at bottom
Picasso.with(mContext).load(offerImage.getUrl()).transform(new CropTransformation(500,150), CropTransformation.GravityHorizontal.CENTER,CropTransformation.GravityVertical.TOP)).into(holder.ivOfferImage);
For more details check here - https://github.com/wasabeef/picasso-transformations/blob/master/transformations/src/main/java/jp/wasabeef/picasso/transformations/CropTransformation.java
Thanks.
Where to call Crop Image Before Setting to ImageView
try this one
try{
Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
bm = Bitmap.createBitmap(bm, 0, 0, 400, 400);
your_imageview.setImageBitmap(bm);
}catch(Exception e){
e.printStackTrace();
}
Related Topics
How to Start a New Android Activity Using Class Name in a String
Unhandled Exception Type Error
How to Detect a Click in an Ontouch Listener
How to Use Flood Fill Algorithm in Android
Android - How to Create Clickable Listview
How to Show Ellipses on My Textview If It Is Greater Than the 1 Line
How to Persist Permission in Android API 19 (Kitkat)
Urlconnection Getcontentlength() Is Returning a Negative Value
Fragment Add or Replace Not Working
Android App Crashes After Sdk-Tools Update Version (Noclassdeffound, Tool Version 22)
Multiple Dex Files Define Landroid/Support/Annotation/Animres
Android: Google Maps API - Change Position of Maps Toolbar
Method Called After Release() Exception Unable to Resume with Android Camera
Convert Normal Java Array or Arraylist to JSON Array in Android
How to Get String Response from Retrofit2
How to Update One Field from All Documents Using Pojo in Firestore