日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实现圆形图片

發布時間:2025/4/16 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实现圆形图片 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實現原理:

  • 得到原圖的寬高,計算出圓心,取圖片的短邊為基準半徑。
  • 創建空白Bitmap,大小為基準半徑的正方形。
  • 位移原圖,使其中心點和創建的空白圖中心點重合。
  • 利用Android的Paint繪制疊加圖,只繪制重疊部分
  • 效果完成。
  • public class CircleImageView extends ImageView{public CircleImageView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public CircleImageView(Context context, AttributeSet attrs) {super(context, attrs);}public CircleImageView(Context context) {super(context);}@Overridepublic void setBackgroundDrawable(Drawable background) {super.setBackgroundDrawable(getCircleDrawable(getResources(), background));}@Overridepublic void setBackgroundResource(int resid) {//Don't worry, we don't need to override it,because it will be call //setBackgroundDrawable(Drawable background)super.setBackgroundResource(resid);}@Overridepublic void setImageBitmap(Bitmap bm) {//Don't worry, we don't need to override it,because it will be call //setImageDrawable(Drawable drawable)super.setImageBitmap(bm);}@Overridepublic void setImageDrawable(Drawable drawable) {super.setImageDrawable(getCircleDrawable(getResources(), drawable));}@Overridepublic void setImageURI(Uri uri) {//cheat it, let's change the way to implementsuper.setImageURI(uri);Drawable img = getCircleDrawable(getResources(), getDrawable());super.setImageDrawable(img);}@Overridepublic void setImageResource(int resId) {//cheat it, let's change the way to implementDrawable img = getCircleDrawable(getResources(), resId);super.setImageDrawable(img);}private static final int SPACING_LINE = 2;private static Paint mCirclePaint = null;private static Paint mLinePaint = null;private static Paint getCirclePaint(){if(mCirclePaint == null){mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);}return mCirclePaint;}private static Paint getLinePaint(){if(mLinePaint == null){mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);mLinePaint.setStyle(Style.STROKE);//You can use it to change the width of the linemLinePaint.setStrokeWidth(1);//You can use it to change the color of the linemLinePaint.setColor(Color.BLACK);}return mLinePaint;}/*** You can call this method to generate the circular bitmap, * even if you don't use this class*/public static Bitmap getCircleBitmap(Bitmap src){if(src == null){return null;}int width = src.getWidth();int height = src.getHeight();int centerX = width / 2;int centerY = height / 2;int radius = Math.min(centerX, centerY) / 2;Bitmap result = Bitmap.createBitmap(radius * 2, radius * 2, Config.ARGB_8888);Canvas canvas = new Canvas(result);canvas.drawCircle(radius, radius, radius - SPACING_LINE, getCirclePaint());getCirclePaint().setXfermode(new PorterDuffXfermode(Mode.SRC_IN));canvas.drawBitmap(src, -(centerX - radius), -(centerY - radius), getCirclePaint());//outer canvas.drawCircle(radius, radius, radius, getLinePaint());//innercanvas.drawCircle(radius, radius, radius - SPACING_LINE, getLinePaint());//resetgetCirclePaint().setXfermode(null);//recyclesrc.recycle();return result;}public static Bitmap getCircleBitmap(Drawable src){if(src instanceof BitmapDrawable){return getCircleBitmap(((BitmapDrawable)src).getBitmap());}else{//now, i don't know how to do...throw new UnsupportedException("Unsupported");}}public static Bitmap getCircleBitmap(Resources res,int id){return getCircleBitmap(BitmapFactory.decodeResource(res, id));}public static Drawable getCircleDrawable(Resources res, Bitmap src){return new BitmapDrawable(res,getCircleBitmap(src));}public static Drawable getCircleDrawable(Resources res, Drawable src){return new BitmapDrawable(res,getCircleBitmap(src));}public static Drawable getCircleDrawable(Resources res, int id) {return new BitmapDrawable(res, getCircleBitmap(res, id));}static class UnsupportedException extends RuntimeException{ private static final long serialVersionUID = 1L;public UnsupportedException(String str){super(str);}} }

    總結

    以上是生活随笔為你收集整理的实现圆形图片的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。