ActivityOptionsCompat.java revision 3a96487b54eca412f51ad00b8f8096055e94dcbb
1/* 2 * Copyright (C) 2012 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.support.v4.app; 18 19import android.content.Context; 20import android.graphics.Bitmap; 21import android.os.Build; 22import android.os.Bundle; 23import android.view.View; 24 25/** 26 * Helper for accessing features in {@link android.app.ActivityOptions} 27 * introduced in API level 16 in a backwards compatible fashion. 28 */ 29public class ActivityOptionsCompat { 30 /** 31 * Create an ActivityOptions specifying a custom animation to run when the 32 * activity is displayed. 33 * 34 * @param context Who is defining this. This is the application that the 35 * animation resources will be loaded from. 36 * @param enterResId A resource ID of the animation resource to use for the 37 * incoming activity. Use 0 for no animation. 38 * @param exitResId A resource ID of the animation resource to use for the 39 * outgoing activity. Use 0 for no animation. 40 * @return Returns a new ActivityOptions object that you can use to supply 41 * these options as the options Bundle when starting an activity. 42 */ 43 public static ActivityOptionsCompat makeCustomAnimation(Context context, 44 int enterResId, int exitResId) { 45 if (Build.VERSION.SDK_INT >= 16) { 46 return new ActivityOptionsImplJB( 47 ActivityOptionsCompatJB.makeCustomAnimation(context, enterResId, exitResId)); 48 } 49 return new ActivityOptionsCompat(); 50 } 51 52 /** 53 * Create an ActivityOptions specifying an animation where the new activity is 54 * scaled from a small originating area of the screen to its final full 55 * representation. 56 * <p/> 57 * If the Intent this is being used with has not set its 58 * {@link android.content.Intent#setSourceBounds(android.graphics.Rect)}, 59 * those bounds will be filled in for you based on the initial bounds passed 60 * in here. 61 * 62 * @param source The View that the new activity is animating from. This 63 * defines the coordinate space for startX and startY. 64 * @param startX The x starting location of the new activity, relative to 65 * source. 66 * @param startY The y starting location of the activity, relative to source. 67 * @param startWidth The initial width of the new activity. 68 * @param startHeight The initial height of the new activity. 69 * @return Returns a new ActivityOptions object that you can use to supply 70 * these options as the options Bundle when starting an activity. 71 */ 72 public static ActivityOptionsCompat makeScaleUpAnimation(View source, 73 int startX, int startY, int startWidth, int startHeight) { 74 if (Build.VERSION.SDK_INT >= 16) { 75 return new ActivityOptionsImplJB( 76 ActivityOptionsCompatJB.makeScaleUpAnimation(source, startX, startY, 77 startWidth, startHeight)); 78 } 79 return new ActivityOptionsCompat(); 80 } 81 82 /** 83 * Create an ActivityOptions specifying an animation where a thumbnail is 84 * scaled from a given position to the new activity window that is being 85 * started. 86 * <p/> 87 * If the Intent this is being used with has not set its 88 * {@link android.content.Intent#setSourceBounds(android.graphics.Rect)}, 89 * those bounds will be filled in for you based on the initial thumbnail 90 * location and size provided here. 91 * 92 * @param source The View that this thumbnail is animating from. This 93 * defines the coordinate space for startX and startY. 94 * @param thumbnail The bitmap that will be shown as the initial thumbnail 95 * of the animation. 96 * @param startX The x starting location of the bitmap, relative to source. 97 * @param startY The y starting location of the bitmap, relative to source. 98 * @return Returns a new ActivityOptions object that you can use to supply 99 * these options as the options Bundle when starting an activity. 100 */ 101 public static ActivityOptionsCompat makeThumbnailScaleUpAnimation(View source, 102 Bitmap thumbnail, int startX, int startY) { 103 if (Build.VERSION.SDK_INT >= 16) { 104 return new ActivityOptionsImplJB( 105 ActivityOptionsCompatJB.makeThumbnailScaleUpAnimation(source, thumbnail, 106 startX, startY)); 107 } 108 return new ActivityOptionsCompat(); 109 } 110 111 112 private static class ActivityOptionsImplJB extends ActivityOptionsCompat { 113 private final ActivityOptionsCompatJB mImpl; 114 115 ActivityOptionsImplJB(ActivityOptionsCompatJB impl) { 116 mImpl = impl; 117 } 118 119 @Override 120 public Bundle toBundle() { 121 return mImpl.toBundle(); 122 } 123 124 @Override 125 public void update(ActivityOptionsCompat otherOptions) { 126 if (otherOptions instanceof ActivityOptionsImplJB) { 127 ActivityOptionsImplJB otherImpl = (ActivityOptionsImplJB)otherOptions; 128 mImpl.update(otherImpl.mImpl); 129 } 130 } 131 } 132 133 134 protected ActivityOptionsCompat() { 135 } 136 137 /** 138 * Returns the created options as a Bundle, which can be passed to 139 * {@link ActivityCompat#startActivity(android.app.Activity, android.content.Intent, android.os.Bundle)}. 140 * Note that the returned Bundle is still owned by the ActivityOptions 141 * object; you must not modify it, but can supply it to the startActivity 142 * methods that take an options Bundle. 143 */ 144 public Bundle toBundle() { 145 return null; 146 } 147 148 /** 149 * Update the current values in this ActivityOptions from those supplied in 150 * otherOptions. Any values defined in otherOptions replace those in the 151 * base options. 152 */ 153 public void update(ActivityOptionsCompat otherOptions) { 154 // Do nothing. 155 } 156} 157