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.PorterDuff;
26 import android.graphics.PorterDuffXfermode;
27 import android.os.Bundle;
28 import android.util.Log;
29 import android.view.Gravity;
30 import android.view.View;
31 import android.view.animation.Animation;
32 import android.view.animation.ScaleAnimation;
33 import android.widget.FrameLayout;
34 
35 @SuppressWarnings({"UnusedDeclaration"})
36 public class BitmapsActivity extends Activity {
37     @Override
onCreate(Bundle savedInstanceState)38     protected void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40         final BitmapsView view = new BitmapsView(this);
41         final FrameLayout layout = new FrameLayout(this);
42         layout.addView(view, new FrameLayout.LayoutParams(480, 800, Gravity.CENTER));
43         setContentView(layout);
44 
45         ScaleAnimation a = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f,
46                 ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
47                 ScaleAnimation.RELATIVE_TO_SELF,0.5f);
48         a.setDuration(2000);
49         a.setRepeatCount(Animation.INFINITE);
50         a.setRepeatMode(Animation.REVERSE);
51         view.startAnimation(a);
52     }
53 
54     static class BitmapsView extends View {
55         private Paint mBitmapPaint;
56         private final Bitmap mBitmap1;
57         private final Bitmap mBitmap2;
58         private final PorterDuffXfermode mDstIn;
59 
BitmapsView(Context c)60         BitmapsView(Context c) {
61             super(c);
62 
63             mBitmap1 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
64             mBitmap2 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset2);
65 
66             Log.d("Bitmap", "mBitmap1.isMutable() = " + mBitmap1.isMutable());
67             Log.d("Bitmap", "mBitmap2.isMutable() = " + mBitmap2.isMutable());
68 
69             BitmapFactory.Options opts = new BitmapFactory.Options();
70             opts.inMutable = true;
71             Bitmap bitmap = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1, opts);
72             Log.d("Bitmap", "bitmap.isMutable() = " + bitmap.isMutable());
73 
74             mBitmapPaint = new Paint();
75             mDstIn = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
76         }
77 
78         @Override
onDraw(Canvas canvas)79         protected void onDraw(Canvas canvas) {
80             super.onDraw(canvas);
81 
82             canvas.translate(120.0f, 50.0f);
83             canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mBitmapPaint);
84 
85             canvas.translate(0.0f, mBitmap1.getHeight());
86             canvas.translate(0.0f, 25.0f);
87             canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, null);
88 
89             mBitmapPaint.setAlpha(127);
90             canvas.translate(0.0f, mBitmap2.getHeight());
91             canvas.translate(0.0f, 25.0f);
92             canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mBitmapPaint);
93 
94             mBitmapPaint.setAlpha(255);
95             canvas.translate(0.0f, mBitmap1.getHeight());
96             canvas.translate(0.0f, 25.0f);
97             mBitmapPaint.setColor(0xffff0000);
98             canvas.drawRect(0.0f, 0.0f, mBitmap2.getWidth(), mBitmap2.getHeight(), mBitmapPaint);
99             mBitmapPaint.setXfermode(mDstIn);
100             canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, mBitmapPaint);
101 
102             mBitmapPaint.reset();
103         }
104     }
105 }
106