1. 程式人生 > >Android自定義ImageView,方角,圓角頭像(可根據自己的需求來設定頭像的圓角或者方角的大小)

Android自定義ImageView,方角,圓角頭像(可根據自己的需求來設定頭像的圓角或者方角的大小)

直接上程式碼,只要改程式碼的一處地方即可根據自己的需求來設定圖片的圓角的大小

在xml中引用自定義的ImageView即可

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
* @ClassName: XCRoundRectImageView
* @Description: 自定義控制元件實現方角圖片
* @author 菲爾普斯
* @date 2015-1-5 下午3:59:28
*
*/
public class XCRoundRectImageView extends ImageView {

     private Paint paint;

     public XCRoundRectImageView(Context context) {
          this(context, null);
     }

     public XCRoundRectImageView(Context context, AttributeSet attrs) {
          this(context, attrs, 0);
     }

     public XCRoundRectImageView(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
          paint = new Paint();
     }

     /**
     * 繪製圓角矩形圖片
     *
     * @author caizhiming
     */
     @SuppressLint("DrawAllocation")
     @Override
     protected void onDraw(Canvas canvas) {
          Drawable drawable = getDrawable();
          if (drawable != null) {
               Bitmap bitmap = drawableToBitmap(drawable);
               Bitmap b = getRoundBitmap(bitmap,360);       //360為一個圓形!改變這個值,可以根據自己的需求來設定
               final Rect rectSrc = new Rect(0, 0, b.getWidth(), b.getHeight());
               final Rect rectDest = new Rect(0, 0, getWidth(), getHeight());
               paint.reset();
               canvas.drawBitmap(b, rectSrc, rectDest, paint);

          } else {
               super.onDraw(canvas);
          }
     }

     /***
     * @Title: drawableToBitmap
     * @Description: Drawble轉為bitmap
     * @param @param drawable
     * @param @return
     * @return Bitmap
     * @throws
     */
     public static Bitmap drawableToBitmap(Drawable drawable) {
          Bitmap bitmap;
          if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
               bitmap = Bitmap.createBitmap(100, 100, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
          } else {
               bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                         : Bitmap.Config.RGB_565);
          }
          Canvas canvas = new Canvas(bitmap);
          drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
          drawable.draw(canvas);
          return bitmap;
     }

     /**
     * 獲取圓角矩形圖片方法
     *
     * @param bitmap
     * @param roundPx
     *            ,一般設定成20
     * @return Bitmap
     * @author caizhiming
     */
     private Bitmap getRoundBitmap(Bitmap bitmap, int roundPx) {
          Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
          Canvas canvas = new Canvas(output);

          final int color = 0xff424242;

          final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
          final RectF rectF = new RectF(rect);
          paint.setAntiAlias(true);
          canvas.drawARGB(0, 0, 0, 0);
          paint.setColor(color);
          canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
          paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
          canvas.drawBitmap(bitmap, rect, rect, paint);
          return output;

     }
}