用途:在一个Activity里切换界面,切换界面时只切换Fragment里面的内容
生命周期方法跟Activity一致,可以理解把其为就是一个Activity
定义布局文件作为Fragment的显示内容
//此方法返回的View就会被显示在Fragment上 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub //用布局文件填充成一个View对象,返回出去,那么就显示在Fragment上了 View v = inflater.inflate(R.layout.fragment01, null); return v; }把Fragment显示至指定ViewGroup中
//把fragment显示至界面 //new出fragment对象 Fragment01 fg = new Fragment01(); FragmentManager fm = getFragmentManager(); //开启事务 FragmentTransaction ft = fm.beginTransaction(); //把fragment对象显示到指定资源id的组件里面 ft.replace(R.id.fl, fg); ft.commit();一张张图片不断的切换,形成动画效果
在drawable目录下定义xml文件,子节点为animation-list,在这里定义要显示的图片和每张图片的显示时长
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/g1" android:duration="200" /> <item android:drawable="@drawable/g2" android:duration="200" /> <item android:drawable="@drawable/g3" android:duration="200" /> </animation-list>在屏幕上播放帧动画
ImageView iv = (ImageView) findViewById(R.id.iv); //把动画文件设置为imageView的背景 iv.setBackgroundResource(R.drawable.animations); AnimationDrawable ad = (AnimationDrawable) iv.getBackground(); //播放动画 ad.start();参数10指的是X的起点坐标,但不是指屏幕x坐标为10的位置,而是imageview的 真实X + 10
参数150指的是X的终点坐标,它的值是imageview的 真实X + 150
//创建为位移动画对象,设置动画的初始位置和结束位置 TranslateAnimation ta = new TranslateAnimation(10, 150, 20, 140);x坐标的起点位置,如果相对于自己,传0.5f,那么起点坐标就是 真实X + 0.5 * iv宽度
x坐标的终点位置,如果传入2,那么终点坐标就是 真实X + 2 * iv的宽度
y坐标的起点位置,如果传入0.5f,那么起点坐标就是 真实Y + 0.5 * iv高度
y坐标的终点位置,如果传入2,那么终点坐标就是 真实Y + 2 * iv高度
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2);动画播放相关的设置
//设置动画持续时间 ta.setDuration(2000); //动画重复播放的次数 ta.setRepeatCount(1); //动画重复播放的模式 ta.setRepeatMode(Animation.REVERSE); //动画播放完毕后,组件停留在动画结束的位置上 ta.setFillAfter(true); //播放动画 iv.startAnimation(ta);参数0.1f表示动画的起始宽度是真实宽度的0.1倍
参数4表示动画的结束宽度是真实宽度的4倍
缩放的中心点在iv左上角
ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4);参数0.1f和4意义与上面相同
改变缩放的中心点:传入的两个0.5f,类型都是相对于自己,这两个参数改变了缩放的中心点
中心点x坐标 = 真实X + 0.5 * iv宽度
中心点Y坐标 = 真实Y + 0.5 * iv高度
ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
0为完全透明,1为完全不透明
AlphaAnimation aa = new AlphaAnimation(0, 0.5f);20表示动画开始时的iv的角度
360表示动画结束时iv的角度
默认旋转的圆心在iv左上角
RotateAnimation ra = new RotateAnimation(20, 360);20,360的意义和上面一样
指定圆心坐标,相对于自己,值传入0.5,那么圆心的x坐标:真实X + iv宽度 * 0.5
圆心的Y坐标:真实Y + iv高度 * 0.5
RotateAnimation ra = new RotateAnimation(20, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);第一个参数target指定要显示动画的组件
第二个参数propertyName指定要改变组件的哪个属性
第三个参数values是可变参数,就是赋予属性的新的值
传入0,代表x起始坐标:当前x + 0
传入100,代表x终点坐标:当前x + 100
//具有get、set方法的成员变量就称为属性 ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 100) ;第三个参数指定缩放的比例
0.1是从原本高度的十分之一开始
2是到原本高度的2倍结束
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "scaleY", 0.1f, 2);透明度,0是完全透明,1是完全不透明
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "alpha", 0.1f, 1);rotation指定是顺时针旋转
20是起始角度
270是结束角度
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotation", 20, 270);属性指定为rotationX是竖直翻转
属性指定为rotationY是水平翻转
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotationY", 20, 180);第三个参数可变参数可以传入多个参数,可以实现往回位移(旋转、缩放、透明)
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 70, 30, 100) ;