Android——new Canvas(Bitmap)中对canvas和bitmap的理解

最近刚接触自定义View的时候,看到Canvas有一个构造函数需要传入一个Bitmap,不解。于是有了这篇文章!

我们先看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).

译:想要画一些东西,你需要4个基本的组件:一个Bitmap来保存像素,一个Canvas用于承载绘制调用(比如drawLine等方法),一个绘制源(比如:Rect,Path,text,Bitmap),一个Paint(即画笔,用于描述绘制的颜色和样式)。

这句话说明Bitmap是用来保存像素的,即保存我们画的东西。

先看看它的构造函数:

①public Canvas ()

Construct an empty raster canvas. Use setBitmap() to specify a bitmap to draw into. The initial target density is DENSITY_NONE; this will typically be replaced when a target bitmap is set for the canvas.

译:这里说是构造一个空的画布,之后可以使用setBitmap来指定一个bitmap用来画东西(说明我们画的操作都是画到指定的bitmap上去的,这里很重要)。初始密度是DENSITY_NONE,这个通常会在为画布设置bitmap时被替换。

②public Canvas (Bitmap bitmap)

Construct a canvas with the specified bitmap to draw into. The bitmap must be mutable.The initial target density of the canvas is the same as the given bitmap's density.

译:构造一个带有给定bitmap的画布,画东西到bitmap里面去,这个bitmap必须是可变的。画布的初始密度和给定的bitmap的密度一致。

Bitmap是什么?

简单来说,Bitmap是位图文件,它将图像定义为由点(像素)组成,每个点可以由多种色彩表示。

Canvas和Bitmap什么关系?

从上面可以看出,我们画的东西都是存放在指定的Bitmap上的。canvas只是起到一个中介的作用,它承载了画的方法(比如drawLine,drawCircle等)。这样就好理解了。

下面用一个Demo来更清晰地认识它:

xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@android:color/holo_red_light"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/iv_tes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

很简单,红色背景,里面有一个ImageView,等下用来显示图片。

MainActivity.class

    private ImageView mImageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = findViewById(R.id.iv_tes);

        //android不允许直接修改res里面的图片,所以要用copy方法
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher).copy(Bitmap.Config.ARGB_8888,true);
        Canvas mCanvas = new Canvas(bitmap);
        mCanvas.drawARGB(255,0,255,0);

        mImageView.setImageBitmap(bitmap);
    }

这里从资源文件中获取bitmap,因为android不允许直接修改res里面的文件,所以这里获取后要用copy方法复制一个,这样就不会修改res里面的文件,图片如下:

获取到bitmap后,用该bitmap作为参数构造一个Canvas,然后调用canvas的drawARGB方法,给整张图片涂上绿色(一片草原),然后显示该bitmap到ImageView。

结果如下:

可以看到,我们的android机器人图片确实被涂成了绿色。并且canvas的大小和图片的大小是一直的。

那么有人会问:不要bitmap行不行?

可以,没有bitmap,单单一个Canvas一样可以调用它的drawARGB等方法,不会报错,但是,你无法保存,既然无法保存,那画来做什么?

这里可以得出结论:

Canvas构造函数需要传入一个Bitmap,该bitmap是我们对canvas进行操作的载体,比如调用canvas的drawLine方法画一条线,将会把线画到bitmap里去。Canvas直接对该Bitmap对象进行修改,Bitmap保存我们的操作。 

 

喜欢点个赞!是对我最大的支持!

  • 22
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值