【android】 圓角頭像

來源:互聯網
上載者:User

標籤:android   des   style   blog   color   get   

I:畫圓角:

 1   private void roundImg(ImageView iv){ 2  3   /**本地資源*/ 4  5   InputStream is = getResources().openRawResource(R.drawable.icon); 6  7   Bitmap bitmap = BitmapFactory.decodeStream(is); 8  9   /**網路資源*/10 11   //FileInputStream fis = new FileInputStream(url);12 13   //Bitmap bitmap = BitmapFactory.decodeStream(fis);14 15     if(bitmap != null){16 17     Bitmap zoomBitmap = ImageUtil.zoomBitmap(bitmap,100,100);18 19     Bitmap roundBitmap = ImageUtil.getRoundedCornerBitmap(zoomBitmap,50.0f);20 21     iv.setImageBitmap(roundBitmap);22 23     }24 25   }

 II:ImageUtil工具類

 1 public class ImageUtil { 2  3     public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) { 4  5         int width = bitmap.getWidth(); 6         int height = bitmap.getHeight(); 7         /** 8          *  9          */10         Matrix matrix = new Matrix();11         float scaleWidth = (float) w / width;12         float scaleHeight = (float) h / height;13         matrix.postScale(scaleWidth, scaleHeight);14         Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);15         return newbmp;16     }17 18     19     public static Bitmap drawableToBitmap(Drawable drawable) {20         int width = drawable.getIntrinsicWidth();21         int height = drawable.getIntrinsicHeight();22         Bitmap bitmap = Bitmap.createBitmap(width, height,23                 drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_888824                         : Bitmap.Config.RGB_565);25         Canvas canvas = new Canvas(bitmap);26         drawable.setBounds(0, 0, width, height);27         drawable.draw(canvas);28         return bitmap;29     }30 31     32     public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {33 34         Bitmap output = Bitmap35                 .createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);36         Canvas canvas = new Canvas(output);37         final int color = 0xff424242;38         final Paint paint = new Paint();39         final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());40         final RectF rectf = new RectF(rect);41 42         paint.setAntiAlias(true);43         canvas.drawARGB(0, 0, 0, 0);44         paint.setColor(color);45         canvas.drawRoundRect(rectf, roundPx, roundPx, paint);46 47         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));48         canvas.drawBitmap(bitmap, rect, rect, paint);49         return output;50     }51 52     public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {53         final int reflectionGap = 4;54         int width = bitmap.getWidth();55         int height = bitmap.getHeight();56 57         Matrix matrix = new Matrix();58         matrix.preScale(1, -1);59 60         Bitmap reflectionImage = Bitmap.createBitmap(bitmap,61                 0, height / 2, width, height / 2, matrix, false);62 63         Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2),64                 Config.ARGB_8888);65 66         Canvas canvas = new Canvas(bitmapWithReflection);67         canvas.drawBitmap(bitmap, 0, 0, null);68         Paint deafalutPaint = new Paint();69         canvas.drawRect(0, height, width, height + reflectionGap,70                 deafalutPaint);71 72         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);73 74         Paint paint = new Paint();75         LinearGradient shader = new LinearGradient(0,76                 bitmap.getHeight(), 0, bitmapWithReflection.getHeight()77                         + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);78         paint.setShader(shader);79         // Set the Transfer mode to be porter duff and destination in80         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));81         // Draw a rectangle using the paint with our linear gradient82         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()83                 + reflectionGap, paint);84 85         return bitmapWithReflection;86     }87     88 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.