Android两个页面之间的切换效果工具类



import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.Toast;

public class ActivityAnimationUtil
{
private Context context;
private Activity activity;
public static boolean IS_V2_3 = false;

public ActivityAnimationUtil(Context context)
{
this.context = context;
this.activity = (Activity) context;
}

public void check()
{
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD)
{
IS_V2_3 = true;
}
}

@SuppressLint("ShowToast")
public void startActivity(Intent intent, StyleIn in)
{
switch (in) {
case FADE:
context.startActivity(intent);
activity.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
break;

case FLIPHORIZONTAL:
context.startActivity(intent);
activity.overridePendingTransition(R.anim.flip_horizontal_in, R.anim.flip_horizontal_out);
break;

case FLIPVERTICAL:
context.startActivity(intent);
activity.overridePendingTransition(R.anim.flip_vertical_in, R.anim.flip_vertical_out);
break;

case DISAPPEARTOPLEFT:
context.startActivity(intent);
activity.overridePendingTransition(R.anim.disappear_top_left_in, R.anim.disappear_top_left_out);
break;

case APPEARBOTTOMRIGHT:
context.startActivity(intent);
activity.overridePendingTransition(R.anim.appear_bottom_right_in, R.anim.appear_bottom_right_out);
break;

case SLIDELEFTRIGHT:
context.startActivity(intent);
activity.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
break;

case SLIDETOPBOTTOM:
context.startActivity(intent);
activity.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
break;

case SPILT:
check();
if (IS_V2_3)
{
SplitAnimation.startActivity(activity, intent);
} else
{
Toast.makeText(context, "手机系统版本过低(不得低于2.3),不支持当前动画此效果!", Toast.LENGTH_SHORT);
}
break;

case UNZOOM:
context.startActivity(intent);
activity.overridePendingTransition(R.anim.unzoom_in, R.anim.unzoom_out);
break;

case STACK:
context.startActivity(intent);
activity.overridePendingTransition(R.anim.open_next, R.anim.close_main);
break;

default:
break;
}
}

public void backActivity(StyleOut out)
{
switch (out) {
case B_FADE:
activity.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
break;

case B_FLIPHORIZONTAL:
activity.overridePendingTransition(R.anim.flip_horizontal_in, R.anim.flip_horizontal_out);
break;
case B_FLIPVERTICAL:
activity.overridePendingTransition(R.anim.flip_vertical_in, R.anim.flip_vertical_out);
break;

case B_DISAPPEARTOPLEFT:
activity.overridePendingTransition(R.anim.appear_top_left_in, R.anim.appear_top_left_out);
break;

case B_APPEARBOTTOMRIGHT:
activity.overridePendingTransition(R.anim.disappear_bottom_right_in, R.anim.disappear_bottom_right_out);
break;

case B_SLIDELEFTRIGHT:
activity.overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);
break;

case B_SLIDETOPBOTTOM:
activity.overridePendingTransition(R.anim.slide_bottom_in, R.anim.slide_top_out);
break;

case B_UNZOOM:
activity.overridePendingTransition(R.anim.unzoom_in, R.anim.unzoom_out);
break;

case B_STACK:
activity.overridePendingTransition(R.anim.open_main, R.anim.close_next);
break;

default:
break;
}
}
}

SPLIT动画工具类:


import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

