Canvas 源码

本文探讨了Android开发中的自定义控件绘制,重点解析了Canvas的源码。Canvas作为绘制的基础,涉及bitmap、canvas、绘图材料和画笔四个关键组件。文章介绍了Canvas的构造方法,包括无参构造和基于bitmap的构造,以及如何设置和获取canvas的密度和viewport。此外,还讨论了剪辑边界、保存和恢复状态的功能,为Android图形绘制提供了深入的理解。
摘要由CSDN通过智能技术生成

最近在看自定义控件---绘制view

今天先上点Canvas的源码,持续更新中。。。

下面是一点Canvas源码

/**
 * The Canvas class holds the "draw" calls. To draw something, you need
 * 4 basic components: A Bitmap to hold the pixels, a Canvas to host
 * the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect,
 * Path, text, Bitmap), and a paint (to describe the colors and styles for the
 * drawing).
 *
 * <div class="special reference">
 * <h3>Developer Guides</h3>
 * <p>For more information about how to use Canvas, read the
 * <a href="{@docRoot}guide/topics/graphics/2d-graphics.html">
 * Canvas and Drawables</a> developer guide.</p></div>
 */
public class Canvas {
    /** @hide */
    public static boolean sCompatibilityRestore = false;

    /**
     * Should only be assigned in constructors (or setBitmap if software canvas),
     * freed in finalizer.
     * @hide
     */
    protected long mNativeCanvasWrapper;
 /**
     * Construct an empty raster canvas. Use setBitmap() to specify a bitmap to
     * draw into.  The initial target density is {@link Bitmap#DENSITY_NONE};
     * this will typically be replaced when a target bitmap is set for the
     * canvas.
     */
    public Canvas() {
        if (!isHardwareAccelerated()) {
            // 0 means no native bitmap
            mNativeCanvasWrapper = initRaster(null);
            mFinalizer = new CanvasFinalizer(mNativeCanvasWrapper);
        } else {
            mFinalizer = null;
        }
    }



Canvas 类保存了draw 的调用,你需要四个基本的组件来绘制。1.bitmap保存像素的位图。2.canvas保存draw的调用。3一个原始绘画材料,4.一个画笔(color,style)

有两个构造方法,一个无参

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值