58.圆角图片

绘制流程:

1.根据获取的属性值,判断显示模式,并设置圆形图片、外边框、内边框的半径值

2.画边框

3.画图片内容

(1)获取原图

(2)得到正方形图

(3)得到缩放图

(4)得到圆形图

public class MyRoundImageView extends ImageView {

private int defaultColor = 0xffffffff;    private ShowType mShowType = ShowType.NoBorder;    private int mBorderThickness = 0;    private int mBorderInsideColor;    private int mBorderOutsideColor;    private int mRadius = 0;

enum ShowType {InsideBorder,OutsideBorder,InsideAndOutsideBorder,NoBorder}

public MyRoundImageView(Context context) {super(context);}

public MyRoundImageView(Context context, AttributeSet attrs) {super(context, attrs);readAttrs(attrs);}

public MyRoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);readAttrs(attrs);}

private void readAttrs(AttributeSet attrs) {        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RoundImageView);mBorderThickness = a.getDimensionPixelSize(R.styleable.RoundImageView_border_thickness, 0);mBorderInsideColor = a.getColor(R.styleable.RoundImageView_border_inside_color, defaultColor);mBorderOutsideColor = a.getColor(R.styleable.RoundImageView_border_outside_color, defaultColor);readShowType();}

private void readShowType() {if (mBorderInsideColor != defaultColor && mBorderOutsideColor != defaultColor) {mShowType = ShowType.InsideAndOutsideBorder;} else if (mBorderInsideColor != defaultColor && mBorderOutsideColor == defaultColor) {mShowType = ShowType.InsideBorder;} else if (mBorderInsideColor == defaultColor && mBorderOutsideColor != defaultColor) {mShowType = ShowType.OutsideBorder;} else {mShowType = ShowType.NoBorder;}    }

@Overrideprotected void onDraw(Canvas canvas) {switch (mShowType) {case InsideBorder:mRadius = (getWidth() < getHeight() ? getWidth() : getHeight()) / 2 - mBorderThickness;drawRoundBorder(canvas, mRadius + mBorderThickness / 2, mBorderInsideColor);                break;            case OutsideBorder:mRadius = (getWidth() < getHeight() ? getWidth() : getHeight()) / 2 - mBorderThickness;drawRoundBorder(canvas, mRadius + mBorderThickness / 2, mBorderOutsideColor);                break;            case InsideAndOutsideBorder:mRadius = (getWidth() < getHeight() ? getWidth() : getHeight()) / 2 - mBorderThickness * 2;drawRoundBorder(canvas, mRadius + mBorderThickness / 2, mBorderInsideColor);drawRoundBorder(canvas, mRadius + mBorderThickness / 2 + mBorderThickness, mBorderOutsideColor);                break;            case NoBorder:mRadius = (getWidth() < getHeight() ? getWidth() : getHeight()) / 2;                break;            default:        }        drawBitmapContent(canvas);}

private void drawRoundBorder(Canvas canvas, int radius, int color) {        Paint paint = new Paint();paint.setAntiAlias(true);paint.setFilterBitmap(true);paint.setDither(true);paint.setColor(color);paint.setStyle(Paint.Style.STROKE);paint.setStrokeWidth(mBorderThickness);canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, paint);}

private void drawBitmapContent(Canvas canvas) {        canvas.drawBitmap(bitmapToRound(bitmapToScale(bitmapToSquare(readBitmap()))), getWidth() / 2 - mRadius, getHeight() / 2 - mRadius, null);}

private Bitmap readBitmap() {        Drawable drawable = getDrawable();        if (drawable == null) {return null;}if (drawable instanceof NinePatchDrawable) {return null;}if (drawable instanceof BitmapDrawable) {return ((BitmapDrawable) drawable).getBitmap().copy(Bitmap.Config.ARGB_8888, true);}return null;}

private Bitmap bitmapToSquare(Bitmap bitmap) {int w = bitmap.getWidth();        int h = bitmap.getHeight();        int x = w > h ? (w - h) / 2 : 0;        int y = w > h ? 0 : (h - w) / 2;        int sideLength = w > h ? h : w;        return Bitmap.createBitmap(bitmap, 0, 0, sideLength, sideLength);}

private Bitmap bitmapToScale(Bitmap bitmap) {return Bitmap.createScaledBitmap(bitmap, mRadius * 2,mRadius * 2, true);}

public Bitmap bitmapToRound(Bitmap bitmap) {        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(output);Paint paint = new Paint();Rect rect = new Rect(0, 0, output.getWidth(),output.getHeight());paint.setAntiAlias(true);canvas.drawRoundRect(new RectF(rect), rect.width()/2, rect.height()/2, paint);//取output的图层与bitmap的图层的交集paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//参数2是截取原图区域,可以不是全部图片,参数3是显示位置区域canvas.drawBitmap(bitmap, rect, rect, paint);        return output;}

}

附1.设置两张图片相交时的模式

