1.fragement入门
ViewGroup特点 可以有自己的孩子
View
import android.app.Fragment;
[1]通过onCreateView这个方法Fragment可以加载自己的布局
[2]name属性指定的是一个我们自己定义的Fragment
[3]直接在布局中声明即可
[1]清单文件改成11以上:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
[2]activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/list"
android:name="com.itheima.fragmentdemo.Fragment1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<fragment
android:id="@+id/viewer"
android:name="com.itheima.fragmentdemo.Fragment2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
</LinearLayout>
[3]fragment1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是Fragment1里面的内容"
android:textColor="#ff0000"
/>
</LinearLayout>
[4]fragment2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是Fragment2里面的内容"
android:textColor="#000000"
/>
</LinearLayout>
[5]Fragment1:
//定义Fragment 理解为 是activity的一部分
public class Fragment1 extends Fragment{
//当系统第一次画ui的时候调用 通过这个方法可以让Fragment显示自己的布局内容
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//通过打气筒把一个布局转换成一个view对象
View view = inflater.inflate(R.layout.fragment1, null);
return view;
}
}
[6]Fragment2:
public class Fragment2 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment2, null);
return view;
}
}
2.动态替换fragment
[1]获取Fragment的管理者
[2]开启一个事务
[3]提交事务
[1]清单文件改成11以上:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
[2]fragment1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是竖屏的内容1"
android:textColor="#ff0000"/>
</LinearLayout>
[3]fragment2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是竖屏的内容2"
android:textColor="#000000"/>
</LinearLayout>
[4]Fragment1:
public class Fragment1 extends Fragment{
//显示Fragment1 自己要显示的内容
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, null);
return view;
}
}
[5]Fragment2:
public class Fragment2 extends Fragment{
//显示fragment2 自己要显示内容
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2, null);
return view;
}
}
[6]MainActivity:
public class MainActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//[1]获取手机的分辨率
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
System.out.println("width:"+width+"--"+height);
Point point=new Point();
wm.getDefaultDisplay().getSize(point);
System.out.println("width"+point.x+"---"+point.y);
//[2]判断横竖屏
Configuration mConfiguration = this.getResources().getConfiguration(); //获取设置的配置信息
int ori = mConfiguration.orientation; //获取屏幕方向
System.out.println(ori);
if (ori == mConfiguration.ORIENTATION_LANDSCAPE) {
//横屏
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//强制为竖屏
} else if (ori == mConfiguration.ORIENTATION_PORTRAIT) {
//竖屏
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//强制为横屏
}
//[3]获取Fragment的管理者 通过上下文直接获取
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();//开启事务
if(ori == mConfiguration.ORIENTATION_PORTRAIT){
//说明是竖屏 加载一个Fragment android.R.id.content //代表当前手机的窗体
beginTransaction.replace(android.R.id.content, new Fragment1());
}else {
//说明是横屏 加载一个Fragment
beginTransaction.replace(android.R.id.content, new Fragment2());
}
//[4]最后一步 记得comment
beginTransaction.commit();
}
}
3.使用fragment创建一个选项卡页面
[1]清单文件改成11以上:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
[2]activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/ll_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="@+id/btn_wx"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="微信"
/>
<Button
android:id="@+id/btn_contact"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="通讯录"
/>
<Button
android:id="@+id/btn_disconver"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="发现"
/>
<Button
android:id="@+id/btn_me"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="我"
/>
</LinearLayout>
</RelativeLayout>
[3]fragment_contact.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="我是通讯录模块的内容"/>
</LinearLayout>
[4]fragment_discover.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="我是发现模块的内容"/>
</LinearLayout>
[5]fragment_me.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="我是我的模块的内容"/>
</LinearLayout>
[6]fragment_wx.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="我是微信模块的内容"/>
<Button
android:id="@+id/btn_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试"
/>
</LinearLayout>
[7]ContactFragment:
public class ContactFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_contact, null);
return view;
}
}
[8]DiscoverFragment:
public class DiscoverFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_discover, null);
return view;
}
}
[9]MeFragment:
public class MeFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_me, null);
return view;
}
}
[10]WxFragment:
public class WxFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_wx, null);
//测试按钮如何点击
view.findViewById(R.id.btn_test).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("按钮按下了");
}
});
return view;
}
}
[11]MainActivity:
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//[1]找到按钮
Button btn_wx=(Button) findViewById(R.id.btn_wx);
Button btn_contact=(Button) findViewById(R.id.btn_contact);
Button btn_discover=(Button) findViewById(R.id.btn_disconver);
Button btn_me=(Button) findViewById(R.id.btn_me);
//[2]设置点击事件
btn_wx.setOnClickListener(this);
btn_contact.setOnClickListener(this);
btn_discover.setOnClickListener(this);
btn_me.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//[3]获取Fragment的管理者
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
//[4]具体判断点击的是哪个按钮
switch (v.getId()) {
case R.id.btn_wx://点击的是微信
beginTransaction.replace(R.id.ll_layout, new WxFragment());
break;
case R.id.btn_contact://点击的是联系人
beginTransaction.replace(R.id.ll_layout, new ContactFragment());
break;
case R.id.btn_disconver://点击的是发现
beginTransaction.replace(R.id.ll_layout, new DiscoverFragment());
break;
case R.id.btn_me://点击的是我
beginTransaction.replace(R.id.ll_layout, new MeFragment());
break;
default:
break;
}
//记得comment
beginTransaction.commit();
}
}
4.使用fragment兼容低版本的写法
[1]fragment是在11的时候被引入的
[2]兼容低版本的写法就是所有的Fragment全部使用V4包中的fragment,android.support.v4.app.Fragment
[3]MainActivity继承的不是Activity,而是android.support.v4.app.FragmentActivity
[1]fragment1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是竖屏的内容1"
android:textColor="#ff0000" />
</LinearLayout>
[2]fragment2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是横屏里面的内容2"
android:textColor="#000000" />
</LinearLayout>
[3]Fragment1:
public class Fragment1 extends Fragment{
//显示Fragment1 自己要显示的内容
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment1, null);
return view;
}
}
[4]Fragment2:
public class Fragment2 extends Fragment{
//显示Fragment2 自己要显示的内容
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2, null);
return view;
}
}
[5]MainActivity:
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//[1]获取手机的分辨率
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
//[2]判断横竖屏
//[3]获取Fragment的管理者 通过上下文直接获取
//FragmentManager fragmentManager = getFragmentManager();
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction beginTransaction = supportFragmentManager.beginTransaction(); //开启事物
if(height > width){
//说明是竖屏 加载一个Fragment android.R.id.content //代表当前手机的窗体
beginTransaction.replace(android.R.id.content, new Fragment1());
}else {
//说明是横屏 加载一个Fragment
beginTransaction.replace(android.R.id.content, new Fragment2());
}
//[4]最后一步 记得comment
beginTransaction.commit();
}
}
5.fragment的生命周期
实际开发中要重写一个方法 onCreateView方法
还可以重写onDestory方法 进行一些回收内存的一些操作
开始(依次执行):
onAttach 首先要依附在activity上
onCreate
onCreateView 系统第一次画ui的时候调用
onActivityCreated 当在onCreateView方法里面准备view 初始化了
onStart
onResume
销毁(依次执行):
onPause
onStop
onDestroyView
onDestroy fragment销毁
onDetach 取消依附
6.fragment之间的通信
fragment公共的桥梁-->Activity
//[2]调用fragment2里面的方法, 通过fragment的公共桥梁 --->activity
Fragment2 fragment2 = (Fragment2) getActivity().getFragmentManager().findFragmentByTag("f2");
fragment2.updateText("哈哈哈呵呵");
例子:
[1]清单文件改成11以上:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
[2]activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/ll1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
</LinearLayout>
<LinearLayout
android:id="@+id/ll2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
</LinearLayout>
</LinearLayout>
[3]fragment1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改" />
</LinearLayout>
[4]fragment2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="helloworld"
/>
</LinearLayout>
[5]MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//[1]获取fragment管理者
FragmentManager fragmentManager = getFragmentManager();
//[2]开启一个事物
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
//[2.1]替换fragment
beginTransaction.replace(R.id.ll1, new Fragment1(),"f1");
beginTransaction.replace(R.id.ll2, new Fragment2(),"f2");
//[3]开启事物
beginTransaction.commit();
}
[6]Fragment1:
//定义Fragment 理解为 是Activity的一部分
public class Fragment1 extends Fragment {
//当系统第一次画ui的时候调用 通过这个方法方法可以让Fragment显示自己的布局内容
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//通过打气筒把一个布局转换成一个View对象
View view = inflater.inflate(R.layout.fragment1, null);
//[1]找到按钮 设置点击事件
view.findViewById(R.id.btn_update).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//[2]修改fragment2里面的内容 通过fragment的公共桥梁 --->activity
Fragment2 fragment2 = (Fragment2) getActivity().getFragmentManager().findFragmentByTag("f2");
fragment2.updateText("哈哈哈呵呵");
}
});
return view;
}
}
[7]Fragment2:
public class Fragment2 extends Fragment {
private TextView tv_content;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2, null);
//[1]ÕÒµ½tv
tv_content = (TextView) view.findViewById(R.id.tv_content);
return view;
}
//ÐÞ¸ÄtextviewµÄÄÚÈÝ
public void updateText(String content){
tv_content.setText(content);
}
}
7.menu菜单
可以自定义菜单,也可以不弹出菜单只弹出对话框
[1]MainActivity代码如下:
//可以自定义菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//[1]添加菜单的第一种方式
//getMenuInflater().inflate(R.menu.main, menu);
//[2]通过代码的方式添加
menu.add(0, 1, 0, "前进");
menu.add(0, 2, 0, "后退");
menu.add(0, 3, 0, "首页");
return true;
}
//当我们想指定具体点击的是哪个菜单条目 重写下面这个方法(固定流程)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//具体点击的是哪个菜单条目
switch (item.getItemId()) {
case 1:
System.out.println("前进");
break;
case 2:
System.out.println("后退");
break;
case 3:
System.out.println("首页");
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
//当菜单打开之前调用这个方法
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
//弹出一个对话框
AlertDialog.Builder builder=new Builder(this);
builder.setTitle("警告");
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
//最后一步 记得show 出来 和toast一样
builder.show();
return false;
}
[2]添加菜单的第一种方式/res/menu/main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.itheima.menu.MainActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/action_settings1"
android:orderInCategory="10"
android:title="setting1"
/>
<item
android:id="@+id/action_settings2"
android:orderInCategory="12"
android:title="setting2"
/>
</menu>
[1]声明菜单
[2]动态去添加菜单(代码的方式)
[3]弹出一个对话框
8.AutoCompleteTextView控件的使用
就是和搜索框一样,输入ll,出现下拉框,显示带ll前缀的
completionThreshold多少个字符提示,默认2个
显示数据的原理和listview一样 也需要数据适配器
[1]activity_main.xml:
<AutoCompleteTextView
android:id="@+id/actv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1">
</AutoCompleteTextView>
[2]MainActivity代码如下:
//模拟actv这个控件要显示的数据
private String[] COUNTRIES = new String[] {
"laofang", "laoli", "laozhang", "laobi", "laowang","aa","abb","ccc"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//[1]找到控件
AutoCompleteTextView actv=(AutoCompleteTextView) findViewById(R.id.actv);
//[2]actv这个控件显示数据的原理和listview一样 需要一个数据适配器
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES);
//[3]显示数据
actv.setAdapter(adapter);
}
9.补间动画
view动画
[1]透明 AlphaAnimation
[2]旋转 rotateAnimation
[3]缩放 ScaleAnimation
[4]位移 translateAnimation
原理:动画效果不会改变控件真实的坐标
[1]activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click1"
android:text="透明"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click2"
android:text="旋转"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click3"
android:text="缩放"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click4"
android:text="位移"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click5"
android:text="一起飞"/>
</LinearLayout>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher"/>
</RelativeLayout>
[2]MainActivity:
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//这个控件用来执行动画
iv = (ImageView) findViewById(R.id.iv);
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "点击imageview", 1).show();
}
});
}
//点击按钮 实现透明效果
public void click1(View v){
//创建透明动画 1.0意味着完全不透明 0.0意味着完全透明
AlphaAnimation animation=new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(2000);//设置动画执行的时间
animation.setRepeatCount(1);//设置动画重复的次数
animation.setRepeatMode(Animation.REVERSE);//设置重复的模式
//开始执行动画
iv.startAnimation(animation);
}
//点击按钮 实现旋转效果
public void click2(View v){
//fromDegrees开始角度 toDegrees结束的角度
//RotateAnimation animation=new RotateAnimation(0, 360);
//围着自己中心旋转
RotateAnimation animation=new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(2000);//设置动画执行的时间
animation.setRepeatCount(1);//设置动画重复的次数
animation.setRepeatMode(Animation.REVERSE);//设置重复的模式
//开始执行动画
iv.startAnimation(animation);
}
//点击按钮 实现缩放效果
public void click3(View v){
ScaleAnimation animation=new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(2000);//设置动画执行的时间
animation.setRepeatCount(1);//设置动画重复的次数
animation.setRepeatMode(Animation.REVERSE);//设置重复的模式
//开始执行动画
iv.startAnimation(animation);
}
//点击按钮 实现平移效果
public void click4(View v){
TranslateAnimation animation=new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.2f);
animation.setDuration(2000);//设置动画执行的时间
animation.setRepeatCount(1);//设置动画重复的次数
animation.setRepeatMode(Animation.REVERSE);//设置重复的模式
//开始执行动画
iv.startAnimation(animation);
}
//点击按钮 让动画一起执行
public void click5(View v){
//创建动画的合集
AnimationSet set=new AnimationSet(true);
AlphaAnimation aa=new AlphaAnimation(1.0f, 0.0f);
aa.setDuration(2000);//设置动画执行的时间
aa.setRepeatCount(1);//设置动画重复的次数
aa.setRepeatMode(Animation.REVERSE);//设置重复的模式
RotateAnimation ra=new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
ra.setDuration(2000);//设置动画执行的时间
ra.setRepeatCount(1);//设置动画重复的次数
ra.setRepeatMode(Animation.REVERSE);//设置重复的模式
ScaleAnimation sa=new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
sa.setDuration(2000);//设置动画执行的时间
sa.setRepeatCount(1);//设置动画重复的次数
sa.setRepeatMode(Animation.REVERSE);//设置重复的模式
//添加动画
set.addAnimation(aa);
set.addAnimation(ra);
set.addAnimation(sa);
//执行动画
iv.startAnimation(set);
}
10.应用程序的反编译
Android逆向助手
apktools 可以获取res下资源文件
dex2jar 可以把apk转换成jar
jd-dui.exe 可以直接查看源码
Android逆向助手
[1]选择源文件(apk),选择反编译apk,可以获取res下资源文件
[2]选择源文件(apk),选择dex转jar,可以把apk转换成jar(会自动用jd-dui.exe帮你打开,或者操作第三步)
[3]选择源文件(jar包),选择jd打开jar
11.使用xml方式定义补间动画
[1]就是在res下创建一个目录(anim)
[2]在该目录下创建对应的动画xml即可
[1]activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click1"
android:text="透明"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click2"
android:text="旋转"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click3"
android:text="缩放"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click4"
android:text="位移"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click5"
android:text="一起飞"/>
</LinearLayout>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher"/>
</RelativeLayout>
[2]res/anim/alpha.xml:
<?xml version="1.0" encoding="utf-8"?>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="2000"
android:repeatCount="1"
android:repeatMode="reverse"
xmlns:android="http://schemas.android.com/apk/res/android">
</alpha>
[3]res/anim/rotate.xml:
<?xml version="1.0" encoding="utf-8"?>
<rotate
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="2000"
android:repeatCount="1"
android:repeatMode="reverse"
xmlns:android="http://schemas.android.com/apk/res/android">
</rotate>
[4]res/anim/scale.xml:
<?xml version="1.0" encoding="utf-8"?>
<scale
android:fromXScale="1.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="2.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
android:repeatCount="1"
android:repeatMode="reverse"
xmlns:android="http://schemas.android.com/apk/res/android">
</scale>
[5]res/anim/translate.xml:
<?xml version="1.0" encoding="utf-8"?>
<translate
android:fromXDelta="0%p"
android:toXDelta="0%p"
android:fromYDelta="0%p"
android:toYDelta="20%p"
android:fillAfter="true"
android:duration="2000"
xmlns:android="http://schemas.android.com/apk/res/android">
</translate>
[6]res/anim/set.xml:
<?xml version="1.0" encoding="utf-8"?>
<set>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="2000"
android:repeatCount="1"
android:repeatMode="reverse"
xmlns:android="http://schemas.android.com/apk/res/android">
</alpha>
<rotate
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="2000"
android:repeatCount="1"
android:repeatMode="reverse"
xmlns:android="http://schemas.android.com/apk/res/android">
</rotate>
<translate
android:fromXDelta="0%p"
android:toXDelta="0%p"
android:fromYDelta="0%p"
android:toYDelta="20%p"
android:fillAfter="true"
android:duration="2000"
xmlns:android="http://schemas.android.com/apk/res/android">
</translate>
<scale
android:fromXScale="1.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="2.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
android:repeatCount="1"
android:repeatMode="reverse"
xmlns:android="http://schemas.android.com/apk/res/android">
</scale>
</set>
[7]MainActivity:
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//这个控件用来执行动画
iv = (ImageView) findViewById(R.id.iv);
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "点击imageview", 1).show();
}
});
}
//点击按钮 实现透明效果
public void click1(View v){
Animation animation=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);
//开始执行动画
iv.startAnimation(animation);
}
//点击按钮 实现旋转效果
public void click2(View v){
Animation animation=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);
//开始执行动画
iv.startAnimation(animation);
}
//点击按钮 实现缩放效果
public void click3(View v){
Animation animation=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale);
//开始执行动画
iv.startAnimation(animation);
}
//点击按钮 实现平移效果
public void click4(View v){
Animation animation=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate);
//开始执行动画
iv.startAnimation(animation);
}
//点击按钮 让动画一起执行
public void click5(View v){
//创建动画的合集
Animation animation=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.set);
//开始执行动画
iv.startAnimation(animation);
}
12.属性动画
包含了用xml定义属性动画
原理:会改变控件的真实坐标 必须是11及以上,会改变控件真实的坐标
ObjectAnimator 他不是new出来的
//10,50,20,150表示从10的位置移到50再移到20再移到150
ObjectAnimator.ofFloat(iv, "translationX", 10,50,20,150);
[1]清单文件改成11以上:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
[2]activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="translate"
android:text="平移" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="scale"
android:text="缩放" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="alpha"
android:text="透明" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="rotate"
android:text="旋转" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="fly"
android:text="set" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ll"
android:onClick="playxml"
android:text="播放xml定义的属性动画" />
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
[3]MainActivity:
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//作用 执行动画
iv = (ImageView) findViewById(R.id.iv);
//给iv设置一个监听事件
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "点击imageview", 0).show();
}
});
}
//位移动画
public void translate(View v){
//创建属性动画
/**
* target 执行的目标 谁执行动画
* propertyName 属性名字 The name of the property being animated.
* float... values 可变参数
*/
ObjectAnimator oa=ObjectAnimator.ofFloat(iv, "translationX", 10,50,20,150);
oa.setDuration(2000);
oa.start();//开始动画
}
//缩放动画
public void scale(View v){
ObjectAnimator oa=ObjectAnimator.ofFloat(iv, "scaleY", 0.1f,2,1,2);
oa.setDuration(2000);
oa.start();
}
///实现透明的效果
public void alpha(View v){
ObjectAnimator oa=ObjectAnimator.ofFloat(iv, "alpha", 0,0.5f,0,1,1);
oa.setDuration(2000);
oa.start();
}
//实现旋转的效果
public void rotate(View v){
//ObjectAnimator oa=ObjectAnimator.ofFloat(iv, "rotation", 0,180,90,360);
ObjectAnimator oa=ObjectAnimator.ofFloat(iv, "rotationY", 0,180,90,360);
oa.setDuration(2000);
oa.start();
}
//一起飞
public void fly(View v){
AnimatorSet as=new AnimatorSet();
ObjectAnimator oa=ObjectAnimator.ofFloat(iv, "translationX", 10,50,20,150);
ObjectAnimator oa2=ObjectAnimator.ofFloat(iv, "scaleY", 0.1f,2,1,2);
ObjectAnimator oa3=ObjectAnimator.ofFloat(iv, "alpha", 0,0.5f,0,1);
ObjectAnimator oa4=ObjectAnimator.ofFloat(iv, "rotationY", 0,180,90,360);
as.setDuration(2000);
as.setTarget(iv);
//往集合中添加动画
//挨个飞
//as.playSequentially(oa,oa2,oa3,oa4);
//一起飞
as.playTogether(oa,oa2,oa3,oa4);
as.start();
}
//使用xml的方式创建属性动画
public void playxml(View v){
ObjectAnimator oa = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.oanimator);
//设置执行目标
oa.setTarget(iv);
oa.start();//开始执行
}
[4]在res下新建animator,再在下面新建xml,/res/animator/oanimator.xml:
<?xml version="1.0" encoding="utf-8"?>
<animator xmlns:android="http://schemas.android.com/apk/res/android" >
<objectAnimator
android:propertyName="translationX"
android:duration="2000"
android:valueFrom="10"
android:valueTo="100">
</objectAnimator>
</animator>
13.通知栏的介绍
[1]Toast
[2]对话框
[3]通知栏
[1]清单文件加权限:
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.VIBRATE"/>
[2]activity_main:
<Button
android:onClick="click1"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="18dp"
android:layout_marginTop="90dp"
android:text="发送通知" />
<Button
android:onClick="click2"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="104dp"
android:text="取消通知" />
[3]MainActivity:
private NotificationManager nm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//[1]获取NotificationManager的实例
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
///点击按钮发送一台哦通知
public void click1(View v){
//链式调用 16及以上
/*Notification notification=new Notification.Builder(this)
.setContentTitle("我是大标题").setContentText("我是标题的内容")
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setContentIntent(pendingIntent)//设置点击逻辑
.build();*/
//兼容低版本的写法 参数是出现时候的标题和内容
Notification notification=new Notification(R.drawable.ic_launcher, "我接收到了一条通知", System.currentTimeMillis());
//设置一下震动 让呼吸灯亮
notification.defaults=Notification.DEFAULT_ALL;
//设置通知不让清除
notification.flags=Notification.FLAG_NO_CLEAR;
//创建意图对象
Intent intent =new Intent();
//实现拨打电话的功能
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+119));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
//点击通知对象的业务逻辑
notification.setLatestEventInfo(this, "我是大标题", "我是标题的内容", pendingIntent);
//发出通知
nm.notify(10, notification);
}
//点击按钮 取消发送一条通知
public void click2(View v){
//取消通知
nm.cancel(10);
}
smsManager 发送短信
fragmentManager fragment的管理者
TelephonyManager 电话管理者
WindowManager 窗口管理者 拿大小
NotificationManager 通知管理者
14.通知和服务连用
可以提升进程的优先级
startForeground(10, notification);//能发送通知,能提高服务的优先级(服务要在清单文件配置)
[1]权限<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.VIBRATE"/>
[2]例子:
public class TestService extends Service{
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
//当服务第一次启动的时候调用
@Override
public void onCreate() {
//[1]获取NotificationManager的实例
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//兼容低版本的写法 参数是出现时候的标题和内容
Notification notification=new Notification(R.drawable.ic_launcher, "我接收到了一条通知", System.currentTimeMillis());
//设置一下震动 让呼吸灯亮
notification.defaults=Notification.DEFAULT_ALL;
//设置通知不让清除
notification.flags=Notification.FLAG_NO_CLEAR;
//创建意图对象
Intent intent =new Intent();
//实现拨打电话的功能
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+119));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
//点击通知对象的业务逻辑
notification.setLatestEventInfo(this, "我是大标题", "我是标题的内容", pendingIntent);
//发出通知
//nm.notify(10, notification);
startForeground(10, notification);
super.onCreate();
}
}
15.今日总结
[1]Fragment:如何定义一个fragment 只要定义一个fragment 必须要重写onCreateView这个方法
[2]拿到fragment的管理者 开始事务 提交事务
[3]fragement兼容低版本的写法 使用V4包中的fragment FragmentActivity
[4]生命周期
[5]公共桥梁 Activity
[6]menu菜单
[7]autoCompleteTextView 和listview一样 需要数据适配器
[8]反编译 Android逆向助手
[9]android 中补间动画(必须的会)
[10]属性动画(ObjectAnimator.ofFloat)
[11]通知
【注意】为了区分Property animation和View animation的资源文件,从Android 3.1(api 12)开始,Property animation的xml文件存在res/animator/目录下(View animation的存在res/anim/目录下),
animator这个名是可选的。但是如果你想要使用Eclipse ADT plugin (ADT 11.0.0+)的布局编辑器,你就必须使用res/animator/目录,因为ADT只在该目录下寻找property animation的资源文件。
【异常】Class requires API level 11 (current min is 8): android.app.Fragment
解决:改清单文件minSdkVersion:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
【FragmentActivity和ActionBarActivity区别】:
Activity <<<FragmentActivity <<< AppCompatActivity <<< ActionBarActivity
Activity 发展到3.0(大概)之后,可以使用fragment了,但是support v4 提供了1.6~3.0的fragment兼容,所以如果需要用兼容版的fragment,则需要继承support v4提供的FragmentActivity。
而后一点点时间之后,3.0(大概)出现的ActionBar也被向前支持了,这次是出现在support v7里,如果需要使用兼容版的actionbar,则继承support v7提供的ActionBarActivity(它是继承FragmentActivity的)。
再然后也就是去年年底到今年,5.0提供了很多很多新东西,于是support v7也更新了,出现了AppCompatActivity , 具体功能请自行查找。
【横竖屏切换或判断】
[1]在AndroidManifest.xml中配置
假设不想让软件在横竖屏之间切换,最简单的办法就是在项目的AndroidManifest.xml中找到你所指定的activity中加上android:screenOrientation属性。他有下面几个參数:
"unspecified":默认值 由系统来推断显示方向.判定的策略是和设备相关的,所以不同的设备会有不同的显示方向.
"landscape":横屏显示(宽比高要长)
"portrait":竖屏显示(高比宽要长)
"user":用户当前首选的方向
"behind":和该Activity以下的那个Activity的方向一致(在Activity堆栈中的)
"sensor":有物理的感应器来决定。假设用户旋转设备这屏幕会横竖屏切换。
"nosensor":忽略物理感应器。这样就不会随着用户旋转设备而更改了("unspecified"设置除外)。
[2]在java代码中
//判断横竖屏
Configuration mConfiguration = this.getResources().getConfiguration(); //获取设置的配置信息
int ori = mConfiguration.orientation; //获取屏幕方向
System.out.println(ori);
if (ori == mConfiguration.ORIENTATION_LANDSCAPE) {
//横屏
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//强制为竖屏
} else if (ori == mConfiguration.ORIENTATION_PORTRAIT) {
//竖屏
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//强制为横屏
}