BitmapsActivity.java revision 2361098da3b9d9c3eeed410dc72ba62c0e9177cf
1ce0537b80087a6225273040a987414b1dd081aa0Romain Guy/* 2ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * Copyright (C) 2010 The Android Open Source Project 3ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * 4ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * Licensed under the Apache License, Version 2.0 (the "License"); 5ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * you may not use this file except in compliance with the License. 6ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * You may obtain a copy of the License at 7ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * 8ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * http://www.apache.org/licenses/LICENSE-2.0 9ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * 10ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * Unless required by applicable law or agreed to in writing, software 11ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * distributed under the License is distributed on an "AS IS" BASIS, 12ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * See the License for the specific language governing permissions and 14ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * limitations under the License. 15ce0537b80087a6225273040a987414b1dd081aa0Romain Guy */ 16ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 17f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guypackage com.android.test.hwui; 18ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 19ce0537b80087a6225273040a987414b1dd081aa0Romain Guyimport android.app.Activity; 20ce0537b80087a6225273040a987414b1dd081aa0Romain Guyimport android.content.Context; 21ce0537b80087a6225273040a987414b1dd081aa0Romain Guyimport android.graphics.Bitmap; 22ce0537b80087a6225273040a987414b1dd081aa0Romain Guyimport android.graphics.BitmapFactory; 23ce0537b80087a6225273040a987414b1dd081aa0Romain Guyimport android.graphics.Canvas; 24ce0537b80087a6225273040a987414b1dd081aa0Romain Guyimport android.graphics.Paint; 25ce0537b80087a6225273040a987414b1dd081aa0Romain Guyimport android.graphics.PorterDuff; 26ce0537b80087a6225273040a987414b1dd081aa0Romain Guyimport android.graphics.PorterDuffXfermode; 27ce0537b80087a6225273040a987414b1dd081aa0Romain Guyimport android.os.Bundle; 282361098da3b9d9c3eeed410dc72ba62c0e9177cfRomain Guyimport android.util.Log; 29d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyimport android.view.Gravity; 30ce0537b80087a6225273040a987414b1dd081aa0Romain Guyimport android.view.View; 31c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guyimport android.view.animation.Animation; 32c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guyimport android.view.animation.ScaleAnimation; 33d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyimport android.widget.FrameLayout; 34ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 35ce0537b80087a6225273040a987414b1dd081aa0Romain Guy@SuppressWarnings({"UnusedDeclaration"}) 36ce0537b80087a6225273040a987414b1dd081aa0Romain Guypublic class BitmapsActivity extends Activity { 37ce0537b80087a6225273040a987414b1dd081aa0Romain Guy @Override 38ce0537b80087a6225273040a987414b1dd081aa0Romain Guy protected void onCreate(Bundle savedInstanceState) { 39ce0537b80087a6225273040a987414b1dd081aa0Romain Guy super.onCreate(savedInstanceState); 40c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guy final BitmapsView view = new BitmapsView(this); 41d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy final FrameLayout layout = new FrameLayout(this); 42d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy layout.addView(view, new FrameLayout.LayoutParams(480, 800, Gravity.CENTER)); 43d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy setContentView(layout); 44c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guy 45c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guy ScaleAnimation a = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f, 46c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guy ScaleAnimation.RELATIVE_TO_SELF, 0.5f, 47c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guy ScaleAnimation.RELATIVE_TO_SELF,0.5f); 48c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guy a.setDuration(2000); 49c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guy a.setRepeatCount(Animation.INFINITE); 50c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guy a.setRepeatMode(Animation.REVERSE); 51c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guy view.startAnimation(a); 52ce0537b80087a6225273040a987414b1dd081aa0Romain Guy } 53ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 54ce0537b80087a6225273040a987414b1dd081aa0Romain Guy static class BitmapsView extends View { 55ce0537b80087a6225273040a987414b1dd081aa0Romain Guy private Paint mBitmapPaint; 56ce0537b80087a6225273040a987414b1dd081aa0Romain Guy private final Bitmap mBitmap1; 57ce0537b80087a6225273040a987414b1dd081aa0Romain Guy private final Bitmap mBitmap2; 58d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy private final PorterDuffXfermode mDstIn; 59ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 60ce0537b80087a6225273040a987414b1dd081aa0Romain Guy BitmapsView(Context c) { 61ce0537b80087a6225273040a987414b1dd081aa0Romain Guy super(c); 62ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 63ce0537b80087a6225273040a987414b1dd081aa0Romain Guy mBitmap1 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1); 64ce0537b80087a6225273040a987414b1dd081aa0Romain Guy mBitmap2 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset2); 652361098da3b9d9c3eeed410dc72ba62c0e9177cfRomain Guy 662361098da3b9d9c3eeed410dc72ba62c0e9177cfRomain Guy Log.d("Bitmap", "mBitmap1.isMutable() = " + mBitmap1.isMutable()); 672361098da3b9d9c3eeed410dc72ba62c0e9177cfRomain Guy Log.d("Bitmap", "mBitmap2.isMutable() = " + mBitmap2.isMutable()); 68ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 692361098da3b9d9c3eeed410dc72ba62c0e9177cfRomain Guy BitmapFactory.Options opts = new BitmapFactory.Options(); 702361098da3b9d9c3eeed410dc72ba62c0e9177cfRomain Guy opts.inMutable = true; 712361098da3b9d9c3eeed410dc72ba62c0e9177cfRomain Guy Bitmap bitmap = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1, opts); 722361098da3b9d9c3eeed410dc72ba62c0e9177cfRomain Guy Log.d("Bitmap", "bitmap.isMutable() = " + bitmap.isMutable()); 732361098da3b9d9c3eeed410dc72ba62c0e9177cfRomain Guy 74ce0537b80087a6225273040a987414b1dd081aa0Romain Guy mBitmapPaint = new Paint(); 75d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy mDstIn = new PorterDuffXfermode(PorterDuff.Mode.DST_IN); 76ce0537b80087a6225273040a987414b1dd081aa0Romain Guy } 77ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 78ce0537b80087a6225273040a987414b1dd081aa0Romain Guy @Override 79ce0537b80087a6225273040a987414b1dd081aa0Romain Guy protected void onDraw(Canvas canvas) { 80ce0537b80087a6225273040a987414b1dd081aa0Romain Guy super.onDraw(canvas); 81ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 82ce0537b80087a6225273040a987414b1dd081aa0Romain Guy canvas.translate(120.0f, 50.0f); 83ce0537b80087a6225273040a987414b1dd081aa0Romain Guy canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mBitmapPaint); 84ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 85ce0537b80087a6225273040a987414b1dd081aa0Romain Guy canvas.translate(0.0f, mBitmap1.getHeight()); 86ce0537b80087a6225273040a987414b1dd081aa0Romain Guy canvas.translate(0.0f, 25.0f); 87ce0537b80087a6225273040a987414b1dd081aa0Romain Guy canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, null); 88ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 89ce0537b80087a6225273040a987414b1dd081aa0Romain Guy mBitmapPaint.setAlpha(127); 90ce0537b80087a6225273040a987414b1dd081aa0Romain Guy canvas.translate(0.0f, mBitmap2.getHeight()); 91ce0537b80087a6225273040a987414b1dd081aa0Romain Guy canvas.translate(0.0f, 25.0f); 92ce0537b80087a6225273040a987414b1dd081aa0Romain Guy canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mBitmapPaint); 93ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 94ce0537b80087a6225273040a987414b1dd081aa0Romain Guy mBitmapPaint.setAlpha(255); 95ce0537b80087a6225273040a987414b1dd081aa0Romain Guy canvas.translate(0.0f, mBitmap1.getHeight()); 96ce0537b80087a6225273040a987414b1dd081aa0Romain Guy canvas.translate(0.0f, 25.0f); 97ce0537b80087a6225273040a987414b1dd081aa0Romain Guy mBitmapPaint.setColor(0xffff0000); 98ce0537b80087a6225273040a987414b1dd081aa0Romain Guy canvas.drawRect(0.0f, 0.0f, mBitmap2.getWidth(), mBitmap2.getHeight(), mBitmapPaint); 99d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy mBitmapPaint.setXfermode(mDstIn); 100ce0537b80087a6225273040a987414b1dd081aa0Romain Guy canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, mBitmapPaint); 101ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 102d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy mBitmapPaint.reset(); 103ce0537b80087a6225273040a987414b1dd081aa0Romain Guy } 104ce0537b80087a6225273040a987414b1dd081aa0Romain Guy } 105ce0537b80087a6225273040a987414b1dd081aa0Romain Guy} 106