【0051】Android基础-39-fragment相关及零碎的知识点
【1】注意事项
【1】所有的控件都是继承与View类;
【2】所有的布局都是继承与ViewGroup类;
【2】fragment入门
【2.1】fragment的认识
fragment是Activity的一部分

【1】fragment表示Activity中的行为或用户界面的一部分。 【2】您可以在单个Activity中组合多个fragment来构建多个窗格的UI,并在多个Activity中重复使用一个fragment。
【3】你可以把一个fragment想象成一个Activity的模块化部分,它有自己的生命周期,接收自己的输入事件以及在Activity运行时可以添加或删除的Activity(有点像一个“子活动”,你可以重用在不同的Activity)。
【4】fragment必须始终嵌入到Activity中,fragment的生命周期直接受到宿主Activity生命周期的影响。 例如,当Activity暂停时,其中的所有fragment也是如此,当Activity被破坏时,所有fragment也是如此。
【5】将fragment作为Activity布局的一部分添加时,它位于Activity视图层次结构内的ViewGroup中,fragment定义了其自己的视图布局。 您可以通过将Activity的布局文件中的
fragment声明为<fragment>元素,或者通过将其添加到现有的ViewGroup中,从应用程序代码中将fragment插入到Activity布局中。
【6】Android在Android 3.0(API级别11)中引入了fragment,主要用于在平板电脑等大屏幕上支持更加动态和灵活的UI设计。
由于平板电脑的屏幕比手机的屏幕大得多,所以有更多的空间来组合和交换UI组件。

例如,新闻应用程序可以使用一个fragment来显示左侧的文章列表,另一个fragment可以显示右侧的文章 - 两个fragment并排显示在一个Activity中,
每个fragment都有自己的一组生命周期 回调方法和处理自己的用户输入事件。 因此,用户可以选择一篇文章并在相同的Activity中读取所有内容,
而不是使用一个Activity来选择文章和另一个阅读文章的Activity,如图1中的平板电脑布局所示。
【Fragment的创建】

onCreateView()
当fragment第一次绘制其用户界面时,系统会调用它。 为了为你的fragment绘制一个UI,你必须从这个方法返回一个View,它是你的fragment布局的根。
如果片段不提供UI,则可以返回null。

【Fragment的清单文件的配置】

【2.2】【实例】自己实现一个fragment
【说明】在实际的开发中不使用这种方法
【2.2.1】在清单中配置一个fragment

【2.2.2】fragment类的创建
【报错原因】当前APP的API版本过低;

【解决办法】

【2.2.3】fragment布局(fragment布局转换为View)

写了两个fragment.xml布局文件

【2.2.4】使用打气筒将布局文件转换为view对象
需要将两个fragment都进行转化;


【2.2.5】修改activity_main中的fragment的名字

【效果演示】

【2.2.6】总结:使用布局方法进行fragment的书写步骤:

[3]name属性 要指定我们自己定义的fragment
【3】【实例】创建fragment的第2中方法:动态替换fragment
【实例】根据手机的横屏还是竖屏,显示不同的fragment;
【事务】执行一段逻辑 要么同时成功 要么同时失败 经典案例:银行转账

【3.1】 首先判断是横屏还是竖屏

【3.2】动态添加fragment

