1_xUtils3 简介
1.1_简介
xUtils3 是 xUtils 的升级版,功能和性能都在提高,xUtils3 主要有四大模块:注解模块,联 网模块,图片加载模块,数据库模块;
注解模块: 用于在 Activity 或者 Fragment 中初始化布局文件,便于代码更加简洁;
联网模块: xUtils3 支持超大文件(超过2G)上传,更全面的http请求协议支持(11种谓词), 拥有更加灵活的 ORM,更多的事件注解支持且不受混淆影响;
图片加载模块: 加载图片很方便并且不用担心内存溢出,还可以图片绑定支持 gif(受系统兼容 性影响, 部分 gif 文件只能静态显示), webp; 支持圆角, 圆形, 方形等裁剪, 支持自动旋转.
数据库模块: 数据库 api 简化提高性能, 达到和 greenDao 一致的性能
1.2_xUtils3 的主要功能
1、注解
2、联网请求文本数据
3、大文件下载
4、大文件上传
5、请求图片
6、数据库模块达到和 greenDao 一致的性能
1.3_下载地址&运行 xUtils3 案例
https://github.com/wyouflf/xUtils3
2_xUtils3 注解模块
主要讲在 Activity 中使用 xUtils3 的注解和在 Fragment 中使用 xUtils3 的注解
1.1_XUtils3MainActivity 布局
<?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"> <!--标题栏 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:background="@android:color/holo_blue_light" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/white" android:textSize="20sp"/> </LinearLayout> <Button android:id="@+id/btn_annotation" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="xUtils3 注解模块"/> <Button android:id="@+id/btn_net" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="xUtils3 联网模块"/> <Button android:id="@+id/btn_image" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="xUtils3 请求图片"/> </LinearLayout>
1.2_在 Activity 中使用注解初始化布局
Activity 的注解
1.在 Application 的 oncreate 方法中加入下面代码: x.Ext.init(this);
2.在 Activity 的 oncreate 方法中加入下面代码: x.view().inject(this);
3.加载当前的 Activity 布局需要如下注解: @ContentView 加入到 Activity 的上方
4.给 View 进行初始化需要如下注解: @InjectView
5.处理控件的各种响应事件需要如下注解: @Envent 使用注解后你会发现写代码更加简洁了
@ContentView(R.layout.activity_xutils3) public class XUtils3Activity extends Activity { /** * 初始化 TextView */ @ViewInject(R.id.tv_title) private TextView tv_title; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 添加注解 x.view().inject(this); // 设置文本 tv_title.setText("xUtils3 详解"); } /** * 多个控件的点击事件共用一个方法 * * @param view */ @Event(value = {R.id.btn_annotation, R.id.btn_net, R.id.btn_image, R.id.btn_database}) private void getEnvent(View view) { switch (view.getId()) { case R.id.btn_annotation: Intent intent = new Intent(this, FragmentActivity.class); startActivity(intent); break; case R.id.btn_net: Toast.makeText(XUtils3Activity.this, "进入网络模块", Toast.LENGTH_SHORT).show(); break; case R.id.btn_image: Toast.makeText(XUtils3Activity.this, "进入网络模块", Toast.LENGTH_SHORT).show(); break; case R.id.btn_database: Toast.makeText(XUtils3Activity.this, "进入数据库模块", Toast.LENGTH_SHORT).show(); break; } } /** * 单个点击事件 * * @param view */ @Event(value = R.id.btn_database) private void database(View view) { Toast.makeText(XUtils3Activity.this, "进入数据库模块单独的", Toast.LENGTH_SHORT).show(); } }
1.3_在 Fragment 中使用注解初始化布局
1_在 FragmentActivity 中添加 Fragment
<?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"><!-标题栏 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:background="@android:color/holo_blue_light" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/white" android:textSize="20sp"/> </LinearLayout> <FrameLayout android:id="@+id/fl_content" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
Fragment 的注解 代码如下
// 设置布局文件 @ContentView(R.layout.activity_frament) public class FragmentActivity extends android.support.v4.app.FragmentActivity { // 初始化布局里面的文本控件 @ViewInject(R.id.tv_title) private TextView tv_title; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 注入 Activity x.view().inject(this); // 直接使用 TextView控件 tv_title.setText("在 Fragment 中使用注解初始化布局"); FragmentManager fm = getSupportFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); transaction.replace(R.id.fl_content, new DemoFragment()); transaction.commit(); } }
2_DemoFragment 布局和代码
布局 fragment_demo.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" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/tv_text" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
代码
@ContentView(R.layout.fragment_demo) public class DemoFragment extends Fragment { @ViewInject(R.id.tv_text) private TextView textView; @ViewInject(R.id.btn) private Button button; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return x.view().inject(this, inflater, container); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); textView.setText("该控件使用注解初始化的"); button.setText("我是按钮"); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getActivity(), "点击了", Toast.LENGTH_SHORT).show(); } }); } }