/**     * setXfermode 设置两张图片相交时的模式*///    PorterDuff.Mode.CLEAR 清除画布上图像//    PorterDuff.Mode.SRC 显示上层图像//    PorterDuff.Mode.DST 显示下层图像//    PorterDuff.Mode.SRC_OVER上下层图像都显示,上层居上显示//    PorterDuff.Mode.DST_OVER 上下层都显示,下层居上显示//    PorterDuff.Mode.SRC_IN 取两层图像交集部门,只显示上层图像//    PorterDuff.Mode.DST_IN 取两层图像交集部门,只显示下层图像//    PorterDuff.Mode.SRC_OUT 取上层图像非交集部门//    PorterDuff.Mode.DST_OUT 取下层图像非交集部门//    PorterDuff.Mode.SRC_ATOP 取下层图像非交集部门与上层图像交集部门//    PorterDuff.Mode.DST_ATOP 取上层图像非交集部门与下层图像交集部门//    PorterDuff.Mode.XOR 取两层图像的非交集部门

参考资料:

http://blog.csdn.net/alan_biao/article/details/17379925

来自为知笔记(Wiz)

时间: 2024-10-10 11:02:02

58.圆角图片的相关文章

CALayer 知识:创建带阴影效果的圆角图片图层和创建自定义绘画内容图层

效果如下: KMLayerDelegate.h 1 #import <UIKit/UIKit.h> 2 3 @interface KMLayerDelegate : NSObject 4 5 @end KMLayerDelegate.m 1 #import "KMLayerDelegate.h" 2 3 @implementation KMLayerDelegate 4 5 /** 6 * 根据角度,获取对应的弧度 7 * 8 * @param degree 角度 9 *

Android 圆形/圆角图片的方法

Android 圆形/圆角图片的方法 目前网上有很多圆角图片的实例,Github上也有一些成熟的项目.之前做项目,为了稳定高效都是选用Github上的项目直接用.但这种结束也是Android开发必备技能 ,所以今天就来简单研究一下该技术,分享给大家. 预备知识: Xfermode介绍: 下面是Android ApiDemo里的"Xfermodes"实例,效果图. Xfermode有三个子类,结构如下: view sourceprint? 1.public class 2.Xfermod

RoundedBitmapDrawable生成圆角图片

Bitmap src = BitmapFactory.decodeResource(getResources(), imageId); //获取Bitmap图片 RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), src); //创建RoundedBitmapDrawable对象 roundedBitmapDrawable.setCornerRadius

Android Xfermode 真实 实现全面、圆角图片

转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/42094215.本文出自:[张鸿洋的博客] 1.概述 事实上这篇本来准备Android BitmapShader 实战 实现圆形.圆角图片放到一篇里面.结果由于篇幅原因就独立出来了~在非常久曾经也写过一个利用Xfermode 实现圆形.圆角图片的,可是那个继承的是View.事实上继承ImageView能方便点,最起码省去了onMeasure里面自己去策略,以及不须要自己去提供设置

一起学android之设置资源图片为圆角图片 (28)

效果图: 参看以下代码: public class MainActivity extends Activity { private ImageView imageView1; private ImageView imageView2; Bitmap mBitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.

95秀-PK 动画 进度条 描边 圆角图片

PK界面 <?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Rela

Android -- 图片编辑:创建圆角图片

创建圆角图片的方式大同小异,最简单的就是 9.png 美工做出来的就是,这样的最省事直接设置即可. 第二种就是通过裁剪 这里的剪裁指的是根据原图我们自己生成一张新的bitmap,这个时候指定图片的目标区域为一个圆角局域.这种做法有一点需要生成一个新的bitmap,所以会消耗至少2倍的图片内存, 下面分析一下代码的含义: a.首先创建一个指定高宽的bitmap,作为输出的内容, b.然后创建一个相同大小的矩形,利用画布绘制时指定圆角角度,这样画布上就有了一个圆角矩形. c.最后就是设置画笔的剪裁方

UITableView性能-圆角图片

圆角图片因为GPU渲染会影响性能 参考:http://www.cocoachina.com/ios/20150803/12873.html http://blog.sina.com.cn/s/blog_671d2e4f0101cxpl.html http://www.cnblogs.com/thefeelingofsimple/archive/2013/02/20/2918547.html instruments用的不多,我直接循环了几个圆角,3种方法试了下 image.layer.corner

div css3 border-radius 之圆角 DIV圆角 图片圆角

CSS3之border-radius圆角 DIV盒子圆角 图片圆角,CSS3样式实现盒子对象圆角.图片圆角效果.div css3 border-radius圆角样式教程篇.一.css3单词与语法结构 - TOP(体感音乐床垫) 1.DIVCSS3圆角单词:border-radius 2.语法结构 div{border-radius:5px} 设置DIV对象盒子四个角5像素圆角效果 div{border-radius:5px 0;} 设置DIV对象盒子左上角和右下角5px圆角,其它两个角为0不圆角