Android rounded corner image,

Source: Internet
Author: User

Android rounded corner image,

Android rounded corner Image

Import android. app. Activity;
Import android. app. AlertDialog;
Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. DialogInterface;
Import android. content. Intent;
Import android. content. IntentFilter;
Import android. content. SharedPreferences;
Import android. content. SharedPreferences. Editor;
Import android. graphics. Bitmap;
Import android. graphics. Bitmap. Config;
Import android. graphics. Canvas;
Import android. graphics. Paint;
Import android. graphics. PorterDuff. Mode;
Import android. graphics. porterduduxfermode;
Import android. graphics. Rect;
Import android. graphics. RectF;
Import android. graphics. drawable. BitmapDrawable;
Import android. graphics. drawable. Drawable;
Import android. OS. Bundle;
Import android. telephony. TelephonyManager;
Import android. view. KeyEvent;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. ImageView;
Import android. widget. Toast;

Public class MainActivity extends Activity {


@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Yuanjiaotupian ();
}

Private void yuanjiaotupian (){
// TODO Auto-generated method stub
ImageView iv = (ImageView) findViewById (R. id. iv_main_yuanjiao );
Drawable drawable = getResources (). getDrawable (R. drawable. asd );
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable. getBitmap ();
BitmapDrawable bd = new BitmapDrawable (toRoundCorner (bitmap, 30 ));
Iv. setBackgroundDrawable (bd );
}

/**
* Method for obtaining the rounded corner bitmap
*
* @ Param bitmap
* Bitmap to be converted to rounded corner
* @ Param pixels
* The degree of the rounded corner. The larger the value, the larger the rounded corner.
* @ Return refers to the rounded corner bitmap after processing.
*/
Public static Bitmap toRoundCorner (Bitmap bitmap, int pixels ){
Bitmap output = Bitmap. createBitmap (bitmap. getWidth (),
Bitmap. getHeight (), Config. ARGB_8888 );
Canvas canvas = new Canvas (output );
Final int color = 0xff0000242;
Final Paint paint = new Paint ();
Final Rect rect = new Rect (0, 0, bitmap. getWidth (), bitmap. getHeight ());
Final RectF rectF = new RectF (rect );
Final float roundPx = pixels;
Paint. setAntiAlias (true );
Canvas. drawARGB (0, 0, 0, 0 );
Paint. setColor (color );
Canvas. drawRoundRect (rectF, roundPx, roundPx, paint );
Paint. setXfermode (new porterduxfermode (Mode. SRC_IN ));
Canvas. drawBitmap (bitmap, rect, rect, paint );
Return output;
}

}

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.