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.crossfading; 18 19 import android.app.Activity; 20 import android.graphics.Bitmap; 21 import android.graphics.Canvas; 22 import android.graphics.Color; 23 import android.graphics.drawable.BitmapDrawable; 24 import android.graphics.drawable.TransitionDrawable; 25 import android.os.Bundle; 26 import android.view.View; 27 import android.widget.ImageView; 28 29 /** 30 * This example shows how to use TransitionDrawable to perform a simple cross-fade effect 31 * between two drawables. 32 * 33 * Watch the associated video for this demo on the DevBytes channel of developer.android.com 34 * or on YouTube at https://www.youtube.com/watch?v=atH3o2uh_94. 35 */ 36 public class CrossFading extends Activity { 37 38 int mCurrentDrawable = 0; 39 40 @Override 41 public void onCreate(Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 setContentView(R.layout.activity_cross_fading); 44 45 final ImageView imageview = (ImageView) findViewById(R.id.imageview); 46 47 // Create red and green bitmaps to cross-fade between 48 Bitmap bitmap0 = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888); 49 Bitmap bitmap1 = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888); 50 Canvas canvas = new Canvas(bitmap0); 51 canvas.drawColor(Color.RED); 52 canvas = new Canvas(bitmap1); 53 canvas.drawColor(Color.GREEN); 54 BitmapDrawable drawables[] = new BitmapDrawable[2]; 55 drawables[0] = new BitmapDrawable(getResources(), bitmap0); 56 drawables[1] = new BitmapDrawable(getResources(), bitmap1); 57 58 // Add the red/green bitmap drawables to a TransitionDrawable. They are layered 59 // in the transition drawalbe. The cross-fade effect happens by fading one out and the 60 // other in. 61 final TransitionDrawable crossfader = new TransitionDrawable(drawables); 62 imageview.setImageDrawable(crossfader); 63 64 // Clicking on the drawable will cause the cross-fade effect to run. Depending on 65 // which drawable is currently being shown, we either 'start' or 'reverse' the 66 // transition, which determines which drawable is faded out/in during the transition. 67 imageview.setOnClickListener(new View.OnClickListener() { 68 69 @Override 70 public void onClick(View v) { 71 if (mCurrentDrawable == 0) { 72 crossfader.startTransition(500); 73 mCurrentDrawable = 1; 74 } else { 75 crossfader.reverseTransition(500); 76 mCurrentDrawable = 0; 77 } 78 } 79 }); 80 } 81 } 82