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两个页面之间的切换效果工具类的更多相关文章
- Vue路由实现之通过URL中的hash(#号)来实现不同页面之间的切换(图表展示、案例分析、附源码详解)
前言 本篇随笔主要写了Vue框架中路由的基本概念.路由对象属性.vue-router插件的基本使用效果展示.案例分析.原理图解.附源码地址获取. 作为自己对Vue路由进行页面跳转效果知识的总结与笔记. ...
- (转载) Android两个子线程之间通信
Android两个子线程之间通信 标签: classthreadandroid子线程通信 2015-03-20 17:03 3239人阅读 评论(0) 收藏 举报 分类: 个人杂谈 版权声明:本文为 ...
- 两个css之间的切换
需求: 头部两个按钮 两种样式之间的切换 解决办法: 结合JQ 三目运算 来处理 第一步: 把需要切换的样式设置为样式里背景,这样做的目的为了避免 js里出现过多 css代码 二来这样会显得更加的清 ...
- spring boot 之如何在两个页面之间传递值(转)
原文地址:spring boot 之如何在两个页面之间传递值 问题:页面之间的跳转,通常带有值的传输,但是,在现在比较流行的SPRING MVC WEB 开发模型中,设计机制导致页面之间的直接接跳转和 ...
- HTML5中window.postMessage,在两个页面之间的数据传递
HTML5中window.postMessage,在两个页面之间的数据传递 2015年11月3日 8536次浏览 关于postMessage window.postMessage虽然说是html5的功 ...
- Android Intent实现页面之间跳转
什么是IntentIntent可以理解为信使(意图)由Intent来协助完成Android各个组件之间的通讯Intent实现页面逐渐的跳转1.startActivity(inetnt)2.startA ...
- Android两个子线程之间通信
Android中,相信主线程和子线程之间的通信大家都不陌生了吧.在一次面试经历中被问到了两个子线程之间是如何进行通信的.哎呦!这可蒙住我了.后来回家研究了下,分享给大家. 其实android中线程通信 ...
- Android实现Activity页面跳转切换动画特效
了解Android程序设计的人应该知道,在Android 2.0之后有了overridePendingTransition(),其中里面两个参数,一个是前一个activity的退出,另一个activi ...
- Android -- 两个activity界面的切换, 显示Intent 和 隐式Intent,putExtra传递数据
1. 两个Activity之间可以通过Intent切换, 包括显示Intent 和 隐式Intent. 实例代码 MainActivity.java public class MainActivity ...
随机推荐
- “static”引发的一个错误
昨天晚上,舍友发来一个程序,先把代码贴上: #include<stdio.h>#define N 20short bufferA[N]={1,2,3,4,5,6,7,8,9,10,11, ...
- JAX-WS使用Handler Chain加工消息
承前 本文的示例,是基于前一篇文章中的实例而改进的,如果想要运行本文的代码例子,需要先实现前一篇的代码. 前一篇文章JAX-WS开发WebService初级 Handler概念 在WebService ...
- C# 基础知识总结
要学好C#,基础知识的重要性不言而喻,现将常用到的一些基础进行总结,总结如下: 1. 数据类型转换: 强制类型转换(Chart--> int): char cr='A'; int i = ...
- OneSQL助力永辉超市大卖特卖
数据库集群查询达到10w/s,更新操作5k/s,正常! 应用并发连接达到历史高峰4倍,正常! 业务平稳运行,正常! 永辉微店527大促,圆满成功!这标志着平民软件数据库工程师.accenture咨询实 ...
- ngui中 代码调用按钮事件(后来改成了按钮绑定键盘..)
ngui中 代码调用按钮事件 好烦人啊这个问题, 我弄完发上来 这个问题解决了一半 发现可以用 按钮绑定来解决这个问题,并且更安全方便快速 直接在按钮上添加一个 key binding 指定按键 搞定 ...
- mysql timestamp类型字段的CURRENT_TIMESTAMP与ON UPDATE CURRENT_TIMESTAMP属性
timestamp有两个属性,分别是CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP两种,使用情况分别如下: 1.CURRENT_TIMESTAMP 当要向 ...
- Java内存模型
1. 概述 多任务和高并发是衡量一台计算机处理器的能力重要指标之一.一般衡量一个服务器性能的高低好坏,使用每秒事务处理数(Transactions Per Second,TPS)这个指标比较能说明问题 ...
- nullable,nonnull, null_resettable以及_Null_unspecified的区别和使用
1.关键字:可以用于属性 方法和返回值参数中 关键字作用:提示作用 告诉开发者属性信息 关键字的目的:迎合swift 强语言,swift必须要指定一个对象是否为空 关键字好处:提高代码规划,减少沟通 ...
- 常用的WEB服务
1.股票行情数据 Web Service(支持香港.深圳.上海基金.债券和股票:支持多股票同时查询) http://www.webxml.com.cn/WebServices/StockInfoWS ...
- SharePoint Foundation 2013 安装出错
前段时间装foundation 13的时候遇到这个问题.怀疑是Office的问题.然后找了一些资料,问题得到了解决 解决方案: 运行 regedit,删除注册表下的office的LicenseType ...