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