/**
*
* 用代码实现类似微信开门效果
*/
@SuppressLint("NewApi")
public class SplitAnimation
{

public static Bitmap mBitmap = null;
private static int[] mLoc1;
private static int[] mLoc2;
private static ImageView mTopImage;
private static ImageView mBottomImage;
private static AnimatorSet mSetAnim;

/**
* Start a new Activity with a Split animation
*
* @param currActivity
* The current Activity
* @param intent
* The Intent needed tot start the new Activity
* @param splitYCoord
* The Y coordinate where we want to split the Activity on the animation. -1 will split the Activity equally
*/
public static void startActivity(Activity currActivity, Intent intent, int splitYCoord)
{

// Preparing the bitmaps that we need to show
prepare(currActivity, splitYCoord);
currActivity.startActivity(intent);
currActivity.overridePendingTransition(0, 0);
}

/**
* Start a new Activity with a Split animation right in the middle of the Activity
*
* @param currActivity
* The current Activity
* @param intent
* The Intent needed tot start the new Activity
*/
public static void startActivity(Activity currActivity, Intent intent)
{
startActivity(currActivity, intent, -1);
}

/**
* Preparing the graphics on the destination Activity. Should be called on the destination activity on Activity#onCreate() BEFORE setContentView()
*
* @param destActivity
* the destination Activity
*/
public static void prepareAnimation(final Activity destActivity)
{
mTopImage = createImageView(destActivity, mBitmap, mLoc1);
mBottomImage = createImageView(destActivity, mBitmap, mLoc2);
}

/**
* Start the animation the reveals the destination Activity Should be called on the destination activity on Activity#onCreate() AFTER setContentView()
*
* @param destActivity
* the destination Activity
* @param duration
* The duration of the animation
* @param interpolator
* The interpulator to use for the animation. null for no interpulation.
*/
public static void animate(final Activity destActivity, final int duration, final TimeInterpolator interpolator)
{

// Post this on the UI thread‘s message queue. It‘s needed for the items to be already measured
new Handler().post(new Runnable()
{

@Override
public void run()
{
mSetAnim = new AnimatorSet();
mTopImage.setLayerType(View.LAYER_TYPE_HARDWARE, null);
mBottomImage.setLayerType(View.LAYER_TYPE_HARDWARE, null);
mSetAnim.addListener(new Animator.AnimatorListener()
{
@Override
public void onAnimationStart(Animator animation)
{
}

@Override
public void onAnimationEnd(Animator animation)
{
clean(destActivity);
}

@Override
public void onAnimationCancel(Animator animation)
{
clean(destActivity);
}

@Override
public void onAnimationRepeat(Animator animation)
{

}
});

// Animating the 2 parts away from each other
Animator anim1 = ObjectAnimator.ofFloat(mTopImage, "translationY", mTopImage.getHeight() * -1);
Animator anim2 = ObjectAnimator.ofFloat(mBottomImage, "translationY", mBottomImage.getHeight());

if (interpolator != null)
{
anim1.setInterpolator(interpolator);
anim2.setInterpolator(interpolator);
}

mSetAnim.setDuration(duration);
mSetAnim.playTogether(anim1, anim2);
mSetAnim.start();
}
});
}

/**
* Start the animation that reveals the destination Activity Should be called on the destination activity on Activity#onCreate() AFTER setContentView()
*
* @param destActivity
* the destination Activity
* @param duration
* The duration of the animation
*/
public static void animate(final Activity destActivity, final int duration)
{
animate(destActivity, duration, new DecelerateInterpolator());
}

/**
* Cancel an in progress animation
*/
public static void cancel()
{
if (mSetAnim != null)
mSetAnim.cancel();
}

/**
* Clean stuff
*
* @param activity
* The Activity where the animation is occurring
*/
private static void clean(Activity activity)
{
if (mTopImage != null)
{
mTopImage.setLayerType(View.LAYER_TYPE_NONE, null);
try
{
// If we use the regular removeView() we‘ll get a small UI glitch
activity.getWindowManager().removeViewImmediate(mBottomImage);
} catch (Exception ignored)
{
}
}
if (mBottomImage != null)
{
mBottomImage.setLayerType(View.LAYER_TYPE_NONE, null);
try
{
activity.getWindowManager().removeViewImmediate(mTopImage);
} catch (Exception ignored)
{
}
}

mBitmap = null;
}

/**
* Preparing the graphics for the animation
*
* @param currActivity
* the current Activity from where we start the new one
* @param splitYCoord
* The Y coordinate where we want to split the activity. -1 will split the activity equally
*/
private static void prepare(Activity currActivity, int splitYCoord)
{

// Get the content of the activity and put in a bitmap
View root = currActivity.getWindow().getDecorView().findViewById(android.R.id.content);
root.setDrawingCacheEnabled(true);
mBitmap = root.getDrawingCache();

// If the split Y coordinate is -1 - We‘ll split the activity equally
splitYCoord = (splitYCoord != -1 ? splitYCoord : mBitmap.getHeight() / 2);

if (splitYCoord > mBitmap.getHeight())
throw new IllegalArgumentException("Split Y coordinate [" + splitYCoord + "] exceeds the activity‘s height [" + mBitmap.getHeight() + "]");

// Set the location to put the 2 bitmaps on the destination activity
mLoc1 = new int[] { 0, splitYCoord, root.getTop() };
mLoc2 = new int[] { splitYCoord, mBitmap.getHeight(), root.getTop() };
}

/**
* Creating the an image, containing one part of the animation on the destination activity
*
* @param destActivity
* The destination activity
* @param bmp
* The Bitmap of the part we want to add to the destination activity
* @param loc
* The location this part should be on the screen
* @return
*/
private static ImageView createImageView(Activity destActivity, Bitmap bmp, int loc[])
{
MyImageView imageView = new MyImageView(destActivity);
imageView.setImageBitmap(bmp);
imageView.setImageOffsets(bmp.getWidth(), loc[0], loc[1]);
WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams();
windowParams.gravity = Gravity.TOP;
windowParams.x = 0;
windowParams.y = loc[2] + loc[0];
windowParams.height = loc[1] - loc[0];
windowParams.width = bmp.getWidth();
windowParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
windowParams.format = PixelFormat.TRANSLUCENT;
windowParams.windowAnimations = 0;
destActivity.getWindowManager().addView(imageView, windowParams);
return imageView;
}

/**
* MyImageView Extended ImageView that draws just part of an image, base on start/end position
*/
private static class MyImageView extends ImageView
{
private Rect mSrcRect;
private Rect mDstRect;
private Paint mPaint;

public MyImageView(Context context)
{
super(context);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}

/**
* Setting the bitmap offests to control the visible area
*
* @param width
* The bitmap image
* @param bmp
* The start Y position
* @param loc
* The end Y position
* @return
*/
public void setImageOffsets(int width, int startY, int endY)
{
mSrcRect = new Rect(0, startY, width, endY);
mDstRect = new Rect(0, 0, width, endY - startY);
}

@Override
protected void onDraw(Canvas canvas)
{
Bitmap bm = null;
Drawable drawable = getDrawable();
if (null != drawable && drawable instanceof BitmapDrawable)
{
bm = ((BitmapDrawable) drawable).getBitmap();
}

if (null == bm)
{
super.onDraw(canvas);
} else
{
canvas.drawBitmap(bm, mSrcRect, mDstRect, mPaint);
}
}
}
}

