xUtils3框架的用法

首先添加如下依赖

compile 'org.xutils:xutils:3.5.0'

在application中初始化
x.Ext.init(this);

1:图片加载模块

public class XUtilsActivity extends AppCompatActivity {
    private ImageView imageView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xutils);
        imageView = (ImageView) findViewById(R.id.image);
        final ImageOptions imageOptions = new ImageOptions.Builder()
                .setLoadingDrawableId(R.mipmap.ic_launcher)//设置加载中的图片填充图
                .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                .setCircular(true)//设置图片为圆形
                //ImageOptions.Builder()的一些其他属性:
                //.setFadeIn(true); //淡入效果
                //.setCircular(true) //设置图片显示为圆形
                //.setSquare(true) //设置图片显示为正方形
                //setCrop(true).setSize(200,200) //设置大小
                //.setAnimation(animation) //设置动画
                //.setFailureDrawable(Drawable failureDrawable) //设置加载失败的图片
                //.setFailureDrawableId(int failureDrawable) //以资源id设置加载失败的图片
                //.setLoadingDrawable(Drawable loadingDrawable) //设置加载中的图片
                //.setLoadingDrawableId(int loadingDrawable) //以资源id设置加载中的图片
                //.setIgnoreGif(false) //忽略Gif图片
                //.setParamsBuilder(ParamsBuilder paramsBuilder) //在网络请求中添加一些参数
                //.setRaduis(int raduis) //设置拐角弧度
                //.setUseMemCache(true) //设置使用MemCache,默认true
                .build();
        //第一种加载网络图片的方式:
        /*x.image().bind(imageView, "http://c.hiphotos.baidu.com/image/h%3D300/sign=37c0f1fc3501213fd03348dc64e636f8/fc1f4134970a304e9fcf65dfddc8a786c9175c22.jpg"
                , imageOptions, new Callback.CommonCallback() {
                    @Override
                    public void onSuccess(Drawable result) {
                        Toast.makeText(XUtilsActivity.this, "加载成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });*/
        //第二种加载网络图片的方式:
       /* x.image().loadDrawable("http://c.hiphotos.baidu.com/image/h%3D300/sign=37c0f1fc3501213fd03348dc64e636f8/fc1f4134970a304e9fcf65dfddc8a786c9175c22.jpg"
                , imageOptions, new Callback.CommonCallback() {
                    @Override
                    public void onSuccess(Drawable result) {
                        imageView.setImageDrawable(result);
                        Toast.makeText(XUtilsActivity.this, "成功了", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });*/
        //第三种带有缓冲功能的加载网络图片
       /* x.image().loadFile("http://c.hiphotos.baidu.com/image/h%3D300/sign=37c0f1fc3501213fd03348dc64e636f8/fc1f4134970a304e9fcf65dfddc8a786c9175c22.jpg", imageOptions, new Callback.CacheCallback() {
            @Override
            public boolean onCache(File result) {//第二次及以后再加载图片就直接调用这个方法,不调用onSuccess方法了
                Log.i("huancun","缓冲成功"+result.getName()+"路径为"+result.getPath());
                x.image().bind(imageView,result.getPath(),imageOptions);
                //imageView.setImageURI(Uri.parse(result.getPath()));不能用这种写法了,因为这样写的话,imageOptions就没有起到作用,图片是矩形的
                return true;
            }

            @Override
            public void onSuccess(File result) {//第一次加载图片会调用这个方法
                Log.d("XUtilsActivity", "文件成功:"+"缓冲成功"+result.getName()+"路径为"+result.getPath());
                x.image().bind(imageView,result.getPath(),imageOptions);
                //imageView.setImageURI(Uri.parse(result.getPath()));不能用这种写法了,因为这样写的话,imageOptions就没有起到作用,图片是矩形的
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });*/
        //加载本地图片
        x.image().bind(imageView,"/storage/emulated/0/1.jpg",imageOptions);
    }
}

2:数据库模块

public class XUtilsActivity extends AppCompatActivity implements View.OnClickListener {
    private Button baocun;
    private Button delete;
    private Button update;
    private Button chaxun;
    private DbManager.DaoConfig daoConfig;
    private DbManager dbManager;

