Transition.java revision 05152ffd67a17491337236621aa5f7ef7c3b51db
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 17package android.media.videoeditor; 18 19import java.io.File; 20 21/** 22 * This class is super class for all transitions. Transitions (with the 23 * exception of TransitionAtStart and TransitioAtEnd) can only be inserted 24 * between media items. 25 * 26 * Adding a transition between MediaItems makes the 27 * duration of the storyboard shorter by the duration of the Transition itself. 28 * As a result, if the duration of the transition is larger than the smaller 29 * duration of the two MediaItems associated with the Transition, an exception 30 * will be thrown. 31 * 32 * During a transition, the audio track are cross-fading 33 * automatically. {@hide} 34 */ 35public abstract class Transition { 36 // The transition behavior 37 private static final int BEHAVIOR_MIN_VALUE = 0; 38 /** The transition starts slowly and speed up */ 39 public static final int BEHAVIOR_SPEED_UP = 0; 40 /** The transition start fast and speed down */ 41 public static final int BEHAVIOR_SPEED_DOWN = 1; 42 /** The transition speed is constant */ 43 public static final int BEHAVIOR_LINEAR = 2; 44 /** The transition starts fast and ends fast with a slow middle */ 45 public static final int BEHAVIOR_MIDDLE_SLOW = 3; 46 /** The transition starts slowly and ends slowly with a fast middle */ 47 public static final int BEHAVIOR_MIDDLE_FAST = 4; 48 49 private static final int BEHAVIOR_MAX_VALUE = 4; 50 51 // The unique id of the transition 52 private final String mUniqueId; 53 54 // The transition is applied at the end of this media item 55 private final MediaItem mAfterMediaItem; 56 // The transition is applied at the beginning of this media item 57 private final MediaItem mBeforeMediaItem; 58 59 // The transition behavior 60 protected final int mBehavior; 61 62 // The transition duration 63 protected long mDurationMs; 64 65 // The transition filename 66 protected String mFilename; 67 68 /** 69 * An object of this type cannot be instantiated by using the default 70 * constructor 71 */ 72 @SuppressWarnings("unused") 73 private Transition() { 74 this(null, null, null, 0, 0); 75 } 76 77 /** 78 * Constructor 79 * 80 * @param transitionId The transition id 81 * @param afterMediaItem The transition is applied to the end of this 82 * media item 83 * @param beforeMediaItem The transition is applied to the beginning of 84 * this media item 85 * @param durationMs The duration of the transition in milliseconds 86 * @param behavior The transition behavior 87 */ 88 protected Transition(String transitionId, MediaItem afterMediaItem, MediaItem beforeMediaItem, 89 long durationMs, int behavior) { 90 if (behavior < BEHAVIOR_MIN_VALUE || behavior > BEHAVIOR_MAX_VALUE) { 91 throw new IllegalArgumentException("Invalid behavior: " + behavior); 92 } 93 mUniqueId = transitionId; 94 mAfterMediaItem = afterMediaItem; 95 mBeforeMediaItem = beforeMediaItem; 96 mDurationMs = durationMs; 97 mBehavior = behavior; 98 } 99 100 /** 101 * @return The of the transition 102 */ 103 public String getId() { 104 return mUniqueId; 105 } 106 107 /** 108 * @return The media item at the end of which the transition is applied 109 */ 110 public MediaItem getAfterMediaItem() { 111 return mAfterMediaItem; 112 } 113 114 /** 115 * @return The media item at the beginning of which the transition is applied 116 */ 117 public MediaItem getBeforeMediaItem() { 118 return mBeforeMediaItem; 119 } 120 121 /** 122 * Set the duration of the transition. 123 * 124 * @param durationMs the duration of the transition in milliseconds 125 */ 126 public void setDuration(long durationMs) { 127 if (durationMs > getMaximumDuration()) { 128 throw new IllegalArgumentException("The duration is too large"); 129 } 130 131 mDurationMs = durationMs; 132 invalidate(); 133 } 134 135 /** 136 * @return the duration of the transition in milliseconds 137 */ 138 public long getDuration() { 139 return mDurationMs; 140 } 141 142 /** 143 * The duration of a transition cannot be greater than half of the minimum 144 * duration of the bounding media items. 145 * 146 * @return The maximum duration of this transition 147 */ 148 public long getMaximumDuration() { 149 if (mAfterMediaItem == null) { 150 return mBeforeMediaItem.getDuration() / 2; 151 } else if (mBeforeMediaItem == null) { 152 return mAfterMediaItem.getDuration() / 2; 153 } else { 154 return (Math.min(mAfterMediaItem.getDuration(), mBeforeMediaItem.getDuration()) / 2); 155 } 156 } 157 158 /** 159 * @return The behavior 160 */ 161 public int getBehavior() { 162 return mBehavior; 163 } 164 165 /** 166 * Generate the video clip for the specified transition. 167 * This method may block for a significant amount of time. 168 * 169 * Before the method completes execution it sets the mFilename to 170 * the name of the newly generated transition video clip file. 171 */ 172 abstract void generate(); 173 174 /** 175 * Remove any resources associated with this transition 176 */ 177 void invalidate() { 178 if (mFilename != null) { 179 new File(mFilename).delete(); 180 mFilename = null; 181 } 182 } 183 184 /** 185 * @return true if the transition is generated 186 */ 187 boolean isGenerated() { 188 return (mFilename != null); 189 } 190 191 /* 192 * {@inheritDoc} 193 */ 194 @Override 195 public boolean equals(Object object) { 196 if (!(object instanceof Transition)) { 197 return false; 198 } 199 return mUniqueId.equals(((Transition)object).mUniqueId); 200 } 201 202 /* 203 * {@inheritDoc} 204 */ 205 @Override 206 public int hashCode() { 207 return mUniqueId.hashCode(); 208 } 209} 210