blob: 607a1738c13ac2a0fa0895e19e8f42c7d74bf1e7 [file] [log] [blame]
Romain Guyce0537b2010-06-29 21:05:21 -07001/*
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
Romain Guyf607bdc2010-09-10 19:20:06 -070017package com.android.test.hwui;
Romain Guyce0537b2010-06-29 21:05:21 -070018
19import android.app.Activity;
20import android.content.Context;
21import android.graphics.Bitmap;
22import android.graphics.BitmapFactory;
23import android.graphics.Canvas;
24import android.graphics.Paint;
25import android.graphics.PorterDuff;
26import android.graphics.PorterDuffXfermode;
27import android.os.Bundle;
Romain Guy23610982011-01-17 12:51:55 -080028import android.util.Log;
Romain Guyd27977d2010-07-14 19:18:51 -070029import android.view.Gravity;
Romain Guyce0537b2010-06-29 21:05:21 -070030import android.view.View;
Romain Guyc1396e92010-06-30 17:56:19 -070031import android.view.animation.Animation;
32import android.view.animation.ScaleAnimation;
Romain Guyd27977d2010-07-14 19:18:51 -070033import android.widget.FrameLayout;
Romain Guyce0537b2010-06-29 21:05:21 -070034
35@SuppressWarnings({"UnusedDeclaration"})
36public class BitmapsActivity extends Activity {
37 @Override
38 protected void onCreate(Bundle savedInstanceState) {
39 super.onCreate(savedInstanceState);
Romain Guyc1396e92010-06-30 17:56:19 -070040 final BitmapsView view = new BitmapsView(this);
Romain Guyd27977d2010-07-14 19:18:51 -070041 final FrameLayout layout = new FrameLayout(this);
42 layout.addView(view, new FrameLayout.LayoutParams(480, 800, Gravity.CENTER));
43 setContentView(layout);
Romain Guyc1396e92010-06-30 17:56:19 -070044
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);
Romain Guyce0537b2010-06-29 21:05:21 -070052 }
53
54 static class BitmapsView extends View {
55 private Paint mBitmapPaint;
56 private final Bitmap mBitmap1;
57 private final Bitmap mBitmap2;
Romain Guyd27977d2010-07-14 19:18:51 -070058 private final PorterDuffXfermode mDstIn;
Romain Guyce0537b2010-06-29 21:05:21 -070059
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);
Romain Guy23610982011-01-17 12:51:55 -080065
66 Log.d("Bitmap", "mBitmap1.isMutable() = " + mBitmap1.isMutable());
67 Log.d("Bitmap", "mBitmap2.isMutable() = " + mBitmap2.isMutable());
Romain Guyce0537b2010-06-29 21:05:21 -070068
Romain Guy23610982011-01-17 12:51:55 -080069 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
Romain Guyce0537b2010-06-29 21:05:21 -070074 mBitmapPaint = new Paint();
Romain Guyd27977d2010-07-14 19:18:51 -070075 mDstIn = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
Romain Guyce0537b2010-06-29 21:05:21 -070076 }
77
78 @Override
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);
Romain Guyd27977d2010-07-14 19:18:51 -070099 mBitmapPaint.setXfermode(mDstIn);
Romain Guyce0537b2010-06-29 21:05:21 -0700100 canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, mBitmapPaint);
101
Romain Guyd27977d2010-07-14 19:18:51 -0700102 mBitmapPaint.reset();
Romain Guyce0537b2010-06-29 21:05:21 -0700103 }
104 }
105}