1.平移

 //构造两个画笔,一个红色,一个绿色
Paint paint_green = generatePaint(Color.GREEN, Paint.Style.STROKE, 3);
Paint paint_red = generatePaint(Color.RED, Paint.Style.STROKE, 3); //构造一个矩形
Rect rect1 = new Rect(0,0,400,220); //在平移画布前用绿色画下边框
canvas.drawRect(rect1, paint_green); //平移画布后,再用红色边框重新画下这个矩形
canvas.translate(100, 100);
canvas.drawRect(rect1, paint_red);

2.旋转

 Paint paint_green = generatePaint(Color.GREEN, Paint.Style.FILL, 5);
Paint paint_red = generatePaint(Color.RED, Paint.Style.STROKE, 5); Rect rect1 = new Rect(300,10,500,100);
canvas.drawRect(rect1, paint_red); //画出原轮廓 canvas.rotate(30);//顺时针旋转画布
canvas.drawRect(rect1, paint_green);//画出旋转后的矩形

3.缩放

     Paint paint_green = generatePaint(Color.GREEN, Paint.Style.STROKE, 5);
Paint paint_red = generatePaint(Color.RED, Paint.Style.STROKE, 5);
Paint paint_yellow = generatePaint(Color.YELLOW, Paint.Style.STROKE, 5); Rect rect1 = new Rect(10,10,200,100);
canvas.drawRect(rect1, paint_green); canvas.scale(0.5f, 1);
canvas.drawRect(rect1, paint_red);

4.扭曲

 Paint paint_green = generatePaint(Color.GREEN, Paint.Style.STROKE, 5);
Paint paint_red = generatePaint(Color.RED, Paint.Style.STROKE, 5); Rect rect1 = new Rect(10,10,200,100); canvas.drawRect(rect1, paint_green);
canvas.skew(1.732f,0);//X轴倾斜60度,Y轴不变
canvas.drawRect(rect1, paint_red);

5.裁剪

 canvas.drawColor(Color.RED);
//保存的画布大小为全屏幕大小
int c1 = canvas.save(); canvas.clipRect(new Rect(100, 100, 800, 800));
canvas.drawColor(Color.GREEN);
//保存画布大小为Rect(100, 100, 800, 800)
int c2 = canvas.save(); canvas.clipRect(new Rect(200, 200, 700, 700));
canvas.drawColor(Color.BLUE);
//保存画布大小为Rect(200, 200, 700, 700)
int c3 = canvas.save(); canvas.clipRect(new Rect(300, 300, 600, 600));
canvas.drawColor(Color.BLACK);
//保存画布大小为Rect(300, 300, 600, 600)
int c4 = canvas.save(); canvas.clipRect(new Rect(400, 400, 500, 500));
canvas.drawColor(Color.WHITE); //连续出栈三次,将最后一次出栈的Canvas状态作为当前画布,并画成黄色背景
canvas.restoreToCount(c2);
canvas.drawColor(Color.YELLOW);

获取画笔方法

  private Paint generatePaint(int color,Paint.Style style,int width){
Paint paint = new Paint();
paint.setColor(color);
paint.setStyle(style);
paint.setStrokeWidth(width);
return paint;
}

画布的保存和恢复

canvas.save()
canvas.restore();

save()每次调用save函数,都会先保存画布的状态,然后将其放入特定的栈中

restore()都会把栈中顶层的画布状态取出来,并按照这个状态恢复当前的画布,然后在这个画布上作画

restoreToCount函数
restoreToCount(int saveCount)
        int c2 = canvas.save();//返回栈的索引
canvas.restoreToCount(c2);//根据索引返回当前画布

示例代码-圆形代码

public class BasisView extends View {

    private Bitmap mBmp;
private Paint mPaint;
private Path mPath;
public BasisView(Context context) {
super(context);
init();
} public BasisView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
} public BasisView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
} private void init(){
setLayerType(LAYER_TYPE_SOFTWARE,null);
mBmp = BitmapFactory.decodeResource(getResources(), R.drawable.avator);
mPaint = new Paint();
mPath = new Path();
int width = mBmp.getWidth();
int height = mBmp.getHeight();
mPath.addCircle(width/2,height/2,width/2, Path.Direction.CCW);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.clipPath(mPath);
canvas.drawBitmap(mBmp,0,0,mPaint);
canvas.restore(); } }

裁剪动画

package com.loaderman.customviewdemo.paint;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View; import com.loaderman.customviewdemo.R; public class BasisView extends View {
private Path mPath;
private Bitmap mBitmap;
private int clipWidth = 0;
private int width;
private int heigth;
private static final int CLIP_HEIGHT = 30;
private Region mRgn;
public BasisView(Context context) {
super(context);
init();
}
public BasisView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public BasisView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setLayerType(LAYER_TYPE_SOFTWARE, null);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.avator);
width = mBitmap.getWidth();
heigth = mBitmap.getHeight();
mRgn = new Region();
mPath=new Path();
}
@Override
protected void onDraw(Canvas canvas) {
mRgn.setEmpty();
int i = 0;
while (i * CLIP_HEIGHT <= heigth) {
if (i % 2 == 0) {
mPath.addRect(new RectF(0, i * CLIP_HEIGHT, clipWidth, (i + 1) * CLIP_HEIGHT),Path.Direction.CCW);
} else {
mPath.addRect(new RectF(width - clipWidth, i * CLIP_HEIGHT, width, (i + 1) * CLIP_HEIGHT),Path.Direction.CCW);
}
i++;
}
canvas.clipPath(mPath);
canvas.drawBitmap(mBitmap, 0, 0, new Paint());
if (clipWidth > width) {
return;
}
clipWidth += 5;
invalidate();
} }

效果:

自定义控件之canvas变换和裁剪的更多相关文章

  1. 【转】android Graphics(四):canvas变换与操作

    android Graphics(四):canvas变换与操作 分类: 5.andriod开发2014-09-05 15:05 5877人阅读 评论(18) 收藏 举报   目录(?)[+]   前言 ...

  2. android Graphics(四):canvas变换与操作

    前言:前几篇讲解了有关canvas绘图的一些操作,今天更深入一些,讲讲对画布的操作,这篇文章不像前几篇那么容易理解,如果以前没有接触过画布的童鞋可能比较难以理解,为什么会这样.我尽量多画图,让大家更清 ...

  3. canvas合成和裁剪

    canvas合成和裁剪 属性 globalCompositeOperation=type 设置覆盖类型 source-over 源覆盖在目标上 source-in 源覆盖在目标上的公共部分(只取源图形 ...

  4. canvas变换

    canvas变换 方法 save() 保存canvas状态 restore() 回复canvas保存的状态 translate(x, y) 移动canvas位置 rotate(radians) 顺时针 ...

  5. 自定义控件之绘图篇(四):canvas变换与操作

    具体操作见下面链接: http://blog.csdn.net/harvic880925/article/details/39080931/

  6. 自定义控件之Canvas图形绘制基础练习-青春痘笑脸^_^

    对于自定义控件的意义不言而喻,所以对它的深入研究是很有必要的,前些年写过几篇关于UI效果的学习过程,但是中途比较懒一直就停滞了,而对于实际工作还是面试来说系统深入的了解自定义控件那是很有必要的,所以接 ...

  7. 使用canvas进行图片裁剪简单功能

    1.html部分 使用一个input[type="file"]进行图片上传: canvas进行图片的裁剪展示 <div> <input type="fi ...

  8. html5 canvas 自定义画图裁剪图片

    html5 给我们带来了极大惊喜的canvas标签,有了它我们可以在浏览器客户端处理图片,不需要经过服务器周转.可以实现: 1.照片本地处理,ps有的一些基本功能都有 2.结合js可以实现一些很炫的动 ...

  9. canvas变换(移动,缩放等)

    代码: 1 /** 2 * Created by Administrator on 2016/1/30. 3 */ 4 function draw (id){ 5 var canvas = docum ...

随机推荐

  1. 用Jmeter做性能测试,之后报表展示

    https://octoperf.com/blog/2017/10/19/how-to-analyze-jmeter-results/ 看到性能测试平台的开发,我在想需要什么功能,报表需要什么样子的 ...

  2. LearnOpenGL学习笔记(一)画个三角形

    开始学习OpenGL,参考的是著名的LearnOpenGL这个网站,在这里做一些总结性的记录,只是方便自己日后查找或者记录自己的一些拓展思考,关于OpenGL的具体内容请移步: https://lea ...

  3. P2661 信息传递[最小环+边带权并查集]

    题目来源:洛谷 题目描述 有 n 个同学(编号为 1 到 n )正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 i 的同学的信息传递对象是编号为 Ti​ 的同学. 游戏 ...

  4. jQuery toast 淡入淡出提示

    #toast{ position: fixed; top: 44%;left:50%;transform: translateX(-50%); min-width: 80px; max-width: ...

  5. flask 框架 转载:https://cloud.tencent.com/developer/article/1465968

    特点总结: 类名称---->数据库表名 类属性---->数据库字段 类的对象----->数据库表中的一行一行数据 3.ORM操作注意(理解) 1/因为SQLALChemy去app身上 ...

  6. JavaScript常用的方法

    indexOf() 功能:indexOf() 方法返回调用 String 对象中第一次出现的指定值的索引. 语法:indexOf(searchValue, fromIndex) searchValue ...

  7. 2019-2020-1 20199312《Linux内核原理与分析》第二周作业

    c语言代码 // main.c int g(int x) { return x + 4; } int f(int x) { return g(x); } int main(void) { return ...

  8. redis--基于内存的高速缓存,NoSql的典型代表

    NoSql入门和概述 入门概述 为什么要使用NoSql? 1.单机mysql的美好年代 在早些年以前,那时候网站的访问量不大,用单个数据库完全可以应付.而且那个时候,绝大部分都是LAMP架构:Linu ...

  9. Codeforces#572 Div2 C---Candies!【倍增】【DP】【思维】

    题目:http://codeforces.com/contest/1189/problem/C 题意:给定n个数,每次查询一个区间$[l,r]$.对这个区间内的数,相邻两个数之和超过10,则得到一个c ...

  10. Flask+nginx+Gunicorn部署

    当我们开发完Flask项目后是不能直接通过命令启动服务来使用的(扛不住的) Gunicorn 是一个给 UNIX 用的 WSGI HTTP 服务器.这是一个从 Ruby 的 Unicorn 项目移植的 ...