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.layouttranschanging; 18 19 import android.animation.LayoutTransition; 20 import android.app.Activity; 21 import android.content.Context; 22 import android.os.Bundle; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.widget.Button; 26 import android.widget.LinearLayout; 27 import android.widget.LinearLayout.LayoutParams; 28 29 /** 30 * This example shows how to use LayoutTransition to animate simple changes in a layout 31 * container. 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=55wLsaWpQ4g. 35 */ 36 public class LayoutTransChanging extends Activity { 37 38 @Override onCreate(Bundle savedInstanceState)39 public void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.main); 42 43 final Button addButton = 44 (Button) findViewById(R.id.addButton); 45 final Button removeButton = 46 (Button) findViewById(R.id.removeButton); 47 final LinearLayout container = 48 (LinearLayout) findViewById(R.id.container); 49 final Context context = this; 50 51 // Start with two views 52 for (int i = 0; i < 2; ++i) { 53 container.addView(new ColoredView(this)); 54 } 55 56 addButton.setOnClickListener(new View.OnClickListener() { 57 @Override 58 public void onClick(View v) { 59 // Adding a view will cause a LayoutTransition animation 60 container.addView(new ColoredView(context), 1); 61 } 62 }); 63 64 removeButton.setOnClickListener(new View.OnClickListener() { 65 @Override 66 public void onClick(View v) { 67 if (container.getChildCount() > 0) { 68 // Removing a view will cause a LayoutTransition animation 69 container.removeViewAt(Math.min(1, container.getChildCount() - 1)); 70 } 71 } 72 }); 73 74 // Note that this assumes a LayoutTransition is set on the container, which is the 75 // case here because the container has the attribute "animateLayoutChanges" set to true 76 // in the layout file. You can also call setLayoutTransition(new LayoutTransition()) in 77 // code to set a LayoutTransition on any container. 78 LayoutTransition transition = container.getLayoutTransition(); 79 80 // New capability as of Jellybean; monitor the container for *all* layout changes 81 // (not just add/remove/visibility changes) and animate these changes as well. 82 transition.enableTransitionType(LayoutTransition.CHANGING); 83 } 84 85 /** 86 * Custom view painted with a random background color and two different sizes which are 87 * toggled between due to user interaction. 88 */ 89 private static class ColoredView extends View { 90 91 private boolean mExpanded = false; 92 93 private LayoutParams mCompressedParams = new LayoutParams( 94 ViewGroup.LayoutParams.MATCH_PARENT, 50); 95 96 private LayoutParams mExpandedParams = new LayoutParams( 97 ViewGroup.LayoutParams.MATCH_PARENT, 200); 98 ColoredView(Context context)99 private ColoredView(Context context) { 100 super(context); 101 int red = (int)(Math.random() * 128 + 127); 102 int green = (int)(Math.random() * 128 + 127); 103 int blue = (int)(Math.random() * 128 + 127); 104 int color = 0xff << 24 | (red << 16) | 105 (green << 8) | blue; 106 setBackgroundColor(color); 107 setLayoutParams(mCompressedParams); 108 setOnClickListener(new OnClickListener() { 109 @Override 110 public void onClick(View v) { 111 // Size changes will cause a LayoutTransition animation if the CHANGING 112 // transition is enabled 113 setLayoutParams(mExpanded ? mCompressedParams : mExpandedParams); 114 mExpanded = !mExpanded; 115 requestLayout(); 116 } 117 }); 118 } 119 } 120 } 121