Android基础Fragment以及动画


##什么是Fragment(运行更新一部分ui)
* 从Android 3.0 (API level 11)开始引入Fragment的。
* Fragment包含在Activity中,Fragment只能存在于Activity的上下文(context)内,没有Activity就无法使用Fragment,因此Fragment只能在Activity的上下文(context)创建。Fragment可以作为Activity的一部分,Fragment和Activity非常相似,Fragment拥有一个与Activity相关的视图层次结构,拥有一个与Activity非常相似的生命周期。

##为什么要使用Fragment
Activity的使用局限:不能将多个Activity界面放在屏幕上一并显示。因此创建了Fragment来弥补Activity的局限。Fragment可以像Activity一样响应Back键等类似Activity的功能。

##创建Fragment(一个类继承Fragment即可,在写onCreateView方法)
    public class SoundFragment extends Fragment {
        //返回当前fragment显示的内容
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
                //打气筒返回frgment显示的内容
            return inflater.inflate(R.layout.sound, null);
        }
    }

##展示一个Fragment到界面上
    public void showSound(View view){
        //创建Fragment 可以直接new
        SoundFragment fragment = new SoundFragment();
        //获取Fragment管理器
           FragmentManager fm = getFragmentManager();
           //开启事务(数据库也有事务,避免花屏) 抽取的时候注意,因为事务只对一次有效,因为commit提交了,所以需要每次开启下
           FragmentTransaction ft =  fm.beginTransaction();
        //将界面上的一块布局替换为Fragment(替换)
           ft.replace(R.id.container, fragment);  
        //replace 这个方法有一个特点切换fragment会重新初始化(如果做了改动就保存不到之前的操作的内容了)
        //这里使用到replace的add方法来保存
        //提交事物
           ft.commit();
    }

##Fragment事物的特点
fm.beginTransaction()和ft.commit()要配对使用

##Fragment和Activity的通信
1. Fragment获取Activity中的数据

        //通过getActivity()获取到所在Activity的引用,然后就可以拿到Activity里面的内容了
        EditText et_name = (EditText) getActivity().findViewById(R.id.et_name);
        Toast.makeText(getActivity(), "name:"+et_name.getText().toString(), 0).show();
2. Activity中获取Fragment中的数据
    * Activity在创建Fragment时可以得到他的引用,利用引用就可以获取里面的内容
    * 创建Fragment时还可以使用replace的另一个方法,给Fragment设置一个tag,以后可以通过tag再次获取这个Fragment
                                                                            //设置tag
            fragmentTransaction.replace(int containerViewId, Fragment fragment, String tag);
            //通过tag找到activity
            fragmentManager.findFragmentByTag(String tag);

##Fragment的生命周期
与Activity类似,在onCreateView()方法返回该Fragment的View对象的引用
其他的都是同步的

##在兼容低版本的开发中如何使用Fragment
1. Activity要继承FragmentActivity
//在support4包下的
2. 使用getSupportFragmentManager()方法拿到FragmentManager
3. 使用android.support.v4.app.Fragment下的Fragment

##View动画
1. 透明度渐变动画    
        //透明度的api                        完全透明---完全不透明
        AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
        //动画播放的时间长度
        aa.setDuration(2000);
        //设置重复播放的次数
        aa.setRepeatCount(Animation.INFINITE);   常量为-1,表示无限次播放
        //设置重复播放的模式
        aa.setRepeatMode(Animation.REVERSE);  倒叙播放  RESTART每次从新开始播放
        //让iv播放aa动画
        iv.startAnimation(aa);
