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 17package com.android.test.hwui; 18 19import static android.graphics.GraphicBuffer.USAGE_HW_TEXTURE; 20import static android.graphics.GraphicBuffer.USAGE_SW_READ_NEVER; 21import static android.graphics.GraphicBuffer.USAGE_SW_WRITE_NEVER; 22import static android.graphics.GraphicBuffer.USAGE_SW_WRITE_RARELY; 23 24import android.app.Activity; 25import android.graphics.Bitmap; 26import android.graphics.Canvas; 27import android.graphics.Color; 28import android.graphics.GraphicBuffer; 29import android.graphics.Paint; 30import android.graphics.PixelFormat; 31import android.graphics.SurfaceTexture; 32import android.os.Bundle; 33import android.view.DisplayListCanvas; 34import android.view.RenderNode; 35import android.view.Surface; 36import android.view.ThreadedRenderer; 37import android.widget.ImageView; 38 39public class DrawIntoHwBitmapActivity extends Activity { 40 @Override 41 protected void onCreate(Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 ImageView view = new ImageView(this); 44 view.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 45 setContentView(view); 46 view.setImageBitmap(createBitmap()); 47 } 48 49 Bitmap createBitmap() { 50 RenderNode node = RenderNode.create("HwuiCanvas", null); 51 node.setLeftTopRightBottom(0, 0, 500, 500); 52 node.setClipToBounds(false); 53 DisplayListCanvas canvas = node.start(500, 500); 54 Paint p = new Paint(); 55 p.setColor(Color.BLACK); 56 p.setTextSize(20 * getResources().getDisplayMetrics().density); 57 canvas.drawColor(0xFF2196F3); 58 p.setColor(0xFFBBDEFB); 59 canvas.drawRect(0, 0, 500, 100, p); 60 p.setColor(Color.BLACK); 61 canvas.drawText("Hello, World!", 0, 90, p); 62 node.end(canvas); 63 return ThreadedRenderer.createHardwareBitmap(node, 500, 500); 64 } 65} 66