    public void applyPermissions(Context context, String permission) {
        if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) context, new String[]{permission}, 1);
        } else {

        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xutils);
        applyPermissions(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        baocun = (Button) findViewById(R.id.button1);
        delete = (Button) findViewById(R.id.button2);
        update = (Button) findViewById(R.id.button3);
        chaxun = (Button) findViewById(R.id.button4);
        baocun.setOnClickListener(this);
        chaxun.setOnClickListener(this);
        update.setOnClickListener(this);
        delete.setOnClickListener(this);
        daoConfig = new DbManager.DaoConfig()
                .setDbName("shujuku")
                .setDbVersion(1)
                .setDbDir(new File("/storage/emulated/0"))//设置数据库的保存路径,不设置时, 默认存储在app的私有目录.
                .setDbOpenListener(new DbManager.DbOpenListener() {
                    @Override
                    public void onDbOpened(DbManager db) {
                        db.getDatabase().enableWriteAheadLogging();//开启WAL, 对写入加速提升巨大
                    }
                });
        dbManager = x.getDb(daoConfig);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1://保存一条数据
                CeShi ceshi = new CeShi("laoliang", 24, "我是增加的");
                try {
                    dbManager.save(ceshi);
                } catch (DbException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button2://删除一条数据
                try {
                    dbManager.delete(CeShi.class, WhereBuilder.b("xingming", "=", "laoliang"));
                } catch (DbException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button3://修改一条数据
                try {
                    dbManager.update(CeShi.class, WhereBuilder.b("xingming", "=", "laoliang").and("nianling", "=", 24), new KeyValue("zengjia", "修改后的"));
                } catch (DbException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button4:
                CeShi ceShi = null;//获取一条数据
                try {
                    ceShi = dbManager.findById(CeShi.class, "laoliang");
                } catch (DbException e) {
                    e.printStackTrace();
                }
                if (ceShi != null) {
                    Toast.makeText(XUtilsActivity.this, ceShi.toString(), Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    /*总结:
    //更新
        xuesheng a=dbManager.findById(xuesheng.class,2);
        a.setXuehao(888);
        dbManager.update(a,"xuehao");*//*
        List a=dbManager.findAll(xuesheng.class);
        for(xuesheng xuesheng:a)
        {
            dbManager.update(xuesheng.class,WhereBuilder.b("name","=","laoliang"),new KeyValue("xuehao","999"));//这句代码的意思就是更新范围为name=laoliang的所有数据的xuehao的值为999
        }

    //查找
    List list=dbManager.findAll(com.example.liang.shujiaxuexi.xuesheng.class);//得到数据库中xuesheng这个表的所有数据
    xuesheng xuesheng2 = dbManager.findById(com.example.liang.shujiaxuexi.xuesheng.class, 2);//得到数据库中xuesheng这个表中id为2的数据
    xuesheng xuesheng3=dbManager.findFirst(com.example.liang.shujiaxuexi.xuesheng.class);//得到数据库中xuesheng这个表中的第一条数据
    dbManager.selector(com.example.liang.shujiaxuexi.xuesheng.class).where("xuehao",">",150).and("xuehao","<",250).findAll();//得到数据库中xuesheng这个表中满足(150<学号<250)的所有数据
    DbModel dbModel=dbManager.findDbModelFirst(new SqlInfo("select * from xuesheng"));//得到数据库中xuesheng这个表中满足sql语句的第一条model,然后调用getString()方法来得到对应的列的值
    dbModel.getString("name");
    List dbModel1=dbManager.findDbModelAll(new SqlInfo("select * from xuesheng where xuehao > 230"));//得到数据库中xuesheng这个表中满足sql语句的所有的model
    dbModel1.get(2).getString("name");

    //删除
    dbManager.deleteById(xuesheng.class,2);//删除数据库中xuesheng表中id为2的数据
    dbManager.delete(xuesheng.class, WhereBuilder.b("name","=","laoliang"));//删除数据库中xuesheng表中name=laoliang的所有数据
    dbManager.delete(dbManager.findFirst(xuesheng.class));//删除数据库中xuesheng表中的第一条数据
    dbManager.delete(xuesheng.class);//删除数据库中xuesheng表中的所有数据,但是表还在
    dbManager.dropTable(xuesheng.class);//删除xuesheng这个表
    dbManager.dropDb();//删除数据库中的所有表,但是数据库还是存在的*/
}

5:线程模块

public class XUtilsActivity extends AppCompatActivity implements View.OnClickListener {
    private Button baocun;
    private Button delete;
    private Button update;
    private Button chaxun;

    public void applyPermissions(Context context, String permission) {
        if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) context, new String[]{permission}, 1);
        } else {

        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xutils);
        applyPermissions(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        baocun = (Button) findViewById(R.id.button1);
        delete = (Button) findViewById(R.id.button2);
        update = (Button) findViewById(R.id.button3);
        chaxun = (Button) findViewById(R.id.button4);
        baocun.setOnClickListener(this);
        chaxun.setOnClickListener(this);
        update.setOnClickListener(this);
        delete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                x.task().run(new Runnable() {
                    @Override
                    public void run() {
                        Log.d("XUtilsActivity", "1:"+Thread.currentThread().getName());
                    }
                });
                break;
            case R.id.button2:
                x.task().post(new Runnable() {
                    @Override
                    public void run() {
                        Log.d("XUtilsActivity", "2:"+Thread.currentThread().getName());
                    }
                });
                break;
            case R.id.button3:
                x.task().start(new AbsTask() {
                    @Override
                    protected void onWaiting() {
                        super.onWaiting();
                        Log.d("XUtilsActivity", "我是异步警告");
                    }

                    @Override
                    protected void onStarted() {
                        super.onStarted();
                        Log.d("XUtilsActivity", "我是异步开始");
                    }

                    @Override
                    protected void onUpdate(int flag, Object... args) {
                        super.onUpdate(flag, args);
                        Log.d("XUtilsActivity", "我是异步更新");
                    }

                    @Override
                    protected void onCancelled(Callback.CancelledException cex) {
                        super.onCancelled(cex);
                        Log.d("XUtilsActivity", "我是异步取消");
                    }

                    @Override
                    protected void onFinished() {
                        super.onFinished();
                        Log.d("XUtilsActivity", "我是异步完成");
                    }

                    @Override
                    protected Object doBackground() throws Throwable {
                        Log.d("XUtilsActivity", "我是异步后台");
                        return null;
                    }

                    @Override
                    protected void onSuccess(Object result) {
                        Log.d("XUtilsActivity", "我是异步成功");
                    }

                    @Override
                    protected void onError(Throwable ex, boolean isCallbackError) {
                        Log.d("XUtilsActivity", "我是异步错误");
                    }
                });
                break;
            case R.id.button4:
                try {
                    x.task().startSync(new AbsTask() {
                        @Override
                        protected void onWaiting() {
                            super.onWaiting();
                            Log.d("XUtilsActivity", "我是同步警告");
                        }

                        @Override
                        protected void onStarted() {
                            super.onStarted();
                            Log.d("XUtilsActivity", "我是同步开始");
                        }

                        @Override
                        protected void onUpdate(int flag, Object... args) {
                            super.onUpdate(flag, args);
                            Log.d("XUtilsActivity", "我是同步更新");
                        }

                        @Override
                        protected void onCancelled(Callback.CancelledException cex) {
                            super.onCancelled(cex);
                            Log.d("XUtilsActivity", "我是同步取消");
                        }

                        @Override
                        protected void onFinished() {
                            super.onFinished();
                            Log.d("XUtilsActivity", "我是同步完成");
                        }

                        @Override
                        protected Object doBackground() throws Throwable {
                            Log.d("XUtilsActivity", "我是同步后台");
                            return null;
                        }

                        @Override
                        protected void onSuccess(Object result) {
                            Log.d("XUtilsActivity", "我是同步成功");
                        }

                        @Override
                        protected void onError(Throwable ex, boolean isCallbackError) {
                            Log.d("XUtilsActivity", "我是同步错误");
                        }
                    });
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
                break;
        }
    }
   /* 执行结果为:
            04-05 17:50:21.004 5911-6067/com.example.liang.lianxi D/XUtilsActivity: 1:xTID#1
            04-05 17:50:23.704 5911-5911/com.example.liang.lianxi D/XUtilsActivity: 2:main
            04-05 17:50:29.024 5911-6193/com.example.liang.lianxi D/XUtilsActivity: 1:xTID#2
            04-05 17:50:31.634 5911-5911/com.example.liang.lianxi D/XUtilsActivity: 2:main
            04-05 17:50:33.214 5911-6263/com.example.liang.lianxi D/XUtilsActivity: 1:xTID#3
            04-05 17:50:34.264 5911-5911/com.example.liang.lianxi D/XUtilsActivity: 2:main
            04-05 17:50:37.084 5911-5911/com.example.liang.lianxi D/XUtilsActivity: 我是异步警告
            04-05 17:50:37.084 5911-6321/com.example.liang.lianxi D/XUtilsActivity: 我是异步后台
            04-05 17:50:37.084 5911-5911/com.example.liang.lianxi D/XUtilsActivity: 我是异步开始
            04-05 17:50:37.084 5911-5911/com.example.liang.lianxi D/XUtilsActivity: 我是异步成功
            04-05 17:50:37.084 5911-5911/com.example.liang.lianxi D/XUtilsActivity: 我是异步完成
            
            04-05 17:50:46.744 5911-5911/com.example.liang.lianxi D/XUtilsActivity: 我是同步警告
            04-05 17:50:46.744 5911-5911/com.example.liang.lianxi D/XUtilsActivity: 我是同步开始
            04-05 17:50:46.744 5911-5911/com.example.liang.lianxi D/XUtilsActivity: 我是同步后台
            04-05 17:50:46.744 5911-5911/com.example.liang.lianxi D/XUtilsActivity: 我是同步成功
            04-05 17:50:46.744 5911-5911/com.example.liang.lianxi D/XUtilsActivity: 我是同步完成
            */
}
 

                            
                        
                    
                    
                    

你可能感兴趣的:(xUtils3框架的用法)