Home | History | Annotate | Download | only in activityanim
      1 /*
      2  * Copyright (C) 2013 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.example.android.activityanim;
     18 
     19 import java.util.ArrayList;
     20 import java.util.HashMap;
     21 
     22 import android.app.Activity;
     23 import android.content.Intent;
     24 import android.content.res.Resources;
     25 import android.graphics.ColorMatrix;
     26 import android.graphics.ColorMatrixColorFilter;
     27 import android.graphics.drawable.BitmapDrawable;
     28 import android.os.Bundle;
     29 import android.view.Menu;
     30 import android.view.MenuItem;
     31 import android.view.View;
     32 import android.widget.GridLayout;
     33 import android.widget.ImageView;
     34 
     35 /**
     36  * This example shows how to create a custom activity animation when you want something more
     37  * than window animations can provide. The idea is to disable window animations for the
     38  * activities and to instead launch or return from the sub-activity immediately, but use
     39  * property animations inside the activities to customize the transition.
     40  *
     41  * Watch the associated video for this demo on the DevBytes channel of developer.android.com
     42  * or on the DevBytes playlist in the androiddevelopers channel on YouTube at
     43  * https://www.youtube.com/playlist?list=PLWz5rJ2EKKc_XOgcRukSoKKjewFJZrKV0.
     44  */
     45 public class ActivityAnimations extends Activity {
     46 
     47     private static final String PACKAGE = "com.example.android.activityanim";
     48     static float sAnimatorScale = 1;
     49 
     50     GridLayout mGridLayout;
     51     HashMap<ImageView, PictureData> mPicturesData = new HashMap<ImageView, PictureData>();
     52     BitmapUtils mBitmapUtils = new BitmapUtils();
     53 
     54     @Override
     55     protected void onCreate(Bundle savedInstanceState) {
     56         super.onCreate(savedInstanceState);
     57         setContentView(R.layout.activity_animations);
     58 
     59         // Grayscale filter used on all thumbnails
     60         ColorMatrix grayMatrix = new ColorMatrix();
     61         grayMatrix.setSaturation(0);
     62         ColorMatrixColorFilter grayscaleFilter = new ColorMatrixColorFilter(grayMatrix);
     63 
     64         mGridLayout = (GridLayout) findViewById(R.id.gridLayout);
     65         mGridLayout.setColumnCount(3);
     66         mGridLayout.setUseDefaultMargins(true);
     67 
     68         // add all photo thumbnails to layout
     69         Resources resources = getResources();
     70         ArrayList<PictureData> pictures = mBitmapUtils.loadPhotos(resources);
     71         for (int i = 0; i < pictures.size(); ++i) {
     72             PictureData pictureData = pictures.get(i);
     73             BitmapDrawable thumbnailDrawable =
     74                     new BitmapDrawable(resources, pictureData.thumbnail);
     75             thumbnailDrawable.setColorFilter(grayscaleFilter);
     76             ImageView imageView = new ImageView(this);
     77             imageView.setOnClickListener(thumbnailClickListener);
     78             imageView.setImageDrawable(thumbnailDrawable);
     79             mPicturesData.put(imageView, pictureData);
     80             mGridLayout.addView(imageView);
     81         }
     82     }
     83 
     84     @Override
     85     public boolean onCreateOptionsMenu(Menu menu) {
     86         getMenuInflater().inflate(R.menu.activity_better_window_animations, menu);
     87         return true;
     88     }
     89 
     90     @Override
     91     public boolean onOptionsItemSelected(MenuItem item) {
     92         if (item.getItemId() == R.id.menu_slow) {
     93             sAnimatorScale = item.isChecked() ? 1 : 5;
     94             item.setChecked(!item.isChecked());
     95         }
     96         return super.onOptionsItemSelected(item);
     97     }
     98 
     99     /**
    100      * When the user clicks a thumbnail, bundle up information about it and launch the
    101      * details activity.
    102      */
    103     private View.OnClickListener thumbnailClickListener = new View.OnClickListener() {
    104 
    105         @Override
    106         public void onClick(View v) {
    107             // Interesting data to pass across are the thumbnail size/location, the
    108             // resourceId of the source bitmap, the picture description, and the
    109             // orientation (to avoid returning back to an obsolete configuration if
    110             // the device rotates again in the meantime)
    111             int[] screenLocation = new int[2];
    112             v.getLocationOnScreen(screenLocation);
    113             PictureData info = mPicturesData.get(v);
    114             Intent subActivity = new Intent(ActivityAnimations.this,
    115                     PictureDetailsActivity.class);
    116             int orientation = getResources().getConfiguration().orientation;
    117             subActivity.
    118                     putExtra(PACKAGE + ".orientation", orientation).
    119                     putExtra(PACKAGE + ".resourceId", info.resourceId).
    120                     putExtra(PACKAGE + ".left", screenLocation[0]).
    121                     putExtra(PACKAGE + ".top", screenLocation[1]).
    122                     putExtra(PACKAGE + ".width", v.getWidth()).
    123                     putExtra(PACKAGE + ".height", v.getHeight()).
    124                     putExtra(PACKAGE + ".description", info.description);
    125             startActivity(subActivity);
    126 
    127             // Override transitions: we don't want the normal window animation in addition
    128             // to our custom one
    129             overridePendingTransition(0, 0);
    130         }
    131     };
    132 
    133 }
    134