Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/layout"
Android: orientation = "vertical"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
>
</LinearLayout>
Package com. test. demo;
Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. graphics. Bitmap. Config;
Import android. graphics. Canvas;
Import android. graphics. Color;
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. widget. ImageView;
Import android. widget. LinearLayout;
Public class MyActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Drawable drawable = getResources (). getDrawable (R. drawable. bg );
// BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
// Bitmap bitmap = bitmapDrawable. getBitmap ();
LinearLayout layout = (LinearLayout) findViewById (R. id. layout );
Drawable drawable = getResources (). getDrawable (R. drawable. bg );
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable. getBitmap ();
BitmapDrawable bbb = new BitmapDrawable (toRoundCorner (bitmap, 30 ));
Layout. setBackgroundDrawable (bbb );
// ImageView imageView = (ImageView) findViewById (R. id. imgShow );
// ImageView. setImageBitmap (MyActivity. getRoundedCornerBitmap (bitmap ));
// ImageView. setImageBitmap (MyActivity. toRoundCorner (bitmap, 20 ));
}
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;
}
}