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 android.uirendering.cts.testclasses.view;
18 
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.Canvas;
22 import android.graphics.Path;
23 import android.util.AttributeSet;
24 import android.view.View;
25 import android.widget.FrameLayout;
26 
27 public class BitmapView extends View {
28     private Bitmap mBitmap;
29     private boolean mSaveLayer;
30 
BitmapView(Context context, AttributeSet attrs)31     public BitmapView(Context context, AttributeSet attrs) {
32         super(context, attrs);
33     }
34 
BitmapView(Context context, AttributeSet attrs, int defStyleAttr)35     public BitmapView(Context context, AttributeSet attrs, int defStyleAttr) {
36         super(context, attrs, defStyleAttr);
37     }
38 
BitmapView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)39     public BitmapView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
40         super(context, attrs, defStyleAttr, defStyleRes);
41     }
42 
BitmapView(Context context, Bitmap b, boolean saveLayer)43     public BitmapView(Context context, Bitmap b, boolean saveLayer) {
44         super(context);
45         mBitmap = b;
46         mSaveLayer = saveLayer;
47     }
48 
getBitmap()49     public Bitmap getBitmap() {
50         return mBitmap;
51     }
52 
setBitmap(Bitmap bitmap)53     public void setBitmap(Bitmap bitmap) {
54         mBitmap = bitmap;
55     }
56 
isSaveLayer()57     public boolean isSaveLayer() {
58         return mSaveLayer;
59     }
60 
setSaveLayer(boolean saveLayer)61     public void setSaveLayer(boolean saveLayer) {
62         mSaveLayer = saveLayer;
63     }
64 
65     @Override
onDraw(Canvas canvas)66     protected void onDraw(Canvas canvas) {
67         super.onDraw(canvas);
68         if (mSaveLayer) {
69             canvas.saveLayer(0.0f, 0.0f, getWidth(), getHeight(), null);
70         }
71         canvas.drawBitmap(mBitmap, 0, 0, null);
72         if (mSaveLayer) {
73             canvas.restore();
74         }
75     }
76 
77     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)78     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
79         setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
80     }
81 }
82