The example in this article describes the Android programming method for drawing circular pictures. Share to everyone for your reference, specific as follows:
The effect chart is as follows:
First step: Create new Roundview custom control inherit view
Package com.rong.activity;
Import COM.RONG.TEST.R;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.Bitmap.Config;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.PorterDuff;
Import Android.graphics.PorterDuffXfermode;
Import Android.util.AttributeSet;
Import Android.widget.ImageView; /** * Use Xfermode to make a circular picture * * * @author Xu Rong */public class Roundview extends ImageView {/** * Brush/Privat
e Paint mpaint;
/** * Prototype Map * * Private Bitmap src;
/** * Mask Layer * * Private Bitmap mask;
Public Roundview (context, AttributeSet attrs) {Super (context, attrs);
Initializes the brush mpaint = new Paint ();
Mpaint.setantialias (TRUE);
Get the prototype figure src = Bitmapfactory.decoderesource (getresources (), r.drawable.ml); @Override protected void onsizechanged (int w, int h, int oldw, int oldh) {Super.onsizechAnged (W, H, OLDW, OLDH); The mask of the picture, why do you want to initialize the mask layer in this area? Because the width () and height () of this method are initialized mask = Bitmap.createbitmap (Getmeasuredwidth (), Getmeasuredheight (), config.argb_4444
);
} @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
Set the color of the canvas to Transparent canvas.drawcolor (color.transparent);
Underline the circle Canvas cc = new Canvas (mask) that you want to display;
Cc.drawcircle (Getmeasuredwidth ()/2, Getmeasuredheight ()/2, Getmeasuredheight ()/2, mpaint); This method is equivalent to PS new layer, the next thing you want to do is to operate on this layer Canvas.savelayer (0, 0, getmeasuredwidth (), getmeasuredheight (), NULL, CANVAS.ALL_SA
Ve_flag);
First draw the Mask layer Canvas.drawbitmap (mask, 0, 0, mpaint);
Sets the blending mode Mpaint.setxfermode (new Porterduffxfermode (PorterDuff.Mode.SRC_IN));
Then draw the SRC source diagram canvas.drawbitmap (src, 0, 0, mpaint);
Restore blending mode Mpaint.setxfermode (NULL);
Restore the canvas, equivalent to PS of the merged layer Canvas.restore ();
}
}
Step two to create a new layout file Activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
android: Background= "#ffffff"
android:orientation= "vertical" >
<com.rong.activity.roundview
android: Layout_width= "160DP"
android:layout_height= "160DP"
android:layout_centerinparent= "true"/>
< /relativelayout>
Run!
More interested readers of Android-related content can view the site's topics: Introduction to Android Development and advanced tutorials, the summary of Android basic components usage, the overview of Android View View techniques, the Android Layout layout skills Summary, and A summary of the usage of Android controls
I hope this article will help you with the Android program.