zoukankan      html  css  js  c++  java
  • android 圆角图片的实现

    图片展示的时候总觉的直角的图片不好看?好办法来了!~~

    public class ToRoundCorner extends Activity{
    
        public Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
    
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
            final int color = 0xff424242;
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            final RectF rectF = new RectF(rect);
            final float roundPx = pixels;
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
            return output;
        }
    
    }

    把上面的代码放到工具包中需要的时候只要调用下就好了!

    ImageView pic = (ImageView) navigationView.getHeaderView(0).findViewById(R.id.tou_pic);
            TextView username = (TextView) navigationView.getHeaderView(0).findViewById(R.id.nav_username);
            TextView phone = (TextView) navigationView.getHeaderView(0).findViewById(R.id.nav_phone);
    
            //将图片转换成bitmap
            Drawable drawable = getResources().getDrawable(R.mipmap.aboutus);
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            Bitmap bitmap = bitmapDrawable.getBitmap();
            //将图片转成圆角
            ToRoundCorner toround = new ToRoundCorner();
            pic.setImageBitmap(toround.toRoundCorner(bitmap , 50));

    用法很简单是不是? 其中最后一行toround.toRoundCorner(bitmap , 50)中bitmap 是要传入的图片,后一个数字越大图片圆角越明显。

    借的图片请不要生气

    方法摘自:脚本之家(http://www.jb51.net/article/32320.htm)

  • 相关阅读:
    杨辉三角形
    open live writer
    已加载"C:WindowsSysWOW64msvcp120d.dll".无法查找或打开 PDB 文件.
    4.标准信号与槽
    python的单元测试unittest(一)
    python面向对象--类与对象
    python的文件操作与目录操作os模块
    Jenkins的安装与配置
    selenium切换到iframe
    selenium对富文本的操作
  • 原文地址:https://www.cnblogs.com/wobeinianqing/p/5076989.html
Copyright © 2011-2022 走看看