一:图片旋转
Matrix matrix = new Matrix();
matrix.postRotate(0); //0是不旋转的
Bitmap newBit = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
iv_test.setImageBitmap(newBit);
二:绘制4个角度一样的圆角图片(加边框)
private void circle(Bitmap bitmap){
int angle=50; //角度
int strokeWidth=5; //边框宽度
//从原bitmap中创建一个null的bitmap
Bitmap temp=Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(temp); //放到画布中
//使用BitmapShader方式来进行绘制
Paint paint=new Paint();
paint.setShader(new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
RectF rectF = new RectF(0f, 0f, bitmap.getWidth(), bitmap.getHeight());
//绘制出带圆角的位图
canvas.drawRoundRect(rectF, angle, angle, paint);
//创建绘制边框的画笔
Paint paint1=new Paint();
paint1.setStyle(Paint.Style.STROKE);
paint1.setStrokeWidth(strokeWidth);
//这里要特别注意绘制边框矩形的参数,如果我们以(0,0,bitmap.getWidth(),bitmap.getHeight())来绘制边框
//绘制完后会发现四个角的边框部分会突出一些,即边框看起来粗一些
//这个原因是因为我们边框是5px宽度,当我们以0,0绘制时边框其实中心坐标应该是2/5,2/5处
//所以会多出2/5的宽度,这个时候我们调整如下就ok了
RectF rectF2 = new RectF(strokeWidth/2, strokeWidth/2, bitmap.getWidth()-strokeWidth/2, bitmap.getHeight()-strokeWidth/2);
paint1.setColor(ContextCompat.getColor(this,R.color.base_color));
canvas.drawRoundRect(rectF2, angle, angle, paint1);
iv_test.setImageBitmap(temp);
}
ps::如上绘制代码,如果我们只想给某个角给角度,其他角不给角度怎么办呢?
//如上方法当我们先把4个角度全部绘制出来,以及边框绘制好之后
//这里我们如果不想要左上角的这个圆角,我们绘制一个圆角角度大小的矩形即可,因为我们采用的是
//BitmapShader绘制,绘制出的矩形是按照图片的纹理进行还还原的不会出问题
canvas.drawRect(0,0,angle,angle,paint);
//同理我们也要把左上角的边框线还原
canvas.drawRect(0,0,angle,angle,paint1);
三:绘制不规则的角度及边框线
1.Canvas.save():用来保存Canvas的状态,save()方法之后的代码,可以调用Canvas的平移、放缩、旋转、裁剪等操作!
2.Canvas,restore():用来恢复Canvas之前保存的状态,防止save()方法代码之后对Canvas执行的操作,继续对后续的绘制会产生影响,通过该方法可以避免连带的影响!
private void circle2(Bitmap bitmap){
//边框线paint
Paint paint1=new Paint();
paint1.setStyle(Paint.Style.STROKE);
paint1.setStrokeWidth(5);
paint1.setAntiAlias(true);
//绘制4个角都是不同的角度,a、b、c、d、代表4个角的角度头不一样
int a=30,b=40,c=50,d=60;
//从位图创建空图形
Bitmap temp=Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(temp);
Paint paint=new Paint();
//使用BitmapShader模式绘制
paint.setShader(new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
canvas.save();
RectF rectF = new RectF(5/2, 5/2, bitmap.getWidth()-5/2, bitmap.getHeight()-5/2);
//剪切画布的左上1/4
canvas.clipRect(0, 0, canvas.getWidth()/2 , canvas.getHeight()/2 );
//绘制a圆角其实这里是把剪切出来的1/4画布都绘制了圆角,
//这里因为我们rectF给出的宽高其实是超出了画布剪切后的的宽高
//他右上,左下,右下都不会显示出来,只剩下右上a的圆角
canvas.drawRoundRect(rectF, a, a, paint);
//绘制边框线
canvas.drawRoundRect(rectF, a, a, paint1);
canvas.restore();
//下面依次把其他3/4全部绘制出来最后合成一个4个不同圆角的图
canvas.save();
canvas.clipRect(0, canvas.getHeight() / 2, canvas.getWidth() / 2, canvas.getHeight());
canvas.drawRoundRect(rectF, b, b, paint);
canvas.drawRoundRect(rectF, b, b, paint1);
canvas.restore();
canvas.save();
canvas.clipRect(canvas.getWidth() / 2, 0, canvas.getWidth(), canvas.getHeight()/2);
canvas.drawRoundRect(rectF, c, c, paint);
canvas.drawRoundRect(rectF, c, c, paint1);
canvas.restore();
canvas.save();
canvas.clipRect(canvas.getWidth() / 2, canvas.getHeight()/2, canvas.getWidth(), canvas.getHeight());
canvas.drawRoundRect(rectF, d, d, paint);
canvas.drawRoundRect(rectF, d, d, paint1);
canvas.restore();
iv_test.setImageBitmap(temp);
}
PS:如上代码就是利用画布剪切,然后没剪切一部分绘制一个圆角,最后4部分全部绘制出来就出现不同的4个圆角,这里必须使用canvas.save();使用之后才能对画布进行裁剪等操作,同时我们使用 canvas.restore();后画布还原成初始状态这样才不会影响后面的裁剪,后面的裁剪都是基于最开始的完整位图中进行裁剪,如果不设置那第一个1/4裁剪后,后面就没法办法继续裁剪了
四:圆角绘制的其他方式
//这种绘制方式采用了图形混合模式Xfermode,即我们 绘制2个不同的图形,然后使用Xfermode图形混合自动裁剪掉不要的部分来实现圆角绘制
private Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
Bitmap roundBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(roundBitmap);
int color = 0xff424242;
Paint paint = new Paint();
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
RectF rectF = new RectF(rect);
float roundPx = 50;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
//先绘制一个空的圆角矩形
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
//使用setXfermode图形模式
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
//绘制矩形位图,二者重合再一起会把不要的地方去掉就出现一个圆角
canvas.drawBitmap(bitmap, rect, rect, paint);
return roundBitmap;
}