day01_xUtils+注解+动画

Xutils框架

1。说明
xUtils 包含了orm, http(s), image, view注解, 但依然很轻量级(246K), 并且特性强大, 方便扩展:
稳定的基石: AbsTask和统一的回调接口Callback, 任何异常, 即使你的回调方法实现有异常都会进入onError, 任何情况下onFinished总会让你知道任务结束了.
基于高效稳定的orm工具, http模块得以更方便的实现cookie(支持domain, path, expiry等特性)和缓存(支持Cache-Control, Last-Modified, ETag等特性)的支持.
有了强大的http及其下载缓存的支持, image模块的实现相当的简洁, 并且支持回收被view持有, 但被Mem Cache移除的图片, 减少页面回退时的闪烁…
view注解模块仅仅400多行代码却灵活的支持了各种View注入和事件绑定, 包括拥有多了方法的listener的支持.
官方地址:https://github.com/wyouflf/xUtils3
2。作用
构建快速互联网开发中小型项目基石,gradle部署,一行代码解决项目中的网络请求,图片加载,本地缓存以及注解查询控件等功能,基于XUtils可实现咨询项目,电商项目,社区项目的快速开发上线。
3。导入
Gradle中implementation ‘org.xutils:xutils:3.5.1’
网络请求
uses-permission android:name=“android.permission.INTERNET”
android:name=“android.permission.READ_EXTERNAL_STORAGE”
android:name=“android.permission.WRITE_EXTERNAL_STORAGE”
网络状态
uses-permission android:name=“android.permission.ACCESS_NETWORK_STATE”
网络状态改变
uses-permission android:name=“android.permission.CHANGE_NETWORK_STATE”
自定义Application
x.Ext.init(this);开启xutils
x.Ext.setDebug(BuildConfig.DEBUG);开启debug模式

注解

1。理解
注解(Annotation) 为我们在代码中添加信息提供了一种形式化的方法,是我们可以在稍后某个时刻方便地使用这些数据(通过 解析注解 来使用这些数据),常见的作用有以下几种:
生成文档。这是最常见的,也是java 最早提供的注解。常用的有@see @param @return 等
跟踪代码依赖性,实现替代配置文件功能。比较常见的是spring 2.5 开始的基于注解配置。作用就是减少配置。现在的框架基本都使用了这种配置来减少配置文件的数量。也是
在编译时进行格式检查。如@override 放在方法前,如果你这个方法并不是覆盖了超类方法,则编译时就能检查出。
2。元注解
元注解是指注解的注解。包括 @Retention @Target @Document @Inherited四种。
1.1、@Retention: 定义注解的保留策略
@Retention(RetentionPolicy.SOURCE)//注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS)// 默认的保留策略,注解会在class字节码文件中存在,但运行时无法得
@Retention(RetentionPolicy.RUNTIME)// 注解会在class字节码文件中存在,在运行时可以通过反射获取到

1.2、@Target:定义注解的作用目标
其定义的源码为:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
@Target(ElementType.TYPE) //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包
其中的@interface是一个关键字,在设计annotations的时候必须把一个类型定义为@interface,而不能用class或interface关键字,由以上的源码可以知道,他的elementType 可以有多个,一个注解可以为类的,方法的,字段的等等。
1.3、@Document:说明该注解将被包含在javadoc中
1.4、@Inherited:说明子类可以继承父类中的该注解

myapplication

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        x.Ext.setDebug(true);
        x.Ext.setDefaultHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
    }
}

ViewUtils使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
    <Button android:layout_width="match_parent"
            android:id="@+id/bt_click"
            android:text="click"
            android:layout_height="50dp"/>
    <Button android:layout_width="match_parent"
            android:id="@+id/bt_longclick"
            android:text="longclick"
            android:layout_height="50dp"/>
    <CheckBox android:layout_width="wrap_content"
              android:id="@+id/checkbox"
              android:layout_height="wrap_content"/>

</LinearLayout>
@ContentView(R.layout.activity_view)
public class ViewActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);

    }
    @Event(value = R.id.bt_click,type = View.OnClickListener.class)
    private void click(View view){
        Toast.makeText(this, "点击了", Toast.LENGTH_SHORT).show();
    }
    @Event(value = R.id.bt_longclick,type = View.OnLongClickListener.class)
    private boolean longclick(View view){
        Toast.makeText(this, "长按了", Toast.LENGTH_SHORT).show();
        return false;
    }
    @Event(value = R.id.checkbox,type = CompoundButton.OnCheckedChangeListener.class)
    private void checkedchanged(CompoundButton buttonView, boolean isChecked){
        if (isChecked){
            Toast.makeText(this, "勾选了", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this, "取消了", Toast.LENGTH_SHORT).show();
        }
    }
}

DBUtils使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
    <Button android:layout_width="match_parent"
            android:text="READ"
            android:id="@+id/bt_read"
            android:layout_height="50dp"/>
    <Button android:layout_width="match_parent"
            android:text="WRITE"
            android:id="@+id/bt_write"
            android:layout_height="50dp"/>
    <TextView android:layout_width="match_parent"
              android:id="@+id/tv_user"
              android:layout_height="wrap_content"/>

</LinearLayout>
@Table(name="UserUtilsDAO")
public class UserUtilsDAO {

    @Column(name="id",isId = true)
    private int id;

    @Column(name="name")
    private String name;

    @Column(name="age")
    private int age;

    public UserUtilsDAO(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public UserUtilsDAO() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "UserUtilsDAO{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

@ContentView(R.layout.activity_db)
public class DbActivity extends Activity {
    @ViewInject(R.id.tv_user)
    private TextView textView;

    private DbManager.DaoConfig daoConfig;
    private DbManager db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);


        daoConfig=new DbManager.DaoConfig();
        daoConfig.setDbName("UserUtilsDAO");
        daoConfig.setDbVersion(1);


        db = x.getDb(daoConfig);

    }
    @Event(value = {R.id.bt_read,R.id.bt_write},type = View.OnClickListener.class)
    private void click(View view){
        switch (view.getId()){
            case R.id.bt_read:
                read();
                break;
            case R.id.bt_write:
                write();
                break;
        }
    }

    private void write() {
        UserUtilsDAO userUtilsDAO=new UserUtilsDAO("刘瑞",19);
        try {
            db.saveOrUpdate(userUtilsDAO);
        } catch (DbException e) {
            e.printStackTrace();
        }
        UserUtilsDAO userUtilsDAO1=new UserUtilsDAO("刘大瑞",18);
        try {
            db.saveOrUpdate(userUtilsDAO1);
        } catch (DbException e) {
            e.printStackTrace();
        }
        UserUtilsDAO userUtilsDAO2=new UserUtilsDAO("刘小瑞",17);
        try {
            db.saveOrUpdate(userUtilsDAO2);
        } catch (DbException e) {
            e.printStackTrace();
        }

    }

    private void read() {
        try {
            UserUtilsDAO byId1 = db.findById(UserUtilsDAO.class, 1);
            UserUtilsDAO byId2 = db.findById(UserUtilsDAO.class, 2);
            UserUtilsDAO byId3 = db.findById(UserUtilsDAO.class, 3);
            textView.setText(byId1.toString()+"\n"+byId2.toString()+"\n"+byId3.toString());
        } catch (DbException e) {
            e.printStackTrace();
        }

    }
}

HttpUtils使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
    <Button android:layout_width="match_parent"
            android:text="GET"
            android:id="@+id/bt_get"
            android:layout_height="50dp"/>
    <Button android:layout_width="match_parent"
            android:text="POST"
            android:id="@+id/bt_post"
            android:layout_height="50dp"/>

