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.pictureviewer; 18 19 import android.app.Activity; 20 import android.graphics.Bitmap; 21 import android.graphics.BitmapFactory; 22 import android.graphics.Color; 23 import android.graphics.drawable.BitmapDrawable; 24 import android.os.Bundle; 25 import android.view.View; 26 import android.widget.ImageView; 27 28 /** 29 * This example shows how to use ViewPropertyAnimator to get a cross-fade effect as new 30 * bitmaps get installed in an ImageView. 31 * 32 * Watch the associated video for this demo on the DevBytes channel of developer.android.com 33 * or on YouTube at https://www.youtube.com/watch?v=9XbKMUtVnJA. 34 */ 35 public class PictureViewer extends Activity { 36 37 int mCurrentDrawable = 0; 38 int drawableIDs[] = { 39 R.drawable.p1, 40 R.drawable.p2, 41 R.drawable.p3, 42 R.drawable.p4, 43 }; 44 45 @Override onCreate(Bundle savedInstanceState)46 public void onCreate(Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 setContentView(R.layout.activity_picture_viewer); 49 50 // This app works by having two views, which get faded in/out for the cross-fade effect 51 final ImageView prevImageView = (ImageView) findViewById(R.id.prevImageView); 52 final ImageView nextImageView = (ImageView) findViewById(R.id.nextImageView); 53 prevImageView.setBackgroundColor(Color.TRANSPARENT); 54 nextImageView.setBackgroundColor(Color.TRANSPARENT); 55 56 // Setup default ViewPropertyAnimator durations for the two ImageViews 57 prevImageView.animate().setDuration(1000); 58 nextImageView.animate().setDuration(1000); 59 60 // NOte that a real app would do this more robustly, and not just load all possible 61 // bitmaps at onCreate() time. 62 final BitmapDrawable drawables[] = new BitmapDrawable[drawableIDs.length]; 63 for (int i = 0; i < drawableIDs.length; ++i) { 64 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 65 drawableIDs[i]); 66 drawables[i] = new BitmapDrawable(getResources(), bitmap); 67 } 68 prevImageView.setImageDrawable(drawables[0]); 69 nextImageView.setImageDrawable(drawables[1]); 70 71 prevImageView.setOnClickListener(new View.OnClickListener() { 72 73 @Override 74 public void onClick(View v) { 75 // Use ViewPropertyAnimator to fade the previous imageView out and the next one in 76 prevImageView.animate().alpha(0).withLayer(); 77 nextImageView.animate().alpha(1).withLayer(). 78 withEndAction(new Runnable() { 79 // When the animation ends, set up references to change the prev/next 80 // associations 81 @Override 82 public void run() { 83 mCurrentDrawable = 84 (mCurrentDrawable + 1) % drawables.length; 85 int nextDrawableIndex = 86 (mCurrentDrawable + 1) % drawables.length; 87 prevImageView.setImageDrawable(drawables[mCurrentDrawable]); 88 nextImageView.setImageDrawable(drawables[nextDrawableIndex]); 89 nextImageView.setAlpha(0f); 90 prevImageView.setAlpha(1f); 91 } 92 }); 93 } 94 }); 95 } 96 97 } 98