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.photoeditor; 18 19 import android.content.Context; 20 import android.graphics.Rect; 21 import android.graphics.drawable.Drawable; 22 import android.util.AttributeSet; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.widget.ImageView; 26 import android.widget.LinearLayout; 27 28 import java.util.ArrayList; 29 import java.util.List; 30 31 /** 32 * Groups effects in an accordion menu style that could either expand or fold effects. 33 */ 34 public class EffectsGroup extends LinearLayout { 35 36 private static final int ANIMATION_INTERVAL = 75; 37 38 private final Drawable downArrow; 39 private final Drawable rightArrow; 40 41 private boolean expandEffects; 42 EffectsGroup(Context context, AttributeSet attrs)43 public EffectsGroup(Context context, AttributeSet attrs) { 44 super(context, attrs); 45 46 downArrow = context.getResources().getDrawable(R.drawable.arrow_down); 47 rightArrow = context.getResources().getDrawable(R.drawable.arrow_right); 48 } 49 50 @Override onSizeChanged(int w, int h, int oldw, int oldh)51 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 52 super.onSizeChanged(w, h, oldw, oldh); 53 54 if (expandEffects) { 55 requestRectangleOnScreen(new Rect(0, 0, 0, getHeight())); 56 } 57 } 58 59 @Override onFinishInflate()60 protected void onFinishInflate() { 61 super.onFinishInflate(); 62 63 final ImageView arrow = (ImageView) findViewById(R.id.group_arrow); 64 final ViewGroup container = (ViewGroup) findViewById(R.id.grouped_effects); 65 final List<View> effects = new ArrayList<View>(); 66 for (int i = 0; i < container.getChildCount(); i++) { 67 effects.add(container.getChildAt(i)); 68 } 69 70 findViewById(R.id.group_header).setOnClickListener(new OnClickListener() { 71 72 @Override 73 public void onClick(View v) { 74 75 if (container.getVisibility() == VISIBLE) { 76 expandEffects = false; 77 78 int delay = 0; 79 for (int i = effects.size() - 1; i >= 0; i--) { 80 final View effect = effects.get(i); 81 82 postDelayed(new Runnable() { 83 84 @Override 85 public void run() { 86 effect.setVisibility(GONE); 87 } 88 }, delay); 89 delay += ANIMATION_INTERVAL; 90 } 91 92 postDelayed(new Runnable() { 93 94 @Override 95 public void run() { 96 container.setVisibility(GONE); 97 arrow.setImageDrawable(rightArrow); 98 } 99 }, delay - ANIMATION_INTERVAL); 100 } else { 101 expandEffects = true; 102 103 arrow.setImageDrawable(downArrow); 104 container.setVisibility(VISIBLE); 105 106 int delay = 0; 107 for (int i = 0; i < effects.size(); i++) { 108 final View effect = effects.get(i); 109 110 postDelayed(new Runnable() { 111 112 @Override 113 public void run() { 114 effect.setVisibility(VISIBLE); 115 } 116 }, delay); 117 delay += ANIMATION_INTERVAL; 118 } 119 } 120 } 121 }); 122 } 123 } 124