动画进入枚举:


package com.functiontest.key;

public enum StyleIn
{
/** 渐变动画 */
FADE,
/** 水平伸缩效果 */
FLIPHORIZONTAL,
/** 垂直伸缩 */
FLIPVERTICAL,
/** 左上角进入动画 */
DISAPPEARTOPLEFT,
/** 右下角进入动画 */
APPEARBOTTOMRIGHT,
/** 水平平移效果 */
SLIDELEFTRIGHT,
/** 垂直平移效果 */
SLIDETOPBOTTOM,
/** 开门效果 */
SPILT,
/** 缩放动画进入 */
UNZOOM,
/** 层叠效果进入 */
STACK
}

动画推出枚举:


package com.functiontest.key;

public enum StyleOut
{
/** 渐变动画 */
B_FADE,
/** 水平伸缩效果 */
B_FLIPHORIZONTAL,
/** 垂直伸缩 */
B_FLIPVERTICAL,
/** 左上角进入动画 */
B_DISAPPEARTOPLEFT,
/** 右下角进入动画 */
B_APPEARBOTTOMRIGHT,
/** 水平平移效果 */
B_SLIDELEFTRIGHT,
/** 垂直平移效果 */
B_SLIDETOPBOTTOM,
/** 缩放动画进入 */
B_UNZOOM,
/** 层叠效果进入 */
B_STACK
}

工具类的使用方法:

第一个页面:


