Simple Use of PictureDrawable

Keywords: Android xml encoding Java

This is the fifth blog about Drawable, Picture Drawable, I feel that if you are not doing Internet development or more sensitive to data traffic, you should generally not use it (I'm nonsense, anyway, I don't do Internet development, I would not use this kind of blog myself if it wasn't for writing this blog).
Don't talk too much nonsense, just get to the point.
Let's take a look at the approximate effect first (actually, what effect is there?)

Main layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.picturedrawable.MainActivity">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="400dp"
        android:minWidth="400dp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/iv"
        android:layout_marginTop="20dp"
        android:onClick="setImageViewBg"
        android:text="@string/set_image" />
</RelativeLayout>

Main Java file:

package com.example.picturedrawable;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.drawable.PictureDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView iv;
    private PictureDrawable pictureDrawable;
    private int width;
    private int height;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView)findViewById(R.id.iv);
    }
    private void initPictureDrawable(){

        Picture picture = new Picture();
        width = iv.getMinimumWidth();
        height = iv.getMinimumHeight();
        Canvas c = picture.beginRecording(width,height);
        Paint p = new Paint();
        p.setColor(Color.GREEN);
        c.drawCircle(width/2,height/2,200,p);
        picture.endRecording();
        pictureDrawable = new PictureDrawable(picture);

    }

    @Override
    protected void onResume() {
        super.onResume();
        initPictureDrawable();
    }

    public void setImageViewBg(View view){
        Bitmap bitmap = Bitmap.createBitmap(width,height, Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.first),0,0,null);
        pictureDrawable.setBounds(0,0,width,height);
        pictureDrawable.draw(canvas);//Direct calls do not draw anything to canvas, so bounds must be set up first
//        canvas.drawPicture(pictureDrawable.getPicture());
        iv.setImageBitmap(bitmap);
    }

}

There's so much code. When using PictureDrawable, Picture is actually used more. There are several important ways to use Picture. For example, createFromStream, writeToStream, beginRecording and endRecording. Picture only records the information of drawing steps, and does not have pixel data. Setting PictureDrawable as the background directly will not display. It needs to call draw method.

Drawing PictureDrawable is actually drawing Picture inside. If you call PictureDrawable.draw directly, you can't draw a graph, because there is no drawing area, you need to specify bounds first. It is not necessary to call Canvas.drawPicture(PictureDrawable.getPicture()). Look at the source code and clear it!

createFromStream and writeToStream read and write a Picture to the stream respectively. If they are transmitted remotely, they do not need to transmit a large number of pixel information. They only need to transfer the drawing process records. They can be redrawn at the other end. It feels that they will save more traffic! ________ (Guess by yourself)

This is my public name. If you can, please pay attention to it. It will be my greatest encouragement. Thank you!

Code address:
Simple Use of PictureDrawable - A Small demo

Posted by cvjacques on Mon, 15 Apr 2019 12:54:32 -0700