Learn how to set a resource image as a rounded image (28) for android ),
:
See the following code:
Public class MainActivity extends Activity {private ImageView imageView1; private ImageView imageView2; Bitmap mBitmap; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. image); initView ();} private void initView () {imageView1 = (ImageView) findViewById (R. id. imageView1); imageView2 = (ImageView) findViewById (R. id. imageView2); // read the resource image mBitmap = readBitMap (); // scale the resource image Bitmap bitmap = zoomBitmap (mBitmap, mBitmap. getWidth ()/2, mBitmap. getHeight ()/2); // set the rounded corner image imageView2.setImageBitmap (setRoundedCorner (bitmap, 20f);}/*** read the resource image * @ return */private Bitmap readBitMap () {BitmapFactory. options opt = new BitmapFactory. options ();/** sets the decoder to be decoded in the best way */opt. inPreferredConfig = Bitmap. config. RGB_565; // use opt in combination with the following two fields. inPurgeable = true; opt. ininputtransferable = true;/** get resource image */InputStream is = this. getResources (). openRawResource (R. drawable. mei); return BitmapFactory. decodeStream (is, null, opt);}/*** zoom image * @ param bitmap * @ param w * @ param h * @ return */public Bitmap zoomBitmap (Bitmap bitmap, int w, int h) {int width = bitmap. getWidth (); int height = bitmap. getHeight (); Matrix matrix = new Matrix (); float scaleWidht = (float) w/width); float scaleHeight = (float) h/height ); /** scale by using the postScale method of the Matrix class */matrix. postScale (scaleWidht, scaleHeight); Bitmap newbmp = Bitmap. createBitmap (bitmap, 0, 0, width, height, matrix, true); return newbmp ;} /*** set the image to a rounded corner * @ param bitmap * @ param roundPx rounded corner angle * @ return */public Bitmap setRoundedCorner (Bitmap bitmap, float roundPx) {Bitmap output = Bitmap. createBitmap (bitmap. getWidth (), bitmap. getHeight (), Config. ARGB_8888); Canvas canvas = new Canvas (output); final Paint paint = new Paint (); final Rect rect = new Rect (0, 0, bitmap. getWidth (), bitmap. getHeight ();/** elliptical */final RectF rectF = new RectF (rect);/** de-sawtooth */paint. setAntiAlias (true); canvas. drawARGB (0, 0, 0, 0);/** draw a rounded rectangle */canvas. drawRoundRect (rectF, roundPx, roundPx, paint);/** set the intersection of two images */paint. setXfermode (new porterduduxfermode (Mode. SRC_IN); canvas. drawBitmap (bitmap, rect, rect, paint); return output ;}}
Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/44314043 sentiment control _