Android profile picture, android profile picture
DIY round Avatar
Currently, most apps use circular portraits, and there are many open-source images on the Internet. But have you considered DIY circular portraits? Next we will implement one by ourselves. Let's take a look at the demo.
Step 1: Explanation of the principle (the picture is ugly and the principle is true)
1. Draw an external circle. Use a custom color to draw a circle, which is 5 DP longer than the image's radius.
2. After the custom circle is drawn, we need to convert the image into a circular Avatar and draw it on it. The image is overwritten from the middle to obtain the image.
3. intercept an image. Set the radius to r = min (width, height), center to cx = width/2, cy = height/2, in this way, the circular image can be captured from the center.
Step 2: code implementation
1. inherit the ImageView
2. Override the onDraw (Canvas canvas) method.
3. Set the image scaling type setScaleType (ScaleType. CENTER_CROP); intercept
The Code is as follows:
[Java]View plaincopy
- @ Override
- Protected void onDraw (Canvas canvas ){
- // Super. onDraw (canvas );
- SetScaleType (ScaleType. CENTER_CROP );
- Drawable drawable = getDrawable ();
- If (null = drawable ){
- Return;
- }
- // Convert drawable to bitmap ==> found online
- Bitmap bitmap = Bitmap
- . CreateBitmap (
- Drawable. getIntrinsicWidth (),
- Drawable. getIntrinsicHeight (),
- Drawable. getOpacity ()! = PixelFormat. OPAQUE? Bitmap. Config. ARGB_8888
- : Bitmap. Config. RGB_565 );
- Canvas srcCanvas = new Canvas (bitmap );
- Drawable. setBounds (0, 0, drawable. getIntrinsicWidth (),
- Drawable. getIntrinsicHeight ());
- Drawable. draw (srcCanvas );
- Float cx = getWidth ()/2;
- Float cy = getHeight ()/2;
- Float radius = Math. min (getWidth (), getHeight ()/2;
- Paint borderPaint = new Paint ();
- BorderPaint. setAntiAlias (true );
- BorderPaint. setColor (Color. GREEN );
- Canvas. drawCircle (cx, cy, radius, borderPaint );
- // Draw
- BitmapShader shader = new BitmapShader (bitmap, TileMode. CLAMP,
- TileMode. CLAMP );
- Paint paint = new Paint ();
- Paint. setShader (shader );
- Paint. setAntiAlias (true );
- Canvas. drawCircle (cx, cy, radius-5, paint );
- }
The code is just a simple demo. Of course, you can customize it to a general CircleImageView. You need to DIY it as needed !!
Other brilliant articles
Android KSOAP2 call. net webservicejQuery tutorial (8)-DOM tree operation using reverse Insertion Method android learning notes (34) using AlertDialog to create a simple dialog box android learning notes (33) Gallery view (Gallery) android navidgation drawer in the navigation drawer how to change the List of selected items...