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.Context;
21 import android.graphics.Bitmap;
22 import android.graphics.BitmapFactory;
23 import android.graphics.BitmapShader;
24 import android.graphics.Canvas;
25 import android.graphics.Matrix;
26 import android.graphics.Paint;
27 import android.graphics.Rect;
28 import android.graphics.RectF;
29 import android.graphics.Shader;
30 import android.os.Bundle;
31 import android.view.View;
32 
33 @SuppressWarnings({"UnusedDeclaration"})
34 public class Alpha8BitmapActivity extends Activity {
35     @Override
onCreate(Bundle savedInstanceState)36     protected void onCreate(Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38         setContentView(new BitmapsView(this));
39     }
40 
41     static class BitmapsView extends View {
42         private Paint mBitmapPaint;
43         private final Bitmap mBitmap1;
44         private final float[] mVertices;
45 
BitmapsView(Context c)46         BitmapsView(Context c) {
47             super(c);
48 
49             Bitmap texture = BitmapFactory.decodeResource(c.getResources(), R.drawable.spot_mask);
50             mBitmap1 = Bitmap.createBitmap(texture.getWidth(), texture.getHeight(),
51                     Bitmap.Config.ALPHA_8);
52             Canvas canvas = new Canvas(mBitmap1);
53             canvas.drawBitmap(texture, 0.0f, 0.0f, null);
54 
55             texture = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
56             BitmapShader shader = new BitmapShader(texture,
57                     Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
58 
59             final float width = texture.getWidth() / 3.0f;
60             final float height = texture.getHeight() / 3.0f;
61 
62             mVertices = new float[] {
63                     0.0f, 0.0f, width, 0.0f, width * 2, 0.0f, width * 3, 0.0f,
64                     0.0f, height, width, height, width * 2, height, width * 4, height,
65                     0.0f, height * 2, width, height * 2, width * 2, height * 2, width * 3, height * 2,
66                     0.0f, height * 4, width, height * 4, width * 2, height * 4, width * 4, height * 4,
67             };
68 
69             mBitmapPaint = new Paint();
70             mBitmapPaint.setFilterBitmap(true);
71             mBitmapPaint.setShader(shader);
72         }
73 
74         @Override
onDraw(Canvas canvas)75         protected void onDraw(Canvas canvas) {
76             super.onDraw(canvas);
77 
78             canvas.drawColor(0xffffffff);
79             canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mBitmapPaint);
80 
81             Matrix matrix = new Matrix();
82             matrix.setScale(2.0f, 2.0f);
83             matrix.postTranslate(0.0f, mBitmap1.getHeight());
84             canvas.drawBitmap(mBitmap1, matrix, mBitmapPaint);
85 
86             Rect src = new Rect(0, 0, mBitmap1.getWidth() / 2, mBitmap1.getHeight() / 2);
87             Rect dst = new Rect(0, mBitmap1.getHeight() * 3, mBitmap1.getWidth(),
88                     mBitmap1.getHeight() * 4);
89             canvas.drawBitmap(mBitmap1, src, dst, mBitmapPaint);
90 
91             canvas.translate(0.0f, mBitmap1.getHeight() * 4);
92             canvas.drawBitmapMesh(mBitmap1, 3, 3, mVertices, 0, null, 0, mBitmapPaint);
93 
94             invalidate();
95         }
96     }
97 }
98