Initial Contribution
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (C) 2007 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.apis.animation;
|
||||
|
||||
import android.view.animation.Animation;
|
||||
import android.view.animation.Transformation;
|
||||
import android.graphics.Camera;
|
||||
import android.graphics.Matrix;
|
||||
|
||||
/**
|
||||
* An animation that rotates the view on the Y axis between two specified angles.
|
||||
* This animation also adds a translation on the Z axis (depth) to improve the effect.
|
||||
*/
|
||||
public class Rotate3dAnimation extends Animation {
|
||||
private final float mFromDegrees;
|
||||
private final float mToDegrees;
|
||||
private final float mCenterX;
|
||||
private final float mCenterY;
|
||||
private final float mDepthZ;
|
||||
private final boolean mReverse;
|
||||
private Camera mCamera;
|
||||
|
||||
/**
|
||||
* Creates a new 3D rotation on the Y axis. The rotation is defined by its
|
||||
* start angle and its end angle. Both angles are in degrees. The rotation
|
||||
* is performed around a center point on the 2D space, definied by a pair
|
||||
* of X and Y coordinates, called centerX and centerY. When the animation
|
||||
* starts, a translation on the Z axis (depth) is performed. The length
|
||||
* of the translation can be specified, as well as whether the translation
|
||||
* should be reversed in time.
|
||||
*
|
||||
* @param fromDegrees the start angle of the 3D rotation
|
||||
* @param toDegrees the end angle of the 3D rotation
|
||||
* @param centerX the X center of the 3D rotation
|
||||
* @param centerY the Y center of the 3D rotation
|
||||
* @param reverse true if the translation should be reversed, false otherwise
|
||||
*/
|
||||
public Rotate3dAnimation(float fromDegrees, float toDegrees,
|
||||
float centerX, float centerY, float depthZ, boolean reverse) {
|
||||
mFromDegrees = fromDegrees;
|
||||
mToDegrees = toDegrees;
|
||||
mCenterX = centerX;
|
||||
mCenterY = centerY;
|
||||
mDepthZ = depthZ;
|
||||
mReverse = reverse;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(int width, int height, int parentWidth, int parentHeight) {
|
||||
super.initialize(width, height, parentWidth, parentHeight);
|
||||
mCamera = new Camera();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void applyTransformation(float interpolatedTime, Transformation t) {
|
||||
final float fromDegrees = mFromDegrees;
|
||||
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
|
||||
|
||||
final float centerX = mCenterX;
|
||||
final float centerY = mCenterY;
|
||||
final Camera camera = mCamera;
|
||||
|
||||
final Matrix matrix = t.getMatrix();
|
||||
|
||||
camera.save();
|
||||
if (mReverse) {
|
||||
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
|
||||
} else {
|
||||
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
|
||||
}
|
||||
camera.rotateY(degrees);
|
||||
camera.getMatrix(matrix);
|
||||
camera.restore();
|
||||
|
||||
matrix.preTranslate(-centerX, -centerY);
|
||||
matrix.postTranslate(centerX, centerY);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package com.example.android.apis.animation;
|
||||
|
||||
import com.example.android.apis.R;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.widget.ListView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ImageView;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.animation.Animation;
|
||||
import android.view.animation.AccelerateInterpolator;
|
||||
import android.view.animation.DecelerateInterpolator;
|
||||
|
||||
/**
|
||||
* This sample application shows how to use layout animation and various
|
||||
* transformations on views. The result is a 3D transition between a
|
||||
* ListView and an ImageView. When the user clicks the list, it flips to
|
||||
* show the picture. When the user clicks the picture, it flips to show the
|
||||
* list. The animation is made of two smaller animations: the first half
|
||||
* rotates the list by 90 degrees on the Y axis and the second half rotates
|
||||
* the picture by 90 degrees on the Y axis. When the first half finishes, the
|
||||
* list is made invisible and the picture is set visible.
|
||||
*/
|
||||
public class Transition3d extends Activity implements
|
||||
AdapterView.OnItemClickListener, View.OnClickListener {
|
||||
private ListView mPhotosList;
|
||||
private ViewGroup mContainer;
|
||||
private ImageView mImageView;
|
||||
|
||||
// Names of the photos we show in the list
|
||||
private static final String[] PHOTOS_NAMES = new String[] {
|
||||
"Lyon",
|
||||
"Livermore",
|
||||
"Tahoe Pier",
|
||||
"Lake Tahoe",
|
||||
"Grand Canyon",
|
||||
"Bodie"
|
||||
};
|
||||
|
||||
// Resource identifiers for the photos we want to display
|
||||
private static final int[] PHOTOS_RESOURCES = new int[] {
|
||||
R.drawable.photo1,
|
||||
R.drawable.photo2,
|
||||
R.drawable.photo3,
|
||||
R.drawable.photo4,
|
||||
R.drawable.photo5,
|
||||
R.drawable.photo6
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
setContentView(R.layout.animations_main_screen);
|
||||
|
||||
mPhotosList = (ListView) findViewById(android.R.id.list);
|
||||
mImageView = (ImageView) findViewById(R.id.picture);
|
||||
mContainer = (ViewGroup) findViewById(R.id.container);
|
||||
|
||||
// Prepare the ListView
|
||||
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
|
||||
android.R.layout.simple_list_item_1, PHOTOS_NAMES);
|
||||
|
||||
mPhotosList.setAdapter(adapter);
|
||||
mPhotosList.setOnItemClickListener(this);
|
||||
|
||||
// Prepare the ImageView
|
||||
mImageView.setClickable(true);
|
||||
mImageView.setFocusable(true);
|
||||
mImageView.setOnClickListener(this);
|
||||
|
||||
// Since we are caching large views, we want to keep their cache
|
||||
// between each animation
|
||||
mContainer.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup a new 3D rotation on the container view.
|
||||
*
|
||||
* @param position the item that was clicked to show a picture, or -1 to show the list
|
||||
* @param start the start angle at which the rotation must begin
|
||||
* @param end the end angle of the rotation
|
||||
*/
|
||||
private void applyRotation(int position, float start, float end) {
|
||||
// Find the center of the container
|
||||
final float centerX = mContainer.getWidth() / 2.0f;
|
||||
final float centerY = mContainer.getHeight() / 2.0f;
|
||||
|
||||
// Create a new 3D rotation with the supplied parameter
|
||||
// The animation listener is used to trigger the next animation
|
||||
final Rotate3dAnimation rotation =
|
||||
new Rotate3dAnimation(start, end, centerX, centerY, 310.0f, true);
|
||||
rotation.setDuration(500);
|
||||
rotation.setFillAfter(true);
|
||||
rotation.setInterpolator(new AccelerateInterpolator());
|
||||
rotation.setAnimationListener(new DisplayNextView(position));
|
||||
|
||||
mContainer.startAnimation(rotation);
|
||||
}
|
||||
|
||||
public void onItemClick(AdapterView parent, View v, int position, long id) {
|
||||
// Pre-load the image then start the animation
|
||||
mImageView.setImageResource(PHOTOS_RESOURCES[position]);
|
||||
applyRotation(position, 0, 90);
|
||||
}
|
||||
|
||||
public void onClick(View v) {
|
||||
applyRotation(-1, 180, 90);
|
||||
}
|
||||
|
||||
/**
|
||||
* This class listens for the end of the first half of the animation.
|
||||
* It then posts a new action that effectively swaps the views when the container
|
||||
* is rotated 90 degrees and thus invisible.
|
||||
*/
|
||||
private final class DisplayNextView implements Animation.AnimationListener {
|
||||
private final int mPosition;
|
||||
|
||||
private DisplayNextView(int position) {
|
||||
mPosition = position;
|
||||
}
|
||||
|
||||
public void onAnimationStart(Animation animation) {
|
||||
}
|
||||
|
||||
public void onAnimationEnd(Animation animation) {
|
||||
mContainer.post(new SwapViews(mPosition));
|
||||
}
|
||||
|
||||
public void onAnimationRepeat(Animation animation) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This class is responsible for swapping the views and start the second
|
||||
* half of the animation.
|
||||
*/
|
||||
private final class SwapViews implements Runnable {
|
||||
private final int mPosition;
|
||||
|
||||
public SwapViews(int position) {
|
||||
mPosition = position;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
final float centerX = mContainer.getWidth() / 2.0f;
|
||||
final float centerY = mContainer.getHeight() / 2.0f;
|
||||
Rotate3dAnimation rotation;
|
||||
|
||||
if (mPosition > -1) {
|
||||
mPhotosList.setVisibility(View.GONE);
|
||||
mImageView.setVisibility(View.VISIBLE);
|
||||
mImageView.requestFocus();
|
||||
|
||||
rotation = new Rotate3dAnimation(90, 180, centerX, centerY, 310.0f, false);
|
||||
} else {
|
||||
mImageView.setVisibility(View.GONE);
|
||||
mPhotosList.setVisibility(View.VISIBLE);
|
||||
mPhotosList.requestFocus();
|
||||
|
||||
rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 310.0f, false);
|
||||
}
|
||||
|
||||
rotation.setDuration(500);
|
||||
rotation.setFillAfter(true);
|
||||
rotation.setInterpolator(new DecelerateInterpolator());
|
||||
|
||||
mContainer.startAnimation(rotation);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
|
||||
<h3>Drawable</h3>
|
||||
<dl>
|
||||
<dt><a href="ShapeDrawable1.html">ShapeDrawable</a></dt>
|
||||
<dd>Demonstrates creating Drawables in XML.</dd>
|
||||
</dl>
|
||||
|
||||
<h3>OpenGL|ES</h3>
|
||||
<dl>
|
||||
<dt><a href="CameraPreview.html">CameraPreview</a></dt>
|
||||
<dd> Demonstrates capturing the image stream from the camera, drawing to a surface (extending SurfaceView) on a separate thread (extending Thread).</dd>
|
||||
|
||||
<dt><a href="GLSurfaceViewActivity.html">GL SurfaceView</a></dt>
|
||||
<dd>Demonstrates how to perform OpenGL rendering in to a SurfaceView.
|
||||
<dl>
|
||||
<dt>Code:
|
||||
<dd> <a href="GLSurfaceViewActivity.html">GLSurfaceViewActivity.java</a>,
|
||||
<a href="GLSurfaceView.html">GLSurfaceView.java</a>
|
||||
<dt>Layout:
|
||||
<dd> <a href="{@docRoot}samples/ApiDemos/res/layout/hello_world.html">
|
||||
hello_world.xml</a>
|
||||
</dl>
|
||||
</dd>
|
||||
|
||||
<dt><a href="PolyToPoly.html">PolyToPoly</a></dt>
|
||||
<dd>Demonstrates calling the <a href="@{docRoot}reference/android/graphics/Matrix.html#setPolyToPoly(float[],%20int,%20float[],%20int,%20int)">Matrix.setPolyToPoly()</a> method to translate coordinates on a canvas to a new perspective (used to simulate perspective). </dd>
|
||||
|
||||
<dt><a href="DrawPoints.html">DrawPoints</a></dt>
|
||||
<dd>Demonstrates using the <a href="android.graphics.Paint">Paint</a> and <a href="android.graphics.Canvas">Canvas</a> objects to draw random points on the screen, with different colors and strokes. </dd>
|
||||
|
||||
<dt><a href="PathEffects.html">PathEffects</a></dt>
|
||||
<dd> Demonstrates the use of <a href="android.graphics.Path">Path</a> and various <a href="android.graphics.PathEffect">PathEffect</a> subclasses. </dd>
|
||||
|
||||
<dt><a href="SurfaceViewOverlay.html">SurfaceView Overlay</a></dt>
|
||||
<dd>Shows how you can place overlays on top of a SurfaceView.
|
||||
<dl>
|
||||
<dt>Code:
|
||||
<dd> <a href="SurfaceViewOverlay.html">SurfaceViewOverlay.java</a>,
|
||||
<a href="GLSurfaceView.html">GLSurfaceView.java</a>
|
||||
<dt>Layout:
|
||||
<dd> <a href="{@docRoot}samples/ApiDemos/res/layout/surface_view_overlay.html">
|
||||
surface_view_overlay.xml</a>
|
||||
</dl>
|
||||
</dd>
|
||||
|
||||
<dt><a href="TouchPaint.html">TouchPaint</a></dt>
|
||||
<dd> Demonstrates the handling of touch screen events to implement a simple painting app. </dd>
|
||||
</dl>
|
||||
Reference in New Issue
Block a user