Android
来源:互联网 发布:mac下载的软件在哪里 编辑:程序博客网 时间:2024/06/05 09:34
1.什么是Fragment
片段,碎片
* 从Android 3.0 (API level 11)开始引入Fragment的。
* Fragment包含在Activity中,Fragment只能存在于Activity的上下文(context)内,没有Activity就无法使用Fragment,因此Fragment只能在Activity的上下文(context)创建。Fragment可以作为Activity的一部分,Fragment和Activity非常相似,Fragment拥有一个与Activity相关的视图层次结构,拥有一个与Activity非常相似的生命周期。
2.为什么要使用Fragment
Activity的使用局限:不能将多个Activity界面放在屏幕上一并显示。因此创建了Fragment来弥补Activity的局限。Fragment可以像Activity一样响应Back键等类似Activity的功能。
3.创建Fragment
public class SoundFragment extends Fragment { //返回当前fragment显示的内容 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.sound, null); }}
4.展示一个Fragment到界面上
public void showSound(View view){ //创建Fragment SoundFragment fragment = new SoundFragment(); //获取Fragment管理器 FragmentManager fm = getFragmentManager(); //开启事务 FragmentTransaction ft = fm.beginTransaction(); //将界面上的一块布局替换为Fragment ft.replace(R.id.container, fragment); //提交事物 ft.commit();}
5.Fragment事物的特点
fm.beginTransaction()和ft.commit()要配对使用
6.Fragment和Activity的通信
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();
Activity中获取Fragment中的数据
- Activity在创建Fragment时可以得到他的引用,利用引用就可以获取里面的内容
创建Fragment时还可以使用replace的另一个方法,给Fragment设置一个tag,以后可以通过tag再次获取这个Fragment
fragmentTransaction.replace(int containerViewId, Fragment fragment, String tag);fragmentManager.findFragmentByTag(String tag);
7.Fragment的生命周期
与Activity类似,在onCreateView()方法返回该Fragment的View
8.在兼容低版本的开发中如何使用Fragment
- Activity要继承FragmentActivity
- 使用getSupportFragmentManager()方法拿到FragmentManager
- 使用android.support.v4.app.Fragment下的Fragment
9.View动画
透明度渐变动画
AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);//动画播放的时间长度aa.setDuration(2000);//设置重复播放的次数aa.setRepeatCount(Animation.INFINITE);//设置重复播放的模式aa.setRepeatMode(Animation.REVERSE);//让iv播放aa动画iv.startAnimation(aa);
平移动画
//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, //结束x值 Animation.RELATIVE_TO_SELF, //起始y类型 0, //起始y值 Animation.RELATIVE_TO_SELF, //结束y类型 1f); //结束y值//动画播放的时间长度ta.setDuration(2000);//设置重复播放的次数ta.setRepeatCount(Animation.INFINITE);//设置重复播放的模式ta.setRepeatMode(Animation.REVERSE);//让iv播放aa动画iv.startAnimation(ta);
缩放动画
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);
旋转动画
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);
10.组合动画的使用方式
AnimationSet set = new AnimationSet(false);...set.addAnimation(ta);set.addAnimation(sa);set.addAnimation(ra);iv.startAnimation(set);
11.使用xml文件描述动画
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);
12.View动画的特点
View的位置并没有真正的变化,只是改变了渲染的位置
13.属性
生成了get、set方法的成员变量就称为属性
14.属性动画的使用方式
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();
15.属性动画的特点
View的位置真正的发生了变化
16.属性动画集合的使用方式
AnimatorSet set = new AnimatorSet();//顺序播放set.playSequentially(oa,oa2);//同时播放set.playTogether(oa,oa2);
17.使用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();
18.总结
- Fragment
- 什么是Fragment
- Fragment的显示
- Fragment与Activity的通信
- Fragment的生命周期
- 不同版本中如何使用Fragment
- View动画
- 平移、缩放、旋转、透明度渐变动画
- 组合动画
- xml文件定义动画
- 属性(Property)动画
- 属性动画的使用
- 属性动画的组合播放
- xml文件定义属性动画
- View动画和属性动画的区别
- 应用程序的反编译
- Android
- android
- Android
- android
- android
- Android
- Android
- android
- android
- android
- Android
- Android
- android!!!
- android
- android
- android
- android
- android:
- Swing图形界面之文件对话框
- 笔试题while((i+1) == (i+1)){}引发的思考
- Map按照key和value进行排序
- WWDC 2017 简要总结
- RestTemplate getForObject方法不能将获取到的数据设置到对象中
- Android
- 原来wireshark还有这功能Statistics -->Conversations
- 串口程序中函数使用WaitCommEvent、ClearCommError、GetcommMask
- 如何把URL传过来的参数进行解析
- 【深度学习笔记001 深度学习入门导读】
- I/O多路转接之select
- layer弹出层显示在top顶层
- Android出现W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection闪退
- dup2重定向