`

两个Activity之间中轴旋转切换

阅读更多
直接上代码:
1.TranslateLayout.java文件(页面1)
package cn.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.view.View;
import android.view.View.OnClickListener;

public class TranslateLayout extends Activity implements OnClickListener {

	RelativeLayout layout1;

	String tag;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.first);
		ImageView first_btn = (ImageView) findViewById(R.id.first_btn);
		first_btn.setOnClickListener(this);

		layout1 = (RelativeLayout) findViewById(R.id.layout1);
		showView();

	}

	public void showView() {
		/* 取得Intent中的Bundle对象 */
		Bundle bundle = this.getIntent().getExtras();

		if (bundle != null) {
			/* 取得Bundle对象中的数据 */
			tag = bundle.getString("second");
			System.out.println("tag =" + tag);
			if (tag.equals("Second")) {
				rotateHelper = new RotationHelper(this,
						Constants.KEY_FIRST_CLOCKWISE);
				rotateHelper.applyLastRotation(layout1, -90, 0);
			}
		}
	}

	RotationHelper rotateHelper;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		rotateHelper = new RotationHelper(this, Constants.KEY_FIRST_INVERSE);
		rotateHelper.applyFirstRotation(layout1, 0, -90);
	}

	public void jumpToSecond() {
		Intent in = new Intent();
		in.setClass(this, Second.class);
		// new一个Bundle对象,并将要传递的数据传入
		Bundle bundle = new Bundle();
		bundle.putString("front", "First");
		/* 将Bundle对象assign给Intent */
		in.putExtras(bundle);
		// 如果已经打开过的实例,将不会重新打开新的Activity
		// in.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		startActivity(in);
		finish();
	}

}


2. Second.java文件(页面2)
package cn.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class Second extends Activity implements OnClickListener {

	String tag = "";

	RotationHelper rotateHelper;

	RelativeLayout layout2;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);

		layout2 = (RelativeLayout) findViewById(R.id.layout2);
		showView();
		setListener();
	}

	public void setListener() {
		ImageView second_btn = (ImageView) findViewById(R.id.second_btn);
		second_btn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		rotateHelper = new RotationHelper(this,
				Constants.KEY_SECOND_CLOCKWISE);
		rotateHelper.applyFirstRotation(layout2, 0, 90);
	}

	public void showView() {
		/* 取得Intent中的Bundle对象 */
		Bundle bundle = this.getIntent().getExtras();

		if (bundle != null) {
			/* 取得Bundle对象中的数据 */
			tag = bundle.getString("front");
		}

		System.out.println("bundle =" + tag);

		if (tag.equals("First")) {
			rotateHelper = new RotationHelper(this, Constants.KEY_SECOND_INVERSE);
			rotateHelper.applyLastRotation(layout2, 90, 0);
		}
	}

	public void jumpToFirst() {
		Intent in = new Intent();
		in.setClass(this, TranslateLayout.class);
		Bundle bundle = new Bundle();
		bundle.putString("second", "Second");
		in.putExtras(bundle);
		startActivity(in);
		finish();
	}

}


3.RotationHelper.java文件
package cn.com;

import android.app.Activity;
import android.util.Log;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;

public class RotationHelper {
	
	DisplayNextView displayNext;
	
	public RotationHelper(Activity con,int order){
		displayNext = new DisplayNextView(con, order);
	}
	
	// 逆时针旋转90
	public void applyFirstRotation(ViewGroup layout,float start, float end) {
		// Find the center of the container
		final float centerX = layout.getWidth() / 2.0f;
		final float centerY = layout.getHeight() / 2.0f;
		Log.i("centerX =" + centerX, "centerX");
		Log.i("centerY =" + centerY, "centerY");

		// 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(700);
		rotation.setFillAfter(true);
		rotation.setInterpolator(new AccelerateInterpolator());
		rotation.setAnimationListener(displayNext);
		layout.startAnimation(rotation);
	}
	
	public void applyLastRotation(ViewGroup layout,float start, float end) {
		// Find the center of the container
		final float centerX = layout.getWidth() / 2.0f;
		final float centerY = layout.getHeight() / 2.0f;
		Log.i("centerX =" + centerX, "centerX");
		Log.i("centerY =" + centerY, "centerY");

		// 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,
				 160, 192, 310.0f, false);
		rotation.setDuration(700);
		rotation.setFillAfter(true);
		rotation.setInterpolator(new AccelerateInterpolator());
		layout.startAnimation(rotation);
	}	
}

4. Rotate3dAnimation.java文件(动画辅助类)
package cn.com;

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);
    }
}


5.DisplayNextView.java监听动画的类
package cn.com;

import android.app.Activity;
import android.view.animation.Animation;

public class DisplayNextView implements Animation.AnimationListener {

	Object obj;

	// 动画监听器的构造函数
	Activity ac;
	int order;

	public DisplayNextView(Activity ac, int order) {
		this.ac = ac;
		this.order = order;
	}

	public void onAnimationStart(Animation animation) {
	}

	public void onAnimationEnd(Animation animation) {
		doSomethingOnEnd(order);
	}

	public void onAnimationRepeat(Animation animation) {
	}

	private final class SwapViews implements Runnable {
		public void run() {
			switch (order) {
			case Constants.KEY_FIRST_INVERSE:
				((TranslateLayout) ac).jumpToSecond();
				break;
			case Constants.KEY_SECOND_CLOCKWISE:
				((Second) ac).jumpToFirst();
				break;
			}
		}
	}

	public void doSomethingOnEnd(int _order) {
		switch (_order) {
		case Constants.KEY_FIRST_INVERSE:
			((TranslateLayout) ac).layout1.post(new SwapViews());
			break;

		case Constants.KEY_SECOND_CLOCKWISE:
			((Second) ac).layout2.post(new SwapViews());
			break;
		}
	}
}

6. Constants标识Layout的id
package cn.com;

public class Constants {

	public final static int KEY_FIRST_INVERSE = 1;

	public final static int KEY_FIRST_CLOCKWISE = 2;

	public final static int KEY_SECOND_INVERSE = 3;

	public final static int KEY_SECOND_CLOCKWISE = 4;
}


7.first.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:id="@+id/layout1">
	<ImageView android:layout_width="fill_parent" android:id="@+id/first_btn"
		android:layout_height="fill_parent" android:background="@drawable/first_bg" />
</RelativeLayout>


8.second.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:id="@+id/layout2">

	<ImageView android:layout_width="fill_parent" android:id="@+id/second_btn"
		android:layout_height="fill_parent" android:background="@drawable/second_bg" />
</RelativeLayout>


9.AndroidMenifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="cn.com" android:versionCode="1" android:versionName="1.0">
	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".TranslateLayout" android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<activity android:name=".Second" />
	</application>
</manifest> 
分享到:
评论
4 楼 WOSHICAIXIANFENG 2013-03-18  
flyingsir_zw 写道
demo 写了一个,帮助你完善,分先demo


http://dl.iteye.com/topics/download/b4c4f2e3-7693-36ae-8922-43e92b6667bf


谢谢你啊,
3 楼 flyingsir_zw 2012-08-08  
demo 写了一个,帮助你完善,分先demo


http://dl.iteye.com/topics/download/b4c4f2e3-7693-36ae-8922-43e92b6667bf
2 楼 lethecat 2012-02-22  
先做个标记,有时间试试
1 楼 WOSHICAIXIANFENG 2011-10-13  
hi, 楼主你好,
  请问,你有没有发现,在旋转的时候,有时 屏幕右侧会出线条,也就是由于屏幕重绘不及时所导致的。 
  有没有发现这个bug ? 当然如果你的Activity里页面背景是黑色的,就看不出来了。我很奇怪为何 只有右侧会出现这个问题, 左边就没有
 

相关推荐

    两个activity之间的切换 android 源代码

    本教程将深入讲解如何通过源代码实现两个Activity之间的切换。 首先,了解Activity的生命周期是关键。Activity有七个主要的生命周期方法:onCreate(), onCreate(Bundle), onStart(), onResume(), onPause(), onStop...

    Android源程序实现两个Activity之间的跳转

    ### Android源程序实现两个Activity之间的跳转 在Android开发中,`Activity`是构成应用程序的基本单元之一,负责处理用户交互并展示界面。本文将详细解释如何通过代码实现两个`Activity`之间的跳转,并分析给定的`...

    用handler在两个activity之间传递数据

    资源包括如何在一个activity A里激活另一个activity B。...然后在两个activity之间用handler传输数据。在activity A里定义全局变量handler,用sendMessage来发数据,在handler里数据以消息队列的方式发送。

    通过Intent实现Activity之间的切换,并传递数据

    在Android应用开发中,Intent是一种强大的机制,用于在组件之间建立通信桥梁,特别是在Activity之间进行切换和数据传递。本文将详细讲解如何通过Intent实现Activity之间的切换,并探讨如何利用Intent进行数据传递。 ...

    两个Activity之间传递参数的代码

    本篇文章将详细讲解如何在Android的两个Activity之间传递参数,并通过给出的`LoginActivity.java`和`MainActivity.java`两个文件为例进行说明。 首先,我们要了解在Android中,有两种主要的方式来传递数据: 1. ...

    两个activity之间跳转进度条

    本篇文章将详细讲解如何在两个`Activity`之间进行跳转时展示进度条,以提高用户体验。 首先,我们需要理解`ProgressBar`的基本用法。`ProgressBar`有两种类型:水平进度条(Horizontal)和圆形进度条(Circular)。...

    演示通过Intent在两个Activity间切换

    本教程将深入探讨如何使用Intent在两个Activity之间进行切换,以实现应用程序的导航功能。标题"演示通过Intent在两个Activity间切换"揭示了我们将重点讨论Intent在Android Activity间的交互。 首先,了解Intent的...

    Activity之间切换的各种效果

    Activity之间切换的各种效果 Activity之间切换的各种效果 Activity之间切换的各种效果 Activity之间切换的各种效果 Activity之间切换的各种效果 Activity之间切换的各种效果 Activity之间切换的各种效果 Activity...

    android 两个activity 翻转动画切换

    本文将深入探讨如何实现两个Activity之间的翻转动画切换。 首先,我们需要创建两个XML文件来定义动画效果。这些文件通常放在res/anim目录下。一个是进入动画(例如,`activity_flip_in.xml`),另一个是退出动画...

    两个activity之间的数据传递

    本教程将详细讲解如何在两个Activity之间实现数据传递,主要涉及三种常见方法:Intent的Extra数据、Bundle对象以及使用静态成员变量。我们首先从Intent的Extra数据开始。 1. **Intent的Extra数据**: Intent是...

    同一个activity中通过一个按钮切换两个不同的布局

    实现在同一个activity中通过一个按钮切换两个不同的布局,此处的按钮并非同一个按钮控件,而是在两个不同的布局文件中点击按钮实现布局的切换。参考博客:http://blog.csdn.net/u010963246/article/details/47324229

    2个Activity之间的切换动画效果

    要实现在Activity之间切换时的动画效果,我们可以利用`overridePendingTransition()`方法。当一个Activity启动另一个Activity时,系统会默认执行一个简单的过渡动画。我们可以通过自定义这个方法,传入两个整数参数...

    Android中轴旋转特效实现,制作别样的图片浏览器

    这是一种3D空间中的变换,物体绕着一个固定的点(即中轴)进行旋转。在Android中,我们可以利用Camera类和Matrix类来实现这种效果。Camera类代表了虚拟相机,可以用来控制视角和进行3D变换;Matrix类则用于处理2D...

    activity之间的切换

    这种情况下,如果每个Activity都独立切换,不仅会消耗更多的系统资源,还可能降低用户体验,因为每次切换都会涉及到Activity的生命周期方法调用和资源加载。为了解决这个问题,我们可以采用“动态切换布局”的策略,...

    Activity之间切换动画

    当用户在应用中导航时,通常会涉及到Activity之间的切换。为了提升用户体验,Android提供了丰富的动画效果,使得Activity间的切换更加流畅和吸引人。自API级别21(Android Lollipop)开始,系统内置了多种预设的...

    Android实现几个Activity之间的转换

    在Android内实现各个Activity之间的转换

    Activity跳转页面切换漂亮效果(转)

    1. 创建动画资源:在res/anim目录下创建两个XML文件,分别表示进入和退出动画。例如,创建一个滑动效果: - enter_from_right.xml: ```xml android:fromXDelta="100%" android:toXDelta="0%" android:...

    android两个activity的例子

    本文将深入探讨“android两个activity的例子”,基于提供的压缩包文件Android_TwoActivity,我们将详细介绍如何创建和管理两个Activity之间的跳转,以及相关的知识点。 首先,Android应用中的每个Activity都需要在`...

    底部导航和四个activity切换的框架

    在Android应用开发中,底部导航(Bottom Navigation)是一种常见的用户界面设计模式,它允许用户在应用的多个顶级视图之间快速切换。此设计通常包含3到5个图标,每个图标代表一个不同的功能区域。在“底部导航和四个...

Global site tag (gtag.js) - Google Analytics