1 /*
2  * Copyright (C) 2010 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 android.app.Activity;
20 import android.content.res.Resources;
21 import android.graphics.Bitmap;
22 import android.graphics.Canvas;
23 import android.graphics.Rect;
24 import android.graphics.RectF;
25 import android.graphics.drawable.Drawable;
26 import android.os.Bundle;
27 import android.view.View;
28 import android.widget.FrameLayout;
29 
30 @SuppressWarnings({"UnusedDeclaration"})
31 public class ThinPatchesActivity extends Activity {
32     @Override
onCreate(Bundle savedInstanceState)33     protected void onCreate(Bundle savedInstanceState) {
34         super.onCreate(savedInstanceState);
35 
36         FrameLayout layout = new FrameLayout(this);
37         PatchView b = new PatchView(this);
38         b.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
39                 FrameLayout.LayoutParams.MATCH_PARENT));
40         layout.addView(b);
41         layout.setBackgroundColor(0xffffffff);
42 
43         setContentView(layout);
44     }
45 
46     private class PatchView extends View {
47         private Drawable mPatch1, mPatch2, mPatch3;
48         private Bitmap mTexture;
49 
PatchView(Activity activity)50         public PatchView(Activity activity) {
51             super(activity);
52 
53             final Resources resources = activity.getResources();
54             mPatch1 = resources.getDrawable(R.drawable.patch);
55             mPatch2 = resources.getDrawable(R.drawable.btn_toggle_on);
56             mPatch3 = resources.getDrawable(R.drawable.patch2);
57 
58             mTexture = Bitmap.createBitmap(4, 3, Bitmap.Config.ARGB_8888);
59             mTexture.setPixel(0, 0, 0xffff0000);
60             mTexture.setPixel(1, 0, 0xffffffff);
61             mTexture.setPixel(2, 0, 0xff000000);
62             mTexture.setPixel(3, 0, 0xffff0000);
63             mTexture.setPixel(0, 1, 0xffff0000);
64             mTexture.setPixel(1, 1, 0xff000000);
65             mTexture.setPixel(2, 1, 0xffffffff);
66             mTexture.setPixel(3, 1, 0xffff0000);
67             mTexture.setPixel(0, 2, 0xffff0000);
68             mTexture.setPixel(1, 2, 0xffff0000);
69             mTexture.setPixel(2, 2, 0xffff0000);
70             mTexture.setPixel(3, 2, 0xffff0000);
71         }
72 
73         @Override
onDraw(Canvas canvas)74         protected void onDraw(Canvas canvas) {
75             final int width = 100;
76             final int height = 60;
77 
78             final int left = (getWidth() - width) / 2;
79             final int top  = (getHeight() - height) / 2;
80 
81             canvas.save();
82             canvas.translate(0.0f, -height * 2 - 20.0f);
83 
84             mPatch3.setBounds(left, top, left + height, top + width);
85             mPatch3.draw(canvas);
86 
87             canvas.restore();
88 
89             mPatch1.setBounds(left, top, left + width, top + height);
90             mPatch1.draw(canvas);
91 
92             canvas.save();
93             canvas.translate(0.0f, height + 20.0f);
94 
95             mPatch2.setBounds(left, top, left + width, top + height);
96             mPatch2.draw(canvas);
97 
98             canvas.restore();
99 
100 //            Rect src = new Rect(1, 0, 3, 2);
101 //            RectF dst = new RectF(0, 0, getWidth(), getHeight());
102 //            canvas.drawBitmap(mTexture, src, dst, null);
103         }
104     }
105 }
106