import com.functiontest.key.StyleIn;
import com.functiontest.uitl.ActivityAnimationUtil;
import com.functiontest.uitl.SplitAnimation;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends FinalActivity
{
@ViewInject(id = R.id.btn_next, click = "onClick")
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void onClick(View view)
{
switch (view.getId()) {
case R.id.btn_next:

Intent intent = new Intent(MainActivity.this, secondActivity.class);
ActivityAnimationUtil animation = new ActivityAnimationUtil(this);
animation.startActivity(intent, StyleIn.SPILT);

break;

default:
break;
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

第二个页面:


import com.functiontest.key.StyleOut;
import com.functiontest.uitl.ActivityAnimationUtil;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;

public class secondActivity extends FinalActivity
{
@ViewInject(id = R.id.btn_back, click = "onClick")
public Button button;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second_main);
}

public void onClick(View view)
{
switch (view.getId()) {
case R.id.btn_back:

back();

break;

default:
break;
}
}

public void back()
{
this.finish();
ActivityAnimationUtil animation = new ActivityAnimationUtil(this);
animation.backActivity(StyleOut.B_STACK);
}

@Override
public void onBackPressed()
{
super.onBackPressed();

back();
}

}

文件下载地址:http://pan.baidu.com/s/1i3eIufb

Android两个页面之间的切换效果工具类,布布扣,bubuko.com

时间: 2024-10-09 03:46:27

Android两个页面之间的切换效果工具类的相关文章

(转载) Android两个子线程之间通信

Android两个子线程之间通信 标签: classthreadandroid子线程通信 2015-03-20 17:03 3239人阅读 评论(0) 收藏 举报  分类: 个人杂谈 版权声明:本文为博主原创文章,未经博主允许不得转载. Android中,相信主线程和子线程之间的通信大家都不陌生了吧.在一次面试经历中被问到了两个子线程之间是如何进行通信的.哎呦!这可蒙住我了.后来回家研究了下,分享给大家. 其实android中线程通信无非就是handler和looper的操作. 一般情况下的主线

两个页面之间的动画跳转。

接触到了两个页面之间的跳转带动画的.效果还不错. 一,先上项目总体图. 二,上代码. AppDelegate.m文件 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; /

两个页面之间的通信

今天要给大家说的是两个不同页面之间的通信,通过一个拖拽demo来模拟: 首先,写好基础的拖拽代码: <script> window.onload = function() { var oDiv = document.getElementById('div'); oDiv.onmousedown = function(ev) { var ev = window.event || ev; var disX = ev.clientX - oDiv.offsetLeft; var disY = ev.

HTML5中window.postMessage,在两个页面之间的数据传递

HTML5中window.postMessage,在两个页面之间的数据传递 2015年11月3日 8536次浏览 关于postMessage window.postMessage虽然说是html5的功能,但是支持IE8+,假如你的网站不需要支持IE6和IE7,那么可以使用window.postMessage.关于window.postMessage,很多朋友说他可以支持跨域,不错,window.postMessage是客户端和客户端直接的数据传递,既可以跨域传递,也可以同域传递. 应用场景 我只

Android Intent实现页面之间跳转

什么是IntentIntent可以理解为信使(意图)由Intent来协助完成Android各个组件之间的通讯Intent实现页面逐渐的跳转1.startActivity(inetnt)2.startActivityForResult(intent, requestCode); onAcitivtyResult(int requestCode, int resultCode, Intent data) setResult(resultCode, data); 先创建两个xml文件firstacti

Android 两个Activity之间信息的交互

出处:http://blog.csdn.net/veryitman/article/details/6611138  感谢原文作者,整个逻辑很清楚,这备份下 多个 Activity 之间可以通过 Application 共享数据,在这里我就让两个 Activity 共享 Handler(更新UI,我一般使用 Handler),主 Activity 中更新 UI,另一个 Activity 发送更新UI的消息.这样就达到在主Activity更新UI的目的.好吧,具体看代码! 1. 主 Activit

Android两个子线程之间通信

Android中,相信主线程和子线程之间的通信大家都不陌生了吧.在一次面试经历中被问到了两个子线程之间是如何进行通信的.哎呦!这可蒙住我了.后来回家研究了下,分享给大家. 其实android中线程通信无非就是handler和looper的操作. 一般情况下的主线程和子线程之间的通信,都是通过主线程中的handler把子线程中的message发给主线程中的looper,或者,主线程中的handler通过post向looper中发送一个runnable.looper默认存在于main线程中.那么子线

如何实现两个页面之间进行传值

参考地址:http://blog.csdn.net/hlk_1135/article/details/52809468 B/S页面间通信 HTTP是无状态的协议.Web页面本身无法向下一个页面传递信息,如果需要让下一个页面得知该页面中的值,除非通过服务器.因此,Web页面保持状态并传递给其它页面,是一个重要的技术. Web页面之间传递数据,是Web程序的重要功能 在HTTP协议中一共有4种方法来完成这件事情: 1)URL传值:2)表单传值:3)Cookie方法:4)Session方法: 一.UR

两个页面之间传值需要注意的问题

今天认识到自己的一个错误, 如果从一个页面传值到另一个页面, 前边页面用request,后边页面也要用request得到 类似的,response.pageContext.session.application依次对应 一般传值我们用request请求,因为它比较简单,response比较麻烦