1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.test.hwui;
18 
19 import static android.graphics.GraphicBuffer.USAGE_HW_TEXTURE;
20 import static android.graphics.GraphicBuffer.USAGE_SW_READ_NEVER;
21 import static android.graphics.GraphicBuffer.USAGE_SW_WRITE_NEVER;
22 import static android.graphics.GraphicBuffer.USAGE_SW_WRITE_RARELY;
23 
24 import android.app.Activity;
25 import android.graphics.Bitmap;
26 import android.graphics.Canvas;
27 import android.graphics.Color;
28 import android.graphics.GraphicBuffer;
29 import android.graphics.Paint;
30 import android.graphics.Picture;
31 import android.graphics.PixelFormat;
32 import android.graphics.SurfaceTexture;
33 import android.os.Bundle;
34 import android.view.DisplayListCanvas;
35 import android.view.RenderNode;
36 import android.view.Surface;
37 import android.view.ThreadedRenderer;
38 import android.widget.ImageView;
39 
40 public class DrawIntoHwBitmapActivity extends Activity {
41     @Override
onCreate(Bundle savedInstanceState)42     protected void onCreate(Bundle savedInstanceState) {
43         super.onCreate(savedInstanceState);
44         ImageView view = new ImageView(this);
45         view.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
46         setContentView(view);
47         view.setImageBitmap(createBitmap());
48     }
49 
createBitmap()50     Bitmap createBitmap() {
51         Picture picture = new Picture();
52         Canvas canvas = picture.beginRecording(500, 500);
53         Paint p = new Paint();
54         p.setColor(Color.BLACK);
55         p.setTextSize(20 * getResources().getDisplayMetrics().density);
56         canvas.drawColor(0xFF2196F3);
57         p.setColor(0xFFBBDEFB);
58         canvas.drawRect(0, 0, 500, 100, p);
59         p.setColor(Color.BLACK);
60         canvas.drawText("Hello, World!", 0, 90, p);
61         picture.endRecording();
62         return Bitmap.createBitmap(picture);
63     }
64 }
65