android:Fragment动画的东西
最近很多人来Fragment动画是很感兴趣,我将是一个样本给大家看。
既然做,我会做动画以下类型:
注入弹出动画:从“”进入。从“上下左右”弹出,当然,你怎么组合都能够。另外你也能够加一些透明度的变化,这就看你的发挥了。
。。
1.先写动画的xml文件
做开发的都知道。在/res/anim/文件夹下,新建xml的动画文件。比方:
fragment_slide_in_from_bottom.xml
<? xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800"
android:fromYDelta="100.0%p"
android:interpolator="@android:anim/decelerate_interpolator"
android:toYDelta="0.0" />
fragment_slide_in_from_left.xml
<? xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800"
android:fromXDelta="-100.0%p"
android:interpolator="@android:anim/decelerate_interpolator"
android:toXDelta="0.0" />
fragment_slide_in_from_right.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800"
android:fromXDelta="100.0%p"
android:interpolator="@android:anim/decelerate_interpolator"
android:toXDelta="0.0" />
fragment_slide_in_from_top.xml
<?xml version="1.0" encoding="utf-8"? >
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800"
android:fromYDelta="-100.0%p"
android:interpolator="@android:anim/decelerate_interpolator"
android:toYDelta="0.0" />
上面的是进入动画。至于弹出动画,仅仅须要将from和to的值翻转一下就可以。你们都懂得,不懂得,直接去github上clone,地址在以下。
2.加入Fragment的时候,使用setCustomAnimations方法。
直接贴代码。简单明了。
package com.example.testfragment; import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
*
* @author Zheng Haibo
* @web http://www.mobctrl.net
*
*/
public class MainActivity extends ActionBarActivity { private FragmentManager fragmentManager; private Button northBtn;
private Button southBtn;
private Button eastBtn;
private Button westBtn;
private Button popBtn; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
initButton();
} private void initButton() {
northBtn = (Button) findViewById(R.id.btn_north);
southBtn = (Button) findViewById(R.id.btn_south);
eastBtn = (Button) findViewById(R.id.btn_east);
westBtn = (Button) findViewById(R.id.btn_west);
popBtn = (Button) findViewById(R.id.btn_pop);
northBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
addNorthFragment();
}
});
southBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
addSouthFragment();
}
});
eastBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
addEastFragment();
}
});
westBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
addWestFragment();
}
}); popBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
fragmentManager.popBackStack();
}
}); } private void addNorthFragment() {
addFragment(R.anim.fragment_slide_in_from_top,
R.anim.fragment_slide_out_to_top,
R.anim.fragment_slide_in_from_top,
R.anim.fragment_slide_out_to_top, 0xa0ff0000);
} private void addSouthFragment() {
addFragment(R.anim.fragment_slide_in_from_bottom,
R.anim.fragment_slide_out_to_bottom,
R.anim.fragment_slide_in_from_bottom,
R.anim.fragment_slide_out_to_bottom, 0xa000ff00);
} private void addEastFragment() {
addFragment(R.anim.fragment_slide_in_from_left,
R.anim.fragment_slide_out_to_left,
R.anim.fragment_slide_in_from_left,
R.anim.fragment_slide_out_to_left, 0xa00000ff);
} private void addWestFragment() {
addFragment(R.anim.fragment_slide_in_from_right,
R.anim.fragment_slide_out_to_right,
R.anim.fragment_slide_in_from_right,
R.anim.fragment_slide_out_to_right, 0xa0ff00ff);
} /**
* add the fragment
*
* @param arg0
* @param arg1
* @param arg2
* @param arg3
* @param color
*/
private void addFragment(int arg0, int arg1, int arg2, int arg3, int color) {
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.setCustomAnimations(arg0, arg1, arg2, arg3);
MyFragment fragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putInt("color", color);
fragment.setArguments(bundle);
ft.add(R.id.rl_container, fragment);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
非常炫的GIF效果,我就不贴了,你下载试试就知道了。
。
Github: https://github.com/nuptboyzhb/FragmentAnimationDemo
兴许问题:
animation的运行是异步的。
假设你想对animation的运行进行监听,你能够重写fragment里面的例如以下方法
/**
* if you need add animation listener for the fragment
* please use this method
*/
@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
Animation anim;
if (enter) {
anim = AnimationUtils.loadAnimation(getActivity(),
android.R.anim.fade_in);
} else {
anim = AnimationUtils.loadAnimation(getActivity(),
android.R.anim.fade_out);
} anim.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) { } public void onAnimationRepeat(Animation animation) { } public void onAnimationStart(Animation animation) { }
}); return anim;
}
然后在回调里,做你想做的事
-------------------------------------------------------------------
很多其它交流,Android开发联盟QQ群:272209595
版权声明:本文博客原创文章,博客,未经同意,不得转载。
android:Fragment动画的东西的更多相关文章
- Android -- Fragment动画异常Unknown animation name: objectAnimator
异常 Caused by: java.lang.RuntimeException: Unknown animation name: objectAnimator 异常代码 FragmentTransa ...
- [Android]Fragment自定义动画、动画监听以及兼容性包使用
Fragment是Android在API 11之后加入的一个组件,对提高Android开发中的布局合理性和布局效率都有很大作用,尤其是在Android平板等大屏幕设备的开发中,Fragment的引入能 ...
- Android Fragment 解析和使用
Android Fragment的生命周期和Activity类似,实际可能会涉及到数据传递,onSaveInstanceState的状态保存,FragmentManager的管理和Transactio ...
- Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误
嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...
- Android Fragment使用(一) 基础篇 温故知新
Fragment使用的基本知识点总结, 包括Fragment的添加, 参数传递和通信, 生命周期和各种操作. Fragment使用基础 Fragment添加 方法一: 布局里的标签 标识符: tag, ...
- Android Fragment
1.Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期. 2.Fragment 生命周期: 首页 最新文章 在线课程 业界 开发 ...
- Android Fragment完全解析
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8881711 我们都知道,Android上的界面展示都是通过Activity实现的, ...
- Android Fragment完全解析,关于碎片你所需知道的一切
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8881711 我们都知道,Android上的界面展示都是通过Activity实现的, ...
- Android Fragment 生命周期及其API使用(建议使用自定义View替换Fragment)
我为什么不主张使用Fragment Fragment:( Fragment就相当于一个有生命周期的View,它的生命周期被所在的Activity的生命周期管理 ) 生命周期回调说明: onAttach ...
随机推荐
- Could not roll back JDBC transaction途径
[异常]接口数量:DM02;错误代码:ERR_EAI_02_014; 错误叙述性说明:当将中间库异常Could not roll back JDBC transaction; nested excep ...
- 写作Openwrt固件
启动tftp软体.并设置文件夹的固件文件(Current Dircctory)和serverIP(Service interface).server指PC机.图.: ...
- Scrum总结
Scrum总结一个轻量级的软件开发方法 Scrum是一个敏捷开发框架,是一个增量迭代的开发过程..在这个框架整个开发周期由若干个小的跌代周期,每个小的的跌代周期称为一个Sprint,每个Sprint的 ...
- EXCEL Pivot table manipulate
Add filter For the Demo time,I would like to filter out the products which not in Red and Black colo ...
- java线程API学习 线程池ThreadPoolExecutor(转)
线程池ThreadPoolExecutor继承自ExecutorService.是jdk1.5加入的新特性,将提交执行的任务在内部线程池中的可用线程中执行. 构造函数 ThreadPoolExecut ...
- Git & Github 一页简明笔记(转)main
由于小组工程需要使用git&github的版本控制来协作,但我对其使用并不熟悉,特此写篇一页的笔记放在手边,备随时查阅. 使用方法:常用命令供随时查阅,其余内容供新手了解. 0. 常用命令一览 ...
- WinForm LED循环显示信息,使用定时器Threading.Timer
原文:WinForm LED循环显示信息,使用定时器Threading.Timer 这里用一个示例来演示timer如何使用.示例:LED屏幕显示描述:这个示例其实很简单,LED屏幕上显示3个信息: ...
- Loadrunner11.00破解方法
1.下载 从http://www.jb51.net/softs/71240.html上面下载到loadrunner 11.0 2.安装loadrunner 11.0 下载之后,我是直接解压到硬盘,再安 ...
- NSOJ 一个人的旅行(图论)
虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景……草儿 ...
- js 正则学习小记之匹配字符串优化篇
原文:js 正则学习小记之匹配字符串优化篇 昨天在<js 正则学习小记之匹配字符串>谈到 个字符,除了第一个 个,只有 个转义( 个字符),所以 次,只有 次成功.这 次匹配失败,需要回溯 ...