Transition.java revision 4b66f7a53f1b5a77c3ca1c12f256cdef078c1799
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.getTimelineDuration() / 2; 151 } else if (mBeforeMediaItem == null) { 152 return mAfterMediaItem.getTimelineDuration() / 2; 153 } else { 154 return (Math.min(mAfterMediaItem.getTimelineDuration(), 155 mBeforeMediaItem.getTimelineDuration()) / 2); 156 } 157 } 158 159 /** 160 * @return The behavior 161 */ 162 public int getBehavior() { 163 return mBehavior; 164 } 165 166 /** 167 * Generate the video clip for the specified transition. 168 * This method may block for a significant amount of time. 169 * 170 * Before the method completes execution it sets the mFilename to 171 * the name of the newly generated transition video clip file. 172 */ 173 abstract void generate(); 174 175 /** 176 * Remove any resources associated with this transition 177 */ 178 void invalidate() { 179 if (mFilename != null) { 180 new File(mFilename).delete(); 181 mFilename = null; 182 } 183 } 184 185 /** 186 * @return true if the transition is generated 187 */ 188 boolean isGenerated() { 189 return (mFilename != null); 190 } 191 192 /* 193 * {@inheritDoc} 194 */ 195 @Override 196 public boolean equals(Object object) { 197 if (!(object instanceof Transition)) { 198 return false; 199 } 200 return mUniqueId.equals(((Transition)object).mUniqueId); 201 } 202 203 /* 204 * {@inheritDoc} 205 */ 206 @Override 207 public int hashCode() { 208 return mUniqueId.hashCode(); 209 } 210} 211