android 开发 View _15 导入一张图片将它裁剪成圆形 与 paint图层叠加处理详解
方法一:
/*
实现思维是这样的:
1.首先拿到bitmap图片
2.得到bitmap图片的高度 宽度,并且计算好各个画图尺寸
3.创建一个空白的 bitmap图片: Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
4.将这个空白图片导入画布里
5.然后重点: 这个画布一定是要设置成透明的
6.在这张透明的画布上画圆型。
7.然后重点:设置相差裁剪属性
8.然后在这张透明的画布上添加bitmap图片
9.返回那张透明的bitmap
*/
public Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
if (width <= height) {
roundPx = width / 2;
left = 0;
top = 0;
right = width;
bottom = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
} Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output); final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
final RectF rectF = new RectF(dst); paint.setAntiAlias(true);// 设置画笔无锯齿 canvas.drawARGB(0, 0, 0, 0); // 填充整个Canvas
paint.setColor(color); // 以下有两种方法画圆,drawRounRect和drawCircle
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);// 画圆角矩形,第一个参数为图形显示区域,第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。
canvas.drawCircle(roundPx, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));// 设置两张图片相交时的模式,参考http://trylovecatch.iteye.com/blog/1189452
canvas.drawBitmap(bitmap, src, dst, paint); //以Mode.SRC_IN模式合并bitmap和已经draw了的Circle return output;
}
参考流程图 & 原理图:

方法二:
转:https://blog.csdn.net/xingxingchance/article/details/77750546
使用clipPath()方法裁剪圆形图片。
首先,我们先看效果图:
裁剪前:
裁剪后:
接下来,我们来一步一步的实现。
1.新建一个module
2.新建一个自定义view类,继承View,并重写两参构造器和onDrawn方法
/**
* Created by zhaoxin on 17/8/31.
*/ public class MyAnimationView extends View { public MyAnimationView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas); }
3.新建一个布局,在布局中通过包名.类导入自定义view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <com.example.zhaoxin.mycustomviewanimation.MyAnimationView
android:layout_width="match_parent"
android:layout_height="match_parent" /> </LinearLayout>
4.接下来就是重要的裁剪圆形图片部分
/**
* Created by zhaoxin on 17/8/31.
*/ public class MyAnimationView extends View { private Bitmap mBitmap;
private Path mPath; public MyAnimationView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas); Paint paint = new Paint(); mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
mPath = new Path(); mPath.addCircle(mBitmap.getWidth() / 2, mBitmap.getHeight() / 2, mBitmap.getWidth() / 2, Path.Direction.CCW);
canvas.clipPath(mPath);
canvas.drawBitmap(mBitmap, 0, 0, paint);
}
}
个人重写
/*
content:圆形图片的自定义ImageView
time:2018-8-7 17:43
build:zhouqiang
使用方法:请用setImage 方法设置图片
*/ public class CircleImageView extends View {
private Path mPath;
private Paint mPaint;
private Bitmap mBitmap; public CircleImageView(Context context) {
super(context);
initPaint();
} public CircleImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initPaint();
} public CircleImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint();
} public void initPaint(){
mPaint = new Paint();
mPath = new Path();
} public void setImage(Bitmap bitmap){
this.mBitmap = bitmap; } @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.reset();
mPath.addCircle(getHeight()/2,getWidth()/2,getWidth()/2.5f,Path.Direction.CW);
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
canvas.clipPath(mPath);
canvas.drawBitmap(mBitmap,
new Rect(0,0,mBitmap.getWidth(),mBitmap.getHeight()),
new Rect(0,0,getWidth(),getHeight()),
mPaint); }
}
android 开发 View _15 导入一张图片将它裁剪成圆形 与 paint图层叠加处理详解的更多相关文章
- android 开发 View _1_ View的子类们 和 视图坐标系图
目录: android 开发 View _2_ View的属性动画ObjectAnimator ,动画效果一览 android 开发 View _3_ View的属性动画ValueAnimator a ...
- Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing ...
- Qt开发技术:QCharts(三)QCharts样条曲线图介绍、Demo以及代码详解
若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...
- Android开发——View滑动的三种实现方式
0. 前言 Android开发中,我们常常需要View滑动实现一些绚丽的效果来优化用户体验.一般View的滑动可以用三种方式实现. 转载请注明出处:http://blog.csdn.net/seu ...
- android 开发 View _10_ Path之基本操作
转载地址:http://www.gcssloop.com/customview/Path_Basic/ 安卓自定义View进阶-Path之基本操作 在上一篇Canvas之图片文字中我们了解了如何使用C ...
- Android开发——View滑动冲突解决方案
0. 前言 我们在Android开发--事件分发机制详解中深入学习了事件分发机制,为我们解决Android开发中的滑动冲突问题做了初步准备.针对滑动冲突这里给出两种解决方案:外部拦截法和内部拦截法 ...
- android 开发 View _6_Canvas详解
牛逼大神的博客地址:http://www.gcssloop.com/customview/Canvas_BasicGraphics 安卓自定义View进阶-Canvas之绘制图形 在上一篇自定义Vie ...
- 【Android UI设计与开发】第01期:引导界面(一)ViewPager介绍和使用详解
做Android开发加起来差不多也有一年多的时间了,总是想写点自己在开发中的心得体会与大家一起交流分享.共同进步,刚开始写也不知该如何下手,仔细想了一下,既然是刚开始写,那就从一个软件给人最直观的感受 ...
- Qt开发技术:图形视图框架(二)场景QGraphicsScene、QGraphicsItem与QGraphicsView详解
前话 Qt的图形视图框架,最核心的三个类为:QGraphicsScene.QGraphicsItem与QGraphicsView. 基于图形框架的高级白板软件Demo QGraphicsSce ...
随机推荐
- Django--QuerySet--基础查询
Django--QuerySet--基础查询 创建模型类: from django.db import models # Create your models here. class Blog(mod ...
- Python装饰器基础及运行时间
一.装饰器基础 装饰器是可调用的对象,其参数是另一个函数(被装饰的函数).装饰器可能会处理被装饰的函数,然后把他返回,或者将其替换成另一个函数或可调用对象. eg:decorate装饰器 @decor ...
- VSTO:使用C#开发Excel、Word【15】
使用文档属性DocumentProperties集合和DocumentProperty对象位于Microsoft Office 11.0 Object Library(office.dll)中,该对象 ...
- Vuejs的$watch实现原理
大概原理如下面代码所示: class Vue { //Vue对象 constructor (options) { this.$options=options; let data = this._dat ...
- re模块+面向对象
re模块 re:其实就是带有特殊语法的字符串 语法:单个字符和多个字符 单个字符: \d是匹配所有的数字 \D是匹配所有的非数字 \s是所有的换行符,制表符,空白等,回车符 \S是所有费换行符,空白和 ...
- android active间数据传递
Bundle是key-value存储. Bundle bundle=new Bundle(); bundle.putString("key", "value") ...
- Github Page 搜索工具
轮子 今天造了一个轮子 -- Github Page搜索工具 https://man-ing.com/github. 什么是Github Page 直接从GitHub存储库托管.只需编辑,推送,更改即 ...
- 2java判断素数
package com.test; import java.math.*;import java.util.Scanner; public class test222 { /** * @param a ...
- jwt 接口加密
什么是JWT Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的,特别适用于分布式站点 ...
- Eclipse 护眼背景色设置
链接地址:http://blog.chinaunix.net/uid-27183448-id-3509010.html 背景颜色推荐:色调:85,饱和度:123,亮度:205 文档都不再是刺眼的白底 ...