Home | History | Annotate | Download | only in hwui
      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.animation.ObjectAnimator;
     20 import android.app.Activity;
     21 import android.content.Context;
     22 import android.graphics.Bitmap;
     23 import android.graphics.Canvas;
     24 import android.graphics.Paint;
     25 import android.os.Bundle;
     26 import android.view.Gravity;
     27 import android.view.View;
     28 import android.widget.FrameLayout;
     29 
     30 @SuppressWarnings({"UnusedDeclaration"})
     31 public class BitmapMutateActivity extends Activity {
     32     private static final int PATTERN_SIZE = 400;
     33 
     34     private ObjectAnimator mAnimator;
     35 
     36     @Override
     37     protected void onCreate(Bundle savedInstanceState) {
     38         super.onCreate(savedInstanceState);
     39         final BitmapsView view = new BitmapsView(this);
     40         final FrameLayout layout = new FrameLayout(this);
     41 
     42         layout.addView(view, new FrameLayout.LayoutParams(480, 800, Gravity.CENTER));
     43 
     44         setContentView(layout);
     45 
     46         mAnimator = ObjectAnimator.ofInt(view, "offset", 0, PATTERN_SIZE - 1);
     47         mAnimator.setDuration(1500);
     48         mAnimator.setRepeatCount(ObjectAnimator.INFINITE);
     49         mAnimator.setRepeatMode(ObjectAnimator.REVERSE);
     50         mAnimator.start();
     51     }
     52 
     53     @Override
     54     protected void onDestroy() {
     55         super.onDestroy();
     56         mAnimator.cancel();
     57     }
     58 
     59     static class BitmapsView extends View {
     60         private final Paint mBitmapPaint;
     61         private final Bitmap mBitmap1;
     62         private final int[] mPixels;
     63 
     64         private int mOffset;
     65         private int mSlice;
     66         private static final int[] mShifts = new int[] { 16, 8, 0 };
     67 
     68         BitmapsView(Context c) {
     69             super(c);
     70 
     71             mBitmap1 = Bitmap.createBitmap(PATTERN_SIZE, PATTERN_SIZE, Bitmap.Config.ARGB_8888);
     72             mBitmapPaint = new Paint();
     73 
     74             mPixels = new int[mBitmap1.getWidth() * mBitmap1.getHeight()];
     75             mSlice = mBitmap1.getWidth() / 3;
     76         }
     77 
     78         public void setOffset(int offset) {
     79             mOffset = offset;
     80             invalidate();
     81         }
     82 
     83         @Override
     84         protected void onDraw(Canvas canvas) {
     85             super.onDraw(canvas);
     86 
     87             int width = mBitmap1.getWidth();
     88             int height = mBitmap1.getHeight();
     89 
     90             canvas.translate((getWidth() - width) / 2, 0);
     91 
     92             for (int x = 0; x < width; x++) {
     93                 int color = 0xff000000;
     94                 int i = x == 0 ? 0 : x - 1;
     95                 color |= (int) ((0xff * ((i + mOffset) % mSlice) / (float) mSlice)) <<
     96                         mShifts[i / mSlice];
     97                 for (int y = 0; y < height; y++) {
     98                     mPixels[y * width + x] = color;
     99                 }
    100             }
    101 
    102             mBitmap1.setPixels(mPixels, 0, width, 0, 0, width, height);
    103             canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mBitmapPaint);
    104 
    105             canvas.save();
    106             canvas.translate(0.0f, height + 32);
    107             canvas.drawBitmap(mPixels, 0, width, 0.0f, 0.0f, width, height, false, mBitmapPaint);
    108             canvas.restore();
    109         }
    110     }
    111 }
    112