</LinearLayout>
@ContentView(R.layout.activity_http)
public class httpActivity extends Activity {
    String url="http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";
    String post_url="http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
    }

    @Event(value = {R.id.bt_get,R.id.bt_post},type = View.OnClickListener.class)
    private void click(View view){
        switch (view.getId()){
            case R.id.bt_get:
                get();
                break;
            case R.id.bt_post:
                post();
                break;
        }
    }

    private void post() {
        RequestParams requestParams=new RequestParams();
        requestParams.setUri(post_url);
        //&limit=20&page=1
        requestParams.addBodyParameter("limit","20");
        requestParams.addBodyParameter("page","1");

        x.http().post(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Toast.makeText(httpActivity.this, ""+result.toString(), Toast.LENGTH_SHORT).show();
                Log.e("###",result.toString());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.getStackTrace();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void get() {
        RequestParams requestParams=new RequestParams(url);
        x.http().get(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Toast.makeText(httpActivity.this, ""+result.toString(), Toast.LENGTH_SHORT).show();
                Log.e("##2#",result.toString());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.getStackTrace();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}

BitmapUtils使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
    <Button android:layout_width="match_parent"
            android:id="@+id/bt_img"
            android:text="image"
            android:layout_height="50dp"/>
    <ImageView android:layout_width="200dp"
               android:layout_gravity="center"
               android:id="@+id/iv"
               android:layout_height="200dp"/>

</LinearLayout>
@ContentView(R.layout.activity_image)
public class ImageActivity extends Activity {

    String url="http://b-ssl.duitang.com/uploads/item/201703/30/20170330225122_TLMun.jpeg";
    @ViewInject(R.id.iv)
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
    }

    @Event(value = R.id.bt_img,type = View.OnClickListener.class)
    private void click(View view){
        x.image().bind(imageView,url);
    }
}

progressbar断点续传

<?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">
    <Button android:layout_width="match_parent"
            android:text="DOWNLOAD"
            android:id="@+id/bt_load"
            android:layout_height="50dp"/>

</LinearLayout>
@ContentView(R.layout.activity_load)
public class LoadActivity extends Activity {
    @ViewInject(R.id.bt_load)
    private Button button;
    private String url="http://softfile.3g.qq.com:8080/msoft/179/24659/43549/qq_hd_mini_1.4.apk";

    private ProgressDialog progressDialog;
    private Callback.Cancelable cancelable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        progress();
        listener();

    }

    private void progress() {

        progressDialog=new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setTitle("下载APK");
        progressDialog.setMessage("下载中");
        progressDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, "暂停", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                cancelable.cancel();
            }
        });

    }

    private void listener() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gethttp();
            }
        });
    }

    private void gethttp() {
        RequestParams requestParams=new RequestParams();
        requestParams.setUri(url);
        requestParams.setAutoRename(true);
        requestParams.setCancelFast(true);
        requestParams.setSaveFilePath("/sdcard/Misc/qq.apk");

        cancelable = x.http().get(requestParams, new Callback.ProgressCallback<File>() {
            @Override
            public void onSuccess(File result) {

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.getStackTrace();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                progressDialog.dismiss();
            }

            @Override
            public void onWaiting() {

            }

            @Override
            public void onStarted() {
                progressDialog.show();
            }

            @Override
            public void onLoading(long total, long current, boolean isDownloading) {
                if (isDownloading){
                    progressDialog.setProgress((int) (current*100/total));
                }
            }
        });

    }
}

xml动画

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
  <LinearLayout android:layout_width="match_parent"
                android:orientation="horizontal"
                android:layout_height="50dp">
      <Button android:layout_width="0dp"
              android:text="start"
              android:id="@+id/bt_startx"
                    android:layout_weight="1"
                    android:layout_height="match_parent"/>
      <Button android:layout_width="0dp"
              android:layout_weight="1"
              android:text="stop"
              android:id="@+id/bt_stopx"
              android:layout_height="match_parent"/>
  </LinearLayout>
    <ImageView android:layout_width="300dp"
               android:id="@+id/ivvx"
               android:background="@drawable/anim"
               android:layout_gravity="center"
               android:layout_height="300dp"/>

</LinearLayout>
@ContentView(R.layout.activity_animx)
public class AnimXmlActivity extends Activity {

    @ViewInject(R.id.ivvx)
    private ImageView imageView;
    private AnimationDrawable background;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);

        background = (AnimationDrawable) imageView.getBackground();

    }


    @Event(value = {R.id.bt_startx,R.id.bt_stopx},type = View.OnClickListener.class)
    private void click(View view){
        switch (view.getId()){
            case R.id.bt_startx:
              if (!background.isRunning()){
                  background.start();
              }
                break;
            case R.id.bt_stopx:
                if (background.isRunning()){
                    background.stop();
                }

                break;
        }
    }
}

java代码实现动画

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
  <LinearLayout android:layout_width="match_parent"
                android:orientation="horizontal"
                android:layout_height="50dp">
      <Button android:layout_width="0dp"
              android:text="start"
              android:id="@+id/bt_start"
                    android:layout_weight="1"
                    android:layout_height="match_parent"/>
      <Button android:layout_width="0dp"
              android:layout_weight="1"
              android:text="stop"
              android:id="@+id/bt_stop"
              android:layout_height="match_parent"/>
  </LinearLayout>
    <ImageView android:layout_width="300dp"
               android:id="@+id/ivv"
               android:layout_gravity="center"
               android:layout_height="300dp"/>

</LinearLayout>
@ContentView(R.layout.activity_anim)
public class AnimJavaActivity extends Activity {
    private AnimationDrawable animationDrawable;

    @ViewInject(R.id.ivv)
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);

        animationDrawable = new AnimationDrawable();
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_01),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_02),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_03),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_04),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_05),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_06),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_07),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_08),50);
        imageView.setImageDrawable(animationDrawable);
    }
    @Event(value = {R.id.bt_start,R.id.bt_stop},type = View.OnClickListener.class)
    private void click(View view){
        switch (view.getId()){
            case R.id.bt_start:
                if (!animationDrawable.isRunning()){
                    animationDrawable.start();
                }
                break;
            case R.id.bt_stop:
                if (animationDrawable.isRunning()){
                    animationDrawable.stop();
                }
                break;
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值