Bitmap rectangle picture loaded as a circular picture

Source: Internet
Author: User
Tags throw exception

Package com.lib.andlib_libtemp.example.dir.exam1;

Import Android.graphics.Bitmap;
Import Android.graphics.Matrix;
Import Android.view.View;

Import Com.lib.andlib_libtemp.utils.LogUtil;

/**
* Created by ENVY15 on 2016/6/16.
* Cut picture size based on view Scale tool class
* Cut mode: middle, top, bottom
*/
public class Bitmapcatutil {
public static Bitmap Imagecatbycenter (View mView, Bitmap source) {
Get the height of the view first, get the failure to return the original
Getimageviewpixsize (MView);
/**
* Get the picture bitmap width height
*/
int suwidth = Source.getwidth ();
int suheight = Source.getheight ();
if (Ivwidth = = 0 | | ivheight = = 0)
{
Logutil.infoe ("Get View width height failure: ivheight=" + ivheight + "ivwidth=" + ivwidth);
return source;
}

/**
* Length to be intercepted in the Y direction
*/
int yaxisadd = Ivheight * suwidth/ivwidth;

if (Yaxisadd = = suheight) {//!!!
When the width of the crop is the same as the width of the picture, the return reference of the bitmap is source and therefore cannot be reclaimed and returned directly
Recovery Throw Exception: Java.lang.IllegalStateException:Transformation sizetransformation returned input Bitmap but recycled it
return source;
} else {
Proportional interception of excess height
if (Yaxisadd < suheight)//intercept height less than the height of the source image
{
(to the left of the starting point, intercept width, intercept height)
Bitmap Bitmap = Bitmap.createbitmap (source,
0, SUHEIGHT/2-YAXISADD/2,
Suwidth,yaxisadd);
Source.recycle ();
return bitmap;
} else {//otherwise the width should be scaled
int xaxisadd = Ivwidth * suheight/ivheight;
Bitmap Bitmap = Bitmap.createbitmap (source,
SUWIDTH/2-XAXISADD/2, 0,
Xaxisadd, Suheight);
Source.recycle ();

return bitmap;
}
}
}

/**
* Returns the width-height pixel size represented by the picture control
* You must specify the size of the image, Wrap_content/match_parent cannot get the width height
* Reference
* http://blog.csdn.net/crazy1235/article/details/41806079
*/
private static int ivheight;
private static int ivwidth;
private static void Getimageviewpixsize (View imageView) {
Get wide-height OK
Ivheight = Imageview.getlayoutparams (). Height;
Ivwidth = Imageview.getlayoutparams (). width;
}

/**
* Get Bitmap zoom ratio
*/
public static float Catscale (View mView, Bitmap Bitmap) {
Return Mview.getlayoutparams (). Height*1.0f/bitmap.getheight ();
}

/**
* Bitmap Amplification/reduction method
* @param bitmap
* @return Bitmap
*/
public static Bitmap bitmapscaling (Bitmap bitmap,float scale) {
Matrix matrix = new Matrix ();
Matrix.postscale (Scale,scale); Ratio of long and wide magnification reduction
Bitmap resizebmp = Bitmap.createbitmap (Bitmap,0,0,bitmap.getwidth (), Bitmap.getheight (), matrix,true);
return resizebmp;
}
}

+++++++++++++++++++++++++++++++
Package com.lib.andlib_libtemp.example.dir.exam1;

Import Android.graphics.Bitmap;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.PorterDuff;
Import Android.graphics.PorterDuffXfermode;
Import Android.graphics.Rect;
Import Android.graphics.RectF;
Import android.graphics.drawable.BitmapDrawable;
Import android.graphics.drawable.Drawable;
Import Android.view.View;

/**
* Created by ENVY15 on 2016/6/16.
* Bitmap Drawing Tool class: Rounded corners
*
*/
public class Bitmapshapeutil {
/** four rounded corners of a picture
* Four rounded corners, Roundradius = Bitmap.getwidth ()/2 drawn as a circular image
* @param bitmap bitmap
* @param roundradius fillet size
* @return Fillet picture
*/
public static Bitmap Getroundedcornerbitmap (Bitmap bitmap,float Roundradius) {
Creates an empty graph object with a specified width and height
Bitmap output = Bitmap.createbitmap (Bitmap.getwidth (),
Bitmap.getheight (), Bitmap.Config.ARGB_8888);
Create a canvas with this bitmap
Canvas canvas = new canvas (output);
Brush objects
Final Paint paint = new paint ();
The color of the brush
final int color = 0xff424242;
Rectangular Area Object
Final rect rect = new Rect (0, 0, bitmap.getwidth (), Bitmap.getheight ());
Unknown
Final RECTF RECTF = new RECTF (rect);
Radius of the Corner
Final float roundpx = Roundradius;
Anti-aliasing
Paint.setantialias (TRUE);
Canvas background color
Canvas.drawargb (0, 0, 0, 0);
Set Brush color
Paint.setcolor (color);
Draw rounded rectangles
Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint);
Unknown
Paint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.SRC_IN));
Draw the picture in the rectangular area of the fillet
Canvas.drawbitmap (Bitmap, rect, rect, paint);
Finally, the rounded rectangle is rendered on the canvas, and then we return to the bitmap object
return output;
}

/** enables dynamic drawing of fillets
* Create four rounded backgrounds
* @param mView calculate drawable size based on view
* @param Roundradius Draw radius
* @return drawable
* Exam Imageview.setbackground (bitmapshapeutil.getroundedcornerdrawable (Imageview,imageview.
* Getlayoutparams (). WIDTH/5, Color.gray));
*/
public static drawable getroundedcornerdrawable (View mView, float roundradius,int color) {
int hight = Mview.getlayoutparams (). Height;
int width = Mview.getlayoutparams (). width;
Bitmap rounder = Bitmap.createbitmap (width, hight, Bitmap.Config.ARGB_8888);
Canvas canvas = new canvas (Rounder);
Paint Xferpaint = new paint (Paint.anti_alias_flag);
Xferpaint.setcolor (color);
Canvas.drawroundrect (New RECTF (0, 0, Width, hight), Roundradius, Roundradius, Xferpaint);
Xferpaint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.DST_IN));
Canvas.drawbitmap (Rounder, 0, 0, NULL);
Canvas.drawbitmap (Rounder, 0, 0, xferpaint);

drawable drawable = new bitmapdrawable (Rounder);
return drawable;
}
}
+++++++++++++++++++++++++++++++

@Override
public void Initdatas () {
Logutil.infod (This, "Initdatas");
Ok
Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), r.mipmap.image_girl_test1);
Logutil.infoe (This,bitmap.getheight () + "" +bitmap.getwidth ());
Crop a picture
Bitmap = Bitmapcatutil.imagecatbycenter (Imageview,bitmap);
Logutil.infoe (This,bitmap.getheight () + "" +bitmap.getwidth ());
Logutil.infoe (This,imageview.getlayoutparams (). height+ "" +imageview.getlayoutparams (). width);
Scaling processing
Bitmap = bitmapcatutil.bitmapscaling (Bitmap,bitmapcatutil.catscale (Imageview,bitmap));
Fillet processing
Bitmap Roundedcornerbitmap = Bitmapshapeutil.getroundedcornerbitmap (
Bitmap,bitmap.getwidth ()/2);
Imageview.setimagebitmap (ROUNDEDCORNERBITMAP);


Imageview.setbackground (Bitmapshapeutil.getroundedcornerdrawable (Imageview,imageview.
Getlayoutparams (). WIDTH/5, Color.gray));


}





Bitmap rectangle picture loaded as a circular picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.