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.Canvas;
24 import android.graphics.Paint;
25 import android.graphics.Rect;
26 import android.graphics.RectF;
27 import android.os.Bundle;
28 import android.view.View;
29 
30 @SuppressWarnings({"UnusedDeclaration"})
31 public class BitmapsRectActivity extends Activity {
32     @Override
onCreate(Bundle savedInstanceState)33     protected void onCreate(Bundle savedInstanceState) {
34         super.onCreate(savedInstanceState);
35         final BitmapsView view = new BitmapsView(this);
36         setContentView(view);
37     }
38 
39     static class BitmapsView extends View {
40         private Paint mBitmapPaint;
41         private final Bitmap mBitmap1;
42         private final Bitmap mBitmap2;
43         private final Rect mSrcRect;
44         private final RectF mDstRect;
45         private final RectF mDstRect2;
46 
BitmapsView(Context c)47         BitmapsView(Context c) {
48             super(c);
49 
50             mBitmap1 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
51             mBitmap2 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset2);
52 
53             mBitmapPaint = new Paint();
54             mBitmapPaint.setFilterBitmap(true);
55 
56             final float fourth = mBitmap1.getWidth() / 4.0f;
57             final float half = mBitmap1.getHeight() / 2.0f;
58             mSrcRect = new Rect((int) fourth, (int) (half - half / 2.0f),
59                     (int) (fourth + fourth), (int) (half + half / 2.0f));
60             mDstRect = new RectF(fourth, half - half / 2.0f, fourth + fourth, half + half / 2.0f);
61             mDstRect2 = new RectF(fourth, half - half / 2.0f,
62                     (fourth + fourth) * 3.0f, (half + half / 2.0f) * 3.0f);
63         }
64 
65         @Override
onDraw(Canvas canvas)66         protected void onDraw(Canvas canvas) {
67             super.onDraw(canvas);
68 
69             canvas.translate(120.0f, 50.0f);
70             canvas.drawBitmap(mBitmap1, mSrcRect, mDstRect, mBitmapPaint);
71 
72             canvas.translate(0.0f, mBitmap1.getHeight());
73             canvas.translate(-100.0f, 25.0f);
74             canvas.drawBitmap(mBitmap1, mSrcRect, mDstRect2, mBitmapPaint);
75         }
76     }
77 }