John Reck | 1fedd91 | 2017-05-23 14:45:22 -0700 | [diff] [blame] | 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 | |
John Reck | 1fedd91 | 2017-05-23 14:45:22 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.graphics.Bitmap; |
| 21 | import android.graphics.Canvas; |
| 22 | import android.graphics.Color; |
John Reck | 1fedd91 | 2017-05-23 14:45:22 -0700 | [diff] [blame] | 23 | import android.graphics.Paint; |
John Reck | 519ad48 | 2018-02-12 17:08:48 -0800 | [diff] [blame] | 24 | import android.graphics.Picture; |
John Reck | 1fedd91 | 2017-05-23 14:45:22 -0700 | [diff] [blame] | 25 | import android.os.Bundle; |
John Reck | 1fedd91 | 2017-05-23 14:45:22 -0700 | [diff] [blame] | 26 | import android.widget.ImageView; |
| 27 | |
| 28 | public class DrawIntoHwBitmapActivity extends Activity { |
| 29 | @Override |
| 30 | protected void onCreate(Bundle savedInstanceState) { |
| 31 | super.onCreate(savedInstanceState); |
| 32 | ImageView view = new ImageView(this); |
| 33 | view.setScaleType(ImageView.ScaleType.CENTER_INSIDE); |
| 34 | setContentView(view); |
| 35 | view.setImageBitmap(createBitmap()); |
| 36 | } |
| 37 | |
| 38 | Bitmap createBitmap() { |
John Reck | 519ad48 | 2018-02-12 17:08:48 -0800 | [diff] [blame] | 39 | Picture picture = new Picture(); |
| 40 | Canvas canvas = picture.beginRecording(500, 500); |
John Reck | 1fedd91 | 2017-05-23 14:45:22 -0700 | [diff] [blame] | 41 | Paint p = new Paint(); |
| 42 | p.setColor(Color.BLACK); |
| 43 | p.setTextSize(20 * getResources().getDisplayMetrics().density); |
| 44 | canvas.drawColor(0xFF2196F3); |
| 45 | p.setColor(0xFFBBDEFB); |
| 46 | canvas.drawRect(0, 0, 500, 100, p); |
| 47 | p.setColor(Color.BLACK); |
| 48 | canvas.drawText("Hello, World!", 0, 90, p); |
John Reck | 519ad48 | 2018-02-12 17:08:48 -0800 | [diff] [blame] | 49 | picture.endRecording(); |
| 50 | return Bitmap.createBitmap(picture); |
John Reck | 1fedd91 | 2017-05-23 14:45:22 -0700 | [diff] [blame] | 51 | } |
| 52 | } |