【fragment1/2.xml源码】
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:text="我是竖屏的内容" 11 android:textColor="#ff0000" 12 android:textSize="20sp" /> 13 14 </LinearLayout>
【Fragment1/2.java源码】
1 package com.itheima.dyncaddfragment; 2 3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 9 //定义一个Fragment 10 public class Fragment1 extends Fragment { 11 12 // 当用户第一次画ui的时候调用 要显示Fragment自己的内容 setContentView(R.layout.activity_main); 13 @Override 14 public View onCreateView(LayoutInflater inflater, ViewGroup container, 15 Bundle savedInstanceState) { 16 // [1]通过打气筒把一个布局转换成view对象 17 View view = inflater.inflate(R.layout.fragment1, null); 18 19 return view; 20 } 21 }
【源码】
1 package com.itheima.dyncaddfragment; 2 3 import android.os.Bundle; 4 import android.annotation.SuppressLint; 5 import android.app.Activity; 6 import android.app.FragmentManager; 7 import android.app.FragmentTransaction; 8 import android.view.Menu; 9 import android.view.WindowManager; 10 11 @SuppressLint("CommitTransaction") 12 public class MainActivity extends Activity { 13 14 @SuppressWarnings("deprecation") 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 20 // [1]获取手机的宽和高 windommanager 21 WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); 22 int width = wm.getDefaultDisplay().getWidth(); 23 int height = wm.getDefaultDisplay().getHeight(); 24 // [2]判断横竖屏 25 26 // [3.1]获取fragment的管理者 27 FragmentManager manager = getFragmentManager(); 28 // [3.2]开启一个事务 29 FragmentTransaction transaction = manager.beginTransaction(); 30 31 if (height > width) { 32 // 说明是竖屏 androind 代表系统定义好的 android.R.id.content理解成是当前手机的窗体 33 transaction.replace(android.R.id.content, new Fragment1()); 34 35 } else { 36 // 横屏 37 transaction.replace(android.R.id.content, new Fragment2()); 38 } 39 40 //[4]一定要记得 提交commit 41 transaction.commit(); 42 43 44 } 45 46 }
【3.3】最后的效果

【4】模拟微信主页面完成
【4.1】主页页面布局

1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context=".MainActivity" > 6 7 <LinearLayout 8 android:id="@+id/ll" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" 11 android:orientation="vertical" > 12 </LinearLayout> 13 14 <LinearLayout 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:layout_alignParentBottom="true" 18 android:orientation="horizontal" > 19 20 <Button 21 android:id="@+id/btn_wx" 22 android:layout_width="0dp" 23 android:layout_height="wrap_content" 24 android:layout_weight="1" 25 android:text="微信" /> 26 27 <Button 28 android:id="@+id/btn_contact" 29 android:layout_width="0dp" 30 android:layout_height="wrap_content" 31 android:layout_weight="1" 32 android:text="联系人" /> 33 34 <Button 35 android:id="@+id/btn_discover" 36 android:layout_width="0dp" 37 android:layout_height="wrap_content" 38 android:layout_weight="1" 39 android:text="发现" /> 40 41 <Button 42 android:id="@+id/btn_me" 43 android:layout_width="0dp" 44 android:layout_height="wrap_content" 45 android:layout_weight="1" 46 android:text="我" /> 47 </LinearLayout> 48 49 </RelativeLayout>
【4.2】其它布局

【4.3】将布局转换为View

【函数解释】
1 View view = inflater.inflate(R.layout.fragment_wx, null);
【第2个参数是ViewGroup root】如果填写第2个参数,则会将转化出来的View直接添加到ViewGroup中,一般不会直接添加,因此写为null;
【4.4】核心转化代码
【MainActivity.java】源码
【主要功能】完成点击下面的4个按钮时的不同的fragment的切换;
1 package com.itheima.wx; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.app.FragmentManager; 6 import android.app.FragmentTransaction; 7 import android.view.Menu; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 12 public class MainActivity extends Activity implements OnClickListener { 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 19 // [1]找到我们关心的控件 20 Button btn_contact = (Button) findViewById(R.id.btn_contact); 21 Button btn_discover = (Button) findViewById(R.id.btn_discover); 22 Button btn_me = (Button) findViewById(R.id.btn_me); 23 Button btn_wx = (Button) findViewById(R.id.btn_wx); 24 25 // [2]给按钮设置点击事件 26 btn_contact.setOnClickListener(this); 27 btn_discover.setOnClickListener(this); 28 btn_me.setOnClickListener(this); 29 btn_wx.setOnClickListener(this); 30 31 } 32 33 @Override 34 public void onClick(View v) { 35 36 // [3]具体判断一下我点击的是哪个按钮 37 38 //[4]获取fragment的管理者 39 FragmentManager fragmentManager = getFragmentManager(); 40 //[5]开启事物 41 FragmentTransaction transaction = fragmentManager.beginTransaction(); 42 43 switch (v.getId()) { 44 case R.id.btn_wx: // 说明点击了微信按钮 45 transaction.replace(R.id.ll, new WxFragment()); 46 break; 47 48 case R.id.btn_contact: // 说明点击了微信按钮 49 transaction.replace(R.id.ll, new ContactFragment()); 50 break; 51 52 case R.id.btn_discover: // 说明点击了微信按钮 53 transaction.replace(R.id.ll, new DiscoverFragment()); 54 break; 55 56 case R.id.btn_me: // 说明点击了微信按钮 57 transaction.replace(R.id.ll, new MeFragment()); 58 break; 59 } 60 //[6]最后一布 记得 提交事物 61 transaction.commit(); 62 63 } 64 65 }
【5】Fragment兼容低版本的写法
【5.1】出现的问题:在低版本上运行上面写的程序会出现下面的bug;本节主要是解决这个BUG;

【5.2】更改方法:不使用APP下的fragment,而使用v4包下的fragment,此包下的fragment兼容低版本的运行;


【解决办法】

【MainActivity.java源码】
1 package com.itheima.dyncaddfragment; 2 3 import android.os.Bundle; 4 import android.annotation.SuppressLint; 5 import android.support.v4.app.FragmentActivity; 6 import android.support.v4.app.FragmentManager; 7 import android.support.v4.app.FragmentTransaction; 8 import android.view.WindowManager; 9 10 @SuppressLint("CommitTransaction") 11 public class MainActivity extends FragmentActivity { 12 13 @SuppressWarnings("deprecation") 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 19 // [1]获取手机的宽和高 windommanager 20 WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); 21 int width = wm.getDefaultDisplay().getWidth(); 22 int height = wm.getDefaultDisplay().getHeight(); 23 // [2]判断横竖屏 24 25 // [3.1]如果使用v4包中的fragment 获取fragment的管理者 是通过getsupportFragmentManager(); 26 FragmentManager supportFragmentManager = getSupportFragmentManager(); 27 28 29 // [3.2]开启一个事务 30 FragmentTransaction transaction = supportFragmentManager.beginTransaction(); 31 32 if (height > width) { 33 // 说明是竖屏 androind 代表系统定义好的 android.R.id.content理解成是当前手机的窗体 34 transaction.replace(android.R.id.content, new Fragment1()); 35 36 } else { 37 // 横屏 38 transaction.replace(android.R.id.content, new Fragment2()); 39 } 40 41 //[4]一定要记得 提交commit 42 transaction.commit(); 43 44 45 } 46 47 }
【Fragment1/2.java源码】
1 package com.itheima.dyncaddfragment; 2 3 import android.os.Bundle; 4 import android.support.v4.app.Fragment; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 9 //定义一个Fragment 10 public class Fragment1 extends Fragment { 11 12 //当用户第一次画ui的时候调用 要显示Fragment自己的内容 setContentView(R.layout.activity_main); 13 @Override 14 public View onCreateView(LayoutInflater inflater, ViewGroup container, 15 Bundle savedInstanceState) { 16 //[1]通过打气筒把一个布局转换成view对象 17 View view = inflater.inflate(R.layout.fragment1, null); 18 19 20 return view; 21 } 22 }
【6】fragment的生命周期
【研究方法】只研究fragment相对于Activity新增的方法,重写新增的方法,然后打印log;



【实际的使用】在公司中,必须书写的方法onCreateView()方法;做一些擦屁股的操作时,需要重写onDestroy()(一般写这个方法的居多)或者onDetch()方法;
【源码】
1 package com.itheima.wx; 2 3 import com.itheima.life.R; 4 5 import android.app.Activity; 6 import android.app.Fragment; 7 import android.os.Bundle; 8 import android.view.LayoutInflater; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.view.ViewGroup; 12 13 public class DiscoverFragment extends Fragment { 14 15 16 //依附在activity上 17 @Override 18 public void onAttach(Activity activity) { 19 System.out.println("onAttach"); 20 super.onAttach(activity); 21 } 22 23 @Override 24 public void onCreate(Bundle savedInstanceState) { 25 System.out.println("onCreate"); 26 super.onCreate(savedInstanceState); 27 } 28 29 //Fragment 加载一个布局 显示Fragment的内容 30 @Override 31 public View onCreateView(LayoutInflater inflater, ViewGroup container, 32 Bundle savedInstanceState) { 33 View view = inflater.inflate(R.layout.fragment_discover, null); 34 System.out.println("onCreateView"); 35 return view; 36 } 37 38 //在这个onCreateView方法中初始化的view 完全初始化 39 @Override 40 public void onActivityCreated(Bundle savedInstanceState) { 41 System.out.println("onActivityCreated"); 42 super.onActivityCreated(savedInstanceState); 43 } 44 45 @Override 46 public void onStart() { 47 System.out.println("onStart"); 48 super.onStart(); 49 } 50 51 @Override 52 public void onResume() { 53 System.out.println("onResume"); 54 super.onResume(); 55 } 56 57 @Override 58 public void onPause() { 59 System.out.println("onPause"); 60 super.onPause(); 61 } 62 63 //当界面不可见 64 @Override 65 public void onStop() { 66 System.out.println("onStop"); 67 super.onStop(); 68 } 69 70 //在oncreateView方法里面初始化的view销毁了 71 @Override 72 public void onDestroyView() { 73 System.out.println("onDestroyView"); 74 super.onDestroyView(); 75 } 76 @Override 77 public void onDestroy() { 78 System.out.println("onDestroy"); 79 super.onDestroy(); 80 } 81 82 //取消依附 83 @Override 84 public void onDetach() { 85 86 System.out.println("onDetach"); 87 super.onDetach(); 88 } 89 90 91 92 }
【7】fragment之间的通信
【功能】在一个Activity中显示左右两个fragment,左边的是一个按钮,右边的是一个textView;点击按钮改变右边的textView中的文字信息;
【说明】Fragment有一个公共的桥梁 Activity
【7.1】布局


【activity_main.xml】源码
【说明】左右两个线性布局
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal" 6 tools:context=".MainActivity" > 7 8 <LinearLayout 9 android:id="@+id/ll1" 10 android:layout_width="0dp" 11 android:layout_height="match_parent" 12 android:layout_weight="1" > 13 </LinearLayout> 14 15 <LinearLayout 16 android:id="@+id/ll2" 17 android:layout_width="0dp" 18 android:layout_height="match_parent" 19 android:layout_weight="1" > 20 </LinearLayout> 21 22 </LinearLayout>
【fragment1.xml】源码:布局一个按钮
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:id="@+id/button1" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="修改" /> 12 13 </LinearLayout>
【fragment2.xml】源码:布局一个TextView,写着helloWorld;
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/tv" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="helloworld" /> 12 13 </LinearLayout>
【7.2】控件替换

【fragment1.java】源码
1 package com.itheima.communication; 2 3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.view.ViewGroup; 9 import android.widget.Toast; 10 11 public class Fragment1 extends Fragment { 12 13 @Override 14 public View onCreateView(LayoutInflater inflater, ViewGroup container, 15 Bundle savedInstanceState) { 16 View view = inflater.inflate(R.layout.fragment1, null); 17 return view; 18 } 19 }
【fragment2.java】源码
1 package com.itheima.communication; 2 3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.TextView; 9 10 public class Fragment2 extends Fragment { 11 12 private TextView tView; 13 14 15 @Override 16 public View onCreateView(LayoutInflater inflater, ViewGroup container, 17 Bundle savedInstanceState) { 18 View view = inflater.inflate(R.layout.fragment2, null); 19 return view; 20 } 21 22 }
【7.3】按钮点击之后文本替换
【注意】



【出错原因】new fragment2的时候,tView指针为空;原因是没有执行方法onCreateView,没有拿到控件TextView;

【核心代码】【原理】在fragment1中获取fragment2的控件,然后调用fragment2中的自定的设置text的方法

【注意】在动态替换的时候使用了replace的第三个参数,专门定义了一个名称;用于在fragment1中查找
【源码】
1 package com.itheima.communication; 2 3 import android.os.Bundle; 4 import android.annotation.SuppressLint; 5 import android.app.Activity; 6 import android.app.FragmentManager; 7 import android.app.FragmentTransaction; 8 import android.view.Menu; 9 10 @SuppressLint("CommitTransaction") 11 public class MainActivity extends Activity { 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 18 // [1]获取Fragment的管理者 19 FragmentManager fragmentManager = getFragmentManager(); 20 // [2]开启事物 21 FragmentTransaction transaction = fragmentManager.beginTransaction(); 22 // [3]动态替换 23 transaction.replace(R.id.ll1, new Fragment1(), "f1"); 24 transaction.replace(R.id.ll2, new Fragment2(), "f2"); 25 26 // [4]最后一步 记得commit 27 transaction.commit(); 28 29 } 30 31 }
【实际的效果如下】

【8】在fragment中弹吐司:注意context的获取,使用getActivity方法;

【9】menu菜单介绍
【说明】共有两种方式添加菜单
【9.1】第一种添加菜单的方式
【说明】添加菜单方式 通过一个布局 在res下 meun目录下创建一个布局



【修改】增加条目
【源码】

【参数说明】
【orderInCategory】:显示的优先级;
【id】用于查找该item;
【效果】

【源码】【得知获取了哪个菜单】

【9.2】第二种添加菜单的方式-动态的添加


【9.3】在应用点击menu按键显示对话框;现在在公司很少这么干了


【10】autocompletetextview控件介绍

【更改属性】只输入一个字母进行提示


【源码】
【/8_autocompletetextview/res/layout/activity_main.xml】源码
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context=".MainActivity" > 6 7 <AutoCompleteTextView 8 android:id="@+id/actv" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:completionThreshold="1" /> 12 13 </RelativeLayout>
【/8_autocompletetextview/src/com/itheima/autocompletetextview/MainActivity.java】源码
1 package com.itheima.autocompletetextview; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.view.Menu; 6 import android.view.View; 7 import android.widget.ArrayAdapter; 8 import android.widget.AutoCompleteTextView; 9 10 public class MainActivity extends Activity { 11 12 // [0]声明AutoCompleteTextView要显示的数据 13 private static final String[] COUNTRIES = new String[] { "laofang", 14 "laozhang", "laoli", "laobi", "laoli", "laowang", "aab", "abb", 15 "cc" }; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 // [1]找到控件 22 AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.actv); 23 24 // [2]创建数据适配器 25 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 26 android.R.layout.simple_dropdown_item_1line, COUNTRIES); 27 28 // [3]设置数据适配器 29 actv.setAdapter(adapter); 30 } 31 32 }
【11】补间动画
【11.1】View Animation 【tweened 动画】

11.2 布局

1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context=".MainActivity" > 6 7 <LinearLayout 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:orientation="horizontal" > 11 12 <Button 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:onClick="click1" 16 android:text="透明" /> 17 18 <Button 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:onClick="click2" 22 android:text="旋转" /> 23 24 <Button 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:onClick="click3" 28 android:text="缩放" /> 29 30 <Button 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:onClick="click4" 34 android:text="位移" /> 35 36 <Button 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:onClick="click5" 40 android:text="一起飞" /> 41 </LinearLayout> 42 43 <ImageView 44 android:id="@+id/iv" 45 android:layout_width="wrap_content" 46 android:layout_height="wrap_content" 47 android:layout_centerInParent="true" 48 android:src="@drawable/ic_launcher" /> 49 50 </RelativeLayout>
11.3【透明】



11.4【旋转】


【修改旋转的中心点】


11.5【缩放】

11.6【位移】

1 //位移动画 2 public void click4(View v){ 3 //此处的0.2f:相对于父窗体的高度的0.2(如果大于1,则会移除到父窗体中) 4 TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.2f); 5 ta.setDuration(2000); //设置动画执行的时间 6 ta.setFillAfter(true);//当动画结束后 动画停留在结束位置 7 8 //开始动画 9 iv.startAnimation(ta); 10 }
11.7【补间动画的特点】总结: 动画效果不会改变控件真实的坐标

【11.8】多种动画一起执行

此值在此处没有什么变化:true 和 false;

【源码】
1 //动画一起飞 2 public void click5(View v){ 3 AnimationSet set = new AnimationSet(false); 4 5 //透明动画 6 AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f); 7 aa.setDuration(2000); //设置动画执行的时间 8 aa.setRepeatCount(1); //设置重复的次数 9 aa.setRepeatMode(Animation.REVERSE);//设置动画执行的模式 10 //旋转动画 11 RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 12 ra.setDuration(2000); //设置动画执行的时间 13 ra.setRepeatCount(1); //设置重复的次数 14 ra.setRepeatMode(Animation.REVERSE);//设置动画执行的模式 15 //缩放 16 ScaleAnimation sa = new ScaleAnimation(1.0f,2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 17 sa.setDuration(2000); //设置动画执行的时间 18 sa.setRepeatCount(1); //设置重复的次数 19 sa.setRepeatMode(Animation.REVERSE);//设置动画执行的模式 20 21 TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.2f); 22 ta.setDuration(2000); //设置动画执行的时间 23 ta.setFillAfter(true);//当动画结束后 动画停留在结束位置 24 25 //添加动画 26 set.addAnimation(aa); 27 set.addAnimation(ra); 28 set.addAnimation(sa); 29 set.addAnimation(ta); 30 31 //最后一步 要记得 执行动画 32 iv.startAnimation(set); 33 34 35 }
【12】应用程序的反编译
【12.1】简单介绍



【12.2】逆向助手的使用

【12.3】逆向助手查看源码

【源码的阅读】

【12.4】反编译上架的APP


【anim】360动画的资源

【查看源码】

【12.5】【说明】
【1】一般上架的app存在代码混淆;很少查看源码;
【2】使用逆向助手一般使用查看xml的布局文件;
【13】使用xml方式定义补间动画
【13.1】透明效果




【13.2】旋转效果



【修改为基于中心点】


【13.3】缩放的效果
【注意】【参数】此处的参数toXScale 需要填写大于1的数值,与代码实现的数值不同,此处为2,就是代码中的0.2;以此类推;



【13.4】位移的效果



【13.5】一起飞的效果

1 <?xml version="1.0" encoding="utf-8"?> 2 <set> 3 4 <alpha 5 xmlns:android="http://schemas.android.com/apk/res/android" 6 android:duration="2000" 7 android:fromAlpha="1.0" 8 android:repeatCount="1" 9 android:repeatMode="reverse" 10 android:toAlpha="0.0" > 11 </alpha> 12 13 <rotate 14 xmlns:android="http://schemas.android.com/apk/res/android" 15 android:duration="2000" 16 android:fromDegrees="0" 17 android:pivotX="50%" 18 android:pivotY="50%" 19 android:repeatCount="1" 20 android:repeatMode="reverse" 21 android:toDegrees="360" > 22 </rotate> 23 24 <scale 25 xmlns:android="http://schemas.android.com/apk/res/android" 26 android:duration="2000" 27 android:fromXScale="1.0" 28 android:fromYScale="1.0" 29 android:pivotX="50%" 30 android:pivotY="50%" 31 android:repeatCount="1" 32 android:repeatMode="reverse" 33 android:toXScale="2.0" 34 android:toYScale="2.0" > 35 </scale> 36 37 <translate 38 xmlns:android="http://schemas.android.com/apk/res/android" 39 android:duration="2000" 40 android:fillAfter="true" 41 android:fromXDelta="0%p" 42 android:fromYDelta="0%p" 43 android:toXDelta="0%p" 44 android:toYDelta="20%p" > 45 </translate> 46 47 </set>


【14】属性动画 (简单认识)
【14.1】如何查看一个陌生的项目?
【1】首先查看AndroidManifest.xml;找到intent-filter,找到程序的主入口(必须是查看intent-filter,而不是MainActivity,可能有的程序的主入口不是改名称);

【2】进入主入口,进入onCreate方法,查看加载的布局:activity_main;

【3】进入之后先查看界面的效果

【注意】查看界面的预览布局

【14.2】属性动画的实现


【14.3】xml实现属性动画
【文件夹的创建】之前创建的文件夹的名称叫做anim;现在的名称叫做;animator;


【15】通知栏介绍
【15.1】通知栏的认识
[1]Toast
[2]对话框
[3]通知栏
【15.2】
【15.2.1】NotifacationManager对象的获取及取消按钮功能的实现


【15.2.2】发送通知:高版本的写法



【增加点击跳转的功能】

【15.2.3】发送通知:低版本的写法
1 //点击按钮发送通知 2 public void click1(View v) {1 12 //兼容低版本的写法 用过时的方法 13 Notification noti = new Notification(R.drawable.ic_launcher, "接收到了一条通知", System.currentTimeMillis()); 14 15 //创建意图对象 16 Intent intent = new Intent(); 17 //实现打电话的逻辑 18 intent.setAction(Intent.ACTION_CALL); 19 intent.setData(Uri.parse("tel:"+119)); 20 //需要添加打电话的权限 Task 21 22 PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, Intent.FLAG_ACTIVITY_NEW_TASK); 23 noti.setLatestEventInfo(this, "小芳", "今天晚上7天酒店....", pendingIntent); 24 25 //[2]发送通知 26 nm.notify(10, noti); 27 28 }

【15.3】增加通知灯和振动功能
【注意】需要增加权限

【源码】

【15.4】无法删除通知


【无法删除通知解决的办法】在Android4.0之后,可以长按,然后进入到设置将通知取消掉;
但是,这样之后的通知将无法收到; 如果还想收到通知,需要返回该应用的设置界面勾选上接收通知;

【16】服务和通知的绑定,能提升当前进程的优先级,常常存在于qq、音乐通知等等;

浙公网安备 33010602011771号