(Turn) create a circular Avatar

Source: Internet
Author: User

First, create an abstract class maskedimage that inherits imageview. Let him rewrite the ondraw method. The Code is as follows:

 

[Java]View plaincopyprint?
  1. Public abstract class maskedimage extends imageview {
  2. Private Static final xfermode mask_xfermode;
  3. Private bitmap mask;
  4. Private paint;
  5.  
  6. Static {
  7. Porterduff. Mode localmode = porterduff. mode. dst_in;
  8. Mask_xfermode = new porterduxfermode (localmode );
  9. }
  10.  
  11. Public maskedimage (context paramcontext ){
  12. Super (paramcontext );
  13. }
  14.  
  15. Public maskedimage (context paramcontext, attributeset paramattributeset ){
  16. Super (paramcontext, paramattributeset );
  17. }
  18.  
  19. Public maskedimage (context paramcontext, attributeset paramattributeset, int paramint ){
  20. Super (paramcontext, paramattributeset, paramint );
  21. }
  22.  
  23. Public abstract bitmap createmask ();
  24.  
  25. Protected void ondraw (canvas paramcanvas ){
  26. Drawable localdrawable = getdrawable ();
  27. If (localdrawable = NULL)
  28. Return;
  29. Try {
  30. If (this. Paint = NULL ){
  31. Paint localpaint1 = new paint ();
  32. This. Paint = localpaint1;
  33. This. Paint. setfilterbitmap (false );
  34. Paint localpaint2 = This. paint;
  35. Xfermode localxfermode1 = mask_xfermode;
  36. @ Suppresswarnings ("UNUSED ")
  37. Xfermode localxfermode2 = localpaint2.setxfermode (localxfermode1 );
  38. }
  39. Float F1 = getwidth ();
  40. Float F2 = getheight ();
  41. Int I = paramcanvas. savelayer (0.0f, 0.0f, F1, F2, null, 31 );
  42. Int J = getwidth ();
  43. Int K = getheight ();
  44. Localdrawable. setbounds (0, 0, j, k );
  45. Localdrawable. Draw (paramcanvas );
  46. If (this. Mask = NULL) | (this. Mask. isrecycled ())){
  47. Bitmap localbitmap1 = createmask ();
  48. This. Mask = localbitmap1;
  49. }
  50. Bitmap localbitmap2 = This. mask;
  51. Paint localpaint3 = This. paint;
  52. Paramcanvas. drawbitmap (localbitmap2, 0.0f, 0.0f, localpaint3 );
  53. Paramcanvas. restoretocount (I );
  54. Return;
  55. } Catch (exception localexception ){
  56. Stringbuilder localstringbuilder = new stringbuilder ()
  57. . Append ("attempting to draw with recycled bitmap. View id = ");
  58. System. Out. println ("localstringbuilder =" + localstringbuilder );
  59. }
  60. }
  61. }
public abstract class MaskedImage extends ImageView {private static final Xfermode MASK_XFERMODE;private Bitmap mask;private Paint paint;static {PorterDuff.Mode localMode = PorterDuff.Mode.DST_IN;MASK_XFERMODE = new PorterDuffXfermode(localMode);}public MaskedImage(Context paramContext) {super(paramContext);}public MaskedImage(Context paramContext, AttributeSet paramAttributeSet) {super(paramContext, paramAttributeSet);}public MaskedImage(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {super(paramContext, paramAttributeSet, paramInt);}public abstract Bitmap createMask();protected void onDraw(Canvas paramCanvas) {Drawable localDrawable = getDrawable();if (localDrawable == null)return;try {if (this.paint == null) {Paint localPaint1 = new Paint();this.paint = localPaint1;this.paint.setFilterBitmap(false);Paint localPaint2 = this.paint;Xfermode localXfermode1 = MASK_XFERMODE;@SuppressWarnings("unused")Xfermode localXfermode2 = localPaint2.setXfermode(localXfermode1);}float f1 = getWidth();float f2 = getHeight();int i = paramCanvas.saveLayer(0.0F, 0.0F, f1, f2, null, 31);int j = getWidth();int k = getHeight();localDrawable.setBounds(0, 0, j, k);localDrawable.draw(paramCanvas);if ((this.mask == null) || (this.mask.isRecycled())) {Bitmap localBitmap1 = createMask();this.mask = localBitmap1;}Bitmap localBitmap2 = this.mask;Paint localPaint3 = this.paint;paramCanvas.drawBitmap(localBitmap2, 0.0F, 0.0F, localPaint3);paramCanvas.restoreToCount(i);return;} catch (Exception localException) {StringBuilder localStringBuilder = new StringBuilder().append("Attempting to draw with recycled bitmap. View ID = ");System.out.println("localStringBuilder=="+localStringBuilder);}}}

 

 

 

Create a new circularimage class that inherits the maskedimage. The Code is as follows:

 

 

[Java]View plaincopyprint?
  1. <Span style = "font-size: 14px"> public class circularimage extends maskedimage {
  2. Public circularimage (context paramcontext ){
  3. Super (paramcontext );
  4. }
  5.  
  6. Public circularimage (context paramcontext, attributeset paramattributeset ){
  7. Super (paramcontext, paramattributeset );
  8. }
  9.  
  10. Public circularimage (context paramcontext, attributeset paramattributeset, int paramint ){
  11. Super (paramcontext, paramattributeset, paramint );
  12. }
  13.  
  14. Public bitmap createmask (){
  15. Int I = getwidth ();
  16. Int J = getheight ();
  17. Bitmap. config localconfig = bitmap. config. argb_8888;
  18. Bitmap localbitmap = bitmap. createbitmap (I, j, localconfig );
  19. Canvas localcanvas = new canvas (localbitmap );
  20. Paint localpaint = new paint (1 );
  21. Localpaint. setcolor (-16777216 );
  22. Float F1 = getwidth ();
  23. Float F2 = getheight ();
  24. Rectf localrectf = new rectf (0.0f, 0.0f, F1, F2 );
  25. Localcanvas. drawoval (localrectf, localpaint );
  26. Return localbitmap;
  27. }
  28. } </Span>
public class CircularImage extends MaskedImage {public CircularImage(Context paramContext) {super(paramContext);}public CircularImage(Context paramContext, AttributeSet paramAttributeSet) {super(paramContext, paramAttributeSet);}public CircularImage(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {super(paramContext, paramAttributeSet, paramInt);}public Bitmap createMask() {int i = getWidth();int j = getHeight();Bitmap.Config localConfig = Bitmap.Config.ARGB_8888;Bitmap localBitmap = Bitmap.createBitmap(i, j, localConfig);Canvas localCanvas = new Canvas(localBitmap);Paint localPaint = new Paint(1);localPaint.setColor(-16777216);float f1 = getWidth();float f2 = getHeight();RectF localRectF = new RectF(0.0F, 0.0F, f1, f2);localCanvas.drawOval(localRectF, localPaint);return localBitmap;}}

Create a mainactivity with the following code:

 

 

[Java]View plaincopyprint?
  1. Public class mainactivity extends activity {
  2.  
  3. @ Override
  4. Protected void oncreate (bundle savedinstancestate ){
  5. Super. oncreate (savedinstancestate );
  6. Setcontentview (R. layout. activity_main );
  7. Circularimage cover_user_photo = (circularimage) findviewbyid (R. Id. cover_user_photo );
  8. Cover_user_photo.setimageresource (R. drawable. Face );
  9. }
  10. }
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);CircularImage cover_user_photo = (CircularImage) findViewById(R.id.cover_user_photo);cover_user_photo.setImageResource(R.drawable.face);}}

 

 

The XML layout file is:

 

[HTML]View plaincopyprint?
  1. <Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  2. Xmlns: Tools = "http://schemas.android.com/tools"
  3. Android: layout_width = "match_parent"
  4. Android: layout_height = "match_parent"
  5. Android: gravity = "center">
  6.  
  7. <Imageview
  8. Android: layout_width = "82.0dip"
  9. Android: layout_height = "82.0dip"
  10. Android: layout_centerinparent = "true"
  11. Android: contentdescription = "@ null"
  12. Android: src = "@ drawable/me_head_bg"/>
  13.  
  14. <Com. doublefi123.diary. widget. circularimage
  15. Android: Id = "@ + ID/cover_user_photo"
  16. Android: layout_width = "74.0dip"
  17. Android: layout_height = "74.0dip"
  18. Android: layout_centerinparent = "true"/>
  19.  
  20. </Relativelayout>

(Turn) create a circular Avatar

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.