Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 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 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 17 | package com.android.test.hwui; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 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; |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 28 | import android.util.Log; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 29 | import android.view.Gravity; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 30 | import android.view.View; |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 31 | import android.view.animation.Animation; |
| 32 | import android.view.animation.ScaleAnimation; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 33 | import android.widget.FrameLayout; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 34 | |
| 35 | @SuppressWarnings({"UnusedDeclaration"}) |
| 36 | public class BitmapsActivity extends Activity { |
| 37 | @Override |
| 38 | protected void onCreate(Bundle savedInstanceState) { |
| 39 | super.onCreate(savedInstanceState); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 40 | final BitmapsView view = new BitmapsView(this); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 41 | final FrameLayout layout = new FrameLayout(this); |
| 42 | layout.addView(view, new FrameLayout.LayoutParams(480, 800, Gravity.CENTER)); |
| 43 | setContentView(layout); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 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); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static class BitmapsView extends View { |
| 55 | private Paint mBitmapPaint; |
| 56 | private final Bitmap mBitmap1; |
| 57 | private final Bitmap mBitmap2; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 58 | private final PorterDuffXfermode mDstIn; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 59 | |
| 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 Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 65 | |
| 66 | Log.d("Bitmap", "mBitmap1.isMutable() = " + mBitmap1.isMutable()); |
| 67 | Log.d("Bitmap", "mBitmap2.isMutable() = " + mBitmap2.isMutable()); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 68 | |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 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 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 74 | mBitmapPaint = new Paint(); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 75 | mDstIn = new PorterDuffXfermode(PorterDuff.Mode.DST_IN); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 76 | } |
| 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 Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 99 | mBitmapPaint.setXfermode(mDstIn); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 100 | canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, mBitmapPaint); |
| 101 | |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 102 | mBitmapPaint.reset(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |