Learn how to set a resource image as a rounded image (28) for android ),

Source: Internet
Author: User

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 _

Related Article

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.