1 package com.android.test.hwui;
2 
3 import android.app.Activity;
4 import android.graphics.Bitmap;
5 import android.graphics.Bitmap.Config;
6 import android.graphics.Paint.Style;
7 import android.graphics.Canvas;
8 import android.graphics.Color;
9 import android.graphics.ColorFilter;
10 import android.graphics.Paint;
11 import android.graphics.PixelFormat;
12 import android.graphics.Rect;
13 import android.graphics.drawable.Drawable;
14 import android.os.Bundle;
15 import android.os.Handler;
16 import android.util.Log;
17 import android.view.PixelCopy;
18 import android.view.View;
19 import android.widget.Button;
20 import android.widget.ImageView;
21 import android.widget.LinearLayout;
22 import android.widget.TextView;
23 
24 public class PixelCopyWindow extends Activity {
25 
26     private Handler mHandler;
27     private ImageView mImage;
28     private TextView mText;
29 
30     @Override
onCreate(Bundle savedInstanceState)31     protected void onCreate(Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33         mHandler = new Handler();
34 
35         LinearLayout layout = new LinearLayout(this);
36         TextView text = new TextView(this);
37         text.setText("Hello, World!");
38         Button btn = new Button(this);
39         btn.setText("Screenshot!");
40         btn.setOnClickListener((v) -> takeScreenshot());
41         mImage = new ImageView(this);
42         mText = new TextView(this);
43 
44         layout.setOrientation(LinearLayout.VERTICAL);
45         layout.addView(text);
46         layout.addView(btn);
47         layout.addView(mImage);
48         layout.addView(mText);
49         final float density = getResources().getDisplayMetrics().density;
50         layout.setBackground(new Drawable() {
51             Paint mPaint = new Paint();
52 
53             @Override
54             public void draw(Canvas canvas) {
55                 mPaint.setStyle(Style.STROKE);
56                 mPaint.setStrokeWidth(4 * density);
57                 mPaint.setColor(Color.BLUE);
58                 final Rect bounds = getBounds();
59                 canvas.drawRect(bounds, mPaint);
60                 mPaint.setColor(Color.RED);
61                 canvas.drawLine(bounds.centerX(), 0, bounds.centerX(), bounds.height(), mPaint);
62                 mPaint.setColor(Color.GREEN);
63                 canvas.drawLine(0, bounds.centerY(), bounds.width(), bounds.centerY(), mPaint);
64             }
65 
66             @Override
67             public void setAlpha(int alpha) {
68             }
69 
70             @Override
71             public void setColorFilter(ColorFilter colorFilter) {
72             }
73 
74             @Override
75             public int getOpacity() {
76                 return PixelFormat.TRANSLUCENT;
77             }
78         });
79         setContentView(layout);
80     }
81 
takeScreenshot()82     private void takeScreenshot() {
83         View decor = getWindow().getDecorView();
84         Rect srcRect = new Rect();
85         decor.getGlobalVisibleRect(srcRect);
86         final Bitmap bitmap = Bitmap.createBitmap(
87                 (int) (srcRect.width() * .25), (int) (srcRect.height() * .25), Config.ARGB_8888);
88         PixelCopy.request(getWindow(), srcRect, bitmap, (result) -> {
89             if (result != PixelCopy.SUCCESS) {
90                 mText.setText("Copy failed, result: " + result);
91                 mImage.setImageBitmap(null);
92             } else {
93                 mText.setText("");
94                 mImage.setImageBitmap(bitmap);
95             }
96         }, mHandler);
97     }
98 }
99