2. 平移动画
        
        //Animation.RELATIVE_TO_SELF     相对于自己,值要填百分比
        //Animation.RELATIVE_TO_PARENT     相对于父控件(整个屏幕),值要填百分比
        //Animation.ABSOLUTE             绝对坐标,值要填具体值
        TranslateAnimation ta = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, //起始x类型
                0,                             //起始x值
                Animation.RELATIVE_TO_SELF,    //结束x类型
                1f,    (相对于自己空间宽度的1倍)                    //结束x值
                Animation.RELATIVE_TO_SELF,    //起始y类型
                0,                            //起始y值
                Animation.RELATIVE_TO_SELF,    //结束y类型
                1f);    (相对于自己空间长度的1倍)                            //结束y值
        //动画播放的时间长度
        ta.setDuration(2000);
        //设置重复播放的次数
        ta.setRepeatCount(Animation.INFINITE);
        //设置重复播放的模式
        ta.setRepeatMode(Animation.REVERSE);
        //让iv播放aa动画
        iv.startAnimation(ta);
3. 缩放动画

        ScaleAnimation sa = new ScaleAnimation(
                0.2f,                         //x轴起始缩放比
                2.0f,                         //x轴结束缩放比
                0.2f,                         //y轴起始缩放比
                2.0f,                        //y轴结束缩放比
                Animation.RELATIVE_TO_SELF, //中心点x类型
                0.5f,                         //中心点x值
                Animation.RELATIVE_TO_SELF, //中心点y类型
                0.5f                        //中心点y值
                );
        //动画播放的时间长度
        sa.setDuration(2000);
        //设置重复播放的次数
        sa.setRepeatCount(Animation.INFINITE);
        //设置重复播放的模式
        sa.setRepeatMode(Animation.REVERSE);
        //让iv播放aa动画
        iv.startAnimation(sa);
4. 旋转动画
        默认以自己空间的左上角旋转
        RotateAnimation ra = new RotateAnimation(  
                0,         开始的角度
                360,     结束的角度
                Animation.RELATIVE_TO_SELF,         //旋转的中心点
                0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f);
        //动画播放的时间长度
        ra.setDuration(2000);
        //设置重复播放的次数
        ra.setRepeatCount(Animation.INFINITE);
        //设置重复播放的模式
        ra.setRepeatMode(Animation.REVERSE);
        //让iv播放aa动画
        iv.startAnimation(ra);

##组合动画的使用方式
    AnimationSet set = new AnimationSet(false);各种动画按自己的时间轴播放  true不按照自己的
    ...
    set.addAnimation(ta);
    set.addAnimation(sa);
    set.addAnimation(ra);
    iv.startAnimation(set);

##使用xml文件描述动画    (fillAfert="true"  最后停止的效果为最后停止那一针的画面)
xml文件放在res/anim文件夹下

    <?xml version="1.0" encoding="utf-8"?>
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="2000"
        android:repeatCount="infinite"
        android:repeatMode="reverse" >
    </alpha>

使用方式

    Animation aa = AnimationUtils.loadAnimation(this, R.anim.alpha_demo);
    iv.startAnimation(aa);

##View动画的特点
View的位置并没有真正的变化,只是改变了渲染的位置

##属性
生成了get、set方法的成员变量就称为属性

##属性动画的使用方式                          控件    改变的哪一个属性    可变参数(水平方向的偏移量)
    ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationY", new float[]{0,2,4,6,8,10,15,20,25,30,50,80,100,150});
    oa.setDuration(2000);
    oa.setRepeatCount(ObjectAnimator.INFINITE);
    oa.setRepeatMode(ObjectAnimator.RESTART);
    oa.start();

##属性动画的特点(更改了控件的位置)
View的位置真正的发生了变化

##属性动画集合的使用方式 一旦属性实现了set,get方法就变成了类的成员变量
    AnimatorSet set = new AnimatorSet();
    //顺序播放
    set.playSequentially(oa,oa2);
    //同时播放
    set.playTogether(oa,oa2);

##使用xml文件描述属性动画
xml文件放在res/animator文件夹下

    <?xml version="1.0" encoding="utf-8"?>
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
        android:propertyName="alpha"    
        android:duration="3000"
        android:repeatCount="3"
        android:repeatMode="reverse"
        android:valueFrom="0.0"               
        android:valueTo="1.0" >
    </objectAnimator>

使用方式

    Animator animator =    AnimatorInflater.loadAnimator(this, R.animator.alpha);
    animator.setTarget(iv);
    animator.start();

你可能感兴趣的:(Android基础Fragment以及动画)