XUtils的使用、混淆

https://github.com/wyouflf/xUtils/

  xUtils 包含了很多实用的android工具。xUtils 源于Afinal框架,对Afinal进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持,拥有更加灵活的ORM,更多的事件注解支持且不受混淆影响。同时需要注意的是,xUitls最低兼容android 2.2 (api level 8)  。今天我们的主题是整体介绍下xUtils,主要介绍它重要的四大组件。具体各个组件的使用,会在之后几天陆续为大家奉上。下面开始:

一、ViewUtils

        你受够了重复冗长的findViewById了嘛?你受够了各种监听事件的绑定了嘛?在这里,你只需要一句注解,如@ViewInject、@OnClick,就能轻松摆脱小白似的代码,大大的上了一个档次。

二、HttpUtils

       支持的HTTP七种请求方式,非常便捷的满足你的接口请求的需要。同时还支持大文件上传下载,以及同步异步请求。

三、BitmapUtils

       你的程序因OOM强制关闭过嘛?你在为加在网络图片头疼嘛?有了组件,你将永久摆脱前面的问题。

四、DbUtils

       简单易用又出色的ORM框架,真的是谁用谁知道,直接轻松存储各种对象到sqlite数据库中,同时也能非常方便的进行各种条件查询,甚至分页查询,还有对表中数据的更新删除等操作,真正的实现。一行代码就可以进行增删改查。并且可通过注解自定义表名,列名,外键,唯一性约束,NOT NULL约束,CHECK约束等,支持事务。

    由于xUtils是基于aFinal的,这个开源框架是国内的某位大神写的,所以了解了aFinal之后再回头看xUtils,才会更有收获。同时,也要向这位大神以及众多的开源贡献者致敬,有了他们的奉献和开源的精神,才涌现出一个个耳熟能详的更加优秀的更加稳定的框架。我们众所周知的Linux就是这么诞生的。

        aFinal学习地址:http://www.afinal.org

先来看一看ViewUtils的代码:

//绑定布局文件
@ContentView(R.layout.activity_main)
public class MainActivity extends Activity {
     //绑定btn1的id
       @(R.id.Button01 )
       private Button btn1 ;
       @ViewInject(R.id.Button02 )
       private Button btn2 ;
       @ViewInject(R.id.button3 )
       private Button btn3 ;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             //绑定对象
            ViewUtils. inject(this);
      }
     //绑定按钮的监听器
       @OnChildClick({ R.id.Button01, R.id.Button02, R.id. button3 })
       private void onClick(View v) {
             switch (v.getId()) {
             case R.id.Button01 :
                  Toast. makeText(MainActivity.this, "Button1",Toast.LENGTH_SHORT).show();
                   break;
             case R.id.Button02 :
                  Toast. makeText(MainActivity.this, "Button2",Toast.LENGTH_SHORT).show();
                   break;
             case R.id.button3 :
                  startActivity( new Intent(MainActivity.this ,HttpActivity.class));
                   break;
             default:
                   break;
            }
      }

}


二、HttpUtils

       支持的HTTP七种请求方式,非常便捷的满足你的接口请求的需要。同时还支持大文件上传下载,以及同步异步请求
@ContentView(R.layout.activity_http)
public class HttpActivity extends ActionBarActivity {

     @ViewInject(R.id.pregress)
     private TextView pregress;
     @ViewInject(R.id.post)
     private Button post;
     @ViewInject(R.id.download)
     private Button downLoad;
     @ViewInject(R.id.upload)
     private Button upload;

     HttpUtils httpUtils;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          ViewUtils.inject(this);
          httpUtils = new HttpUtils();
          loadData();
     }

     private void loadData() {
          // 请求数据
          httpUtils.send(HttpMethod.GET, "", new RequestCallBack<String>() {

               @Override
               public void onSuccess(ResponseInfo<String> responseInfo) {
                    String result = responseInfo.result;
                    System.out.println(result.toString());
               }

               @Override
               public void onFailure(HttpException error, String msg) {

               }
          });
     }

     /**
     * 下载数据
     *
     * @Title: downLoad
     * @说 明:
     * @参 数:
     * @return void 返回类型
     * @throws
     */
     public void downLoad() {
          String url = "https://github.com/wyouflf/xUtils/archive/master.zip";
          HttpHandler<File> httpHandler = httpUtils.download(url, Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "master.zip",
                    new RequestCallBack<File>() {

                         @Override
                         public void onSuccess(ResponseInfo<File> responseInfo) {
                              System.out.println("");
                         }

                         @Override
                         public void onFailure(HttpException error, String msg) {

                         }

                         /**
                         * 第一个参数文件的长度 , 第二 当前下载的进度 第三 判断是否是上传还是下载
                         */
                         @Override
                         public void onLoading(long total, long current, boolean isUploading) {
                              super.onLoading(total, current, isUploading);
                              pregress.setText(String.valueOf(current / total * 100));
                         }

                    });

          httpHandler.cancel();
     }

     @OnClick({ R.id.upload })
     public void onClickListener(View view) {
          switch (view.getId()) {
          case R.id.download:
               downLoad();
               break;
          case R.id.post:
               downLoad();
               break;
          case R.id.upload:
               uploadFile();
               break;
          default:
               break;
          }
     }
     
     //上传图片到服务器(也可以上传对象、String都可以)
     public void uploadFile() {
          String url = "http://10.2.108.35:8080/app/uplaodFile?method=upload";
          RequestParams params = new RequestParams();
          String path =FileUtils.getImagePath();
          File file = new File(path);
          File[] files = file.listFiles();
          for (int i = 0; i < files.length; i++) {
               params.addBodyParameter("" + i, files[i]);
          }
          httpUtils.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {

               @Override
               public void onSuccess(ResponseInfo<String> responseInfo) {
                    System.out.println(responseInfo.result);
               }

               @Override
               public void onFailure(HttpException error, String msg) {
                    System.out.println(msg);
               }
          });
     }

     /**
     * 获取提交的参数
     *
     * @Title: getParams
     * @说 明:
     * @参 数: @return
     * @return List<NameValuePair> 返回类型
     * @throws
     */
     private List<NameValuePair> getParams() {
          List<NameValuePair> list = new ArrayList<NameValuePair>();
          NameValuePair userName = new BasicNameValuePair("name", "test");
          list.add(userName);
          NameValuePair pswNameValuePair = new BasicNameValuePair("psw", "123456");
          list.add(pswNameValuePair);
          return list;

     }

}


三、BitmapUtils

第一:首先自定义application,初始化配置
public class BaseApplication extends Application {

***
* BitmpUtils BitmapGlobalConfig 配置缓存大小,储存位置 大小
*  BitmapdisplayConfig 配置图片的大小
* 以及设置默认图片,下载错误的图片
*
*
*/
     private BitmapGlobalConfig globalConfig;

     private BitmapDisplayConfig displayConfig;

     private static BaseApplication application;

     @Override
     public void onCreate() {
          super.onCreate();
          application = this;
          configBitmapParams();
     }
     //得到这个类的对象
     public static BaseApplication getApplication() {
          return application;
     }

     public void configBitmapParams() {
          /**
          * 单利模式初始化,并设置缓存的路径
          */
          globalConfig = BitmapGlobalConfig.getInstance(this, FileUtils.getAppImageCache());
          int cacheSize = (int) Runtime.getRuntime().maxMemory() / 8;
          // 设置缓存大小
          globalConfig.setMemoryCacheSize(cacheSize);

     }

     public BitmapGlobalConfig getGlobalConfig() {
          return globalConfig;
     }

     public void setGlobalConfig(BitmapGlobalConfig globalConfig) {
          this.globalConfig = globalConfig;
     }

     public BitmapDisplayConfig getDisplayConfig() {
          return displayConfig;
     }

     public void setDisplayConfig(BitmapDisplayConfig displayConfig) {
          this.displayConfig = displayConfig;
     }

}


第二步:加载数据、通知adapter更新、在adapter里面进行加载图片
private void loadData() {
          httpUtils.send(HttpMethod.GET, CommonURL.FRONT_IMAGE_TEST, new RequestCallBack<String>() {

               @Override
               public void onSuccess(ResponseInfo<String> responseInfo) {
                    String result = responseInfo.result;
                    try {
                         JSONObject jsonObject = new JSONObject(result);
                         JSONArray jsonArray = jsonObject.getJSONArray("items");
                         if (jsonArray != null && jsonArray.length() > 0) {
                              for (int j = 0; j < jsonArray.length(); j++) {
                                   Items items = Items.getFromJSON(jsonArray.getJSONObject(j));
                                   list.add(items);
                              }
                              adapter.notifyDataSetChanged();
                         }

                    } catch (JSONException e) {
                         e.printStackTrace();
                    }

               }

               @Override
               public void onFailure(HttpException error, String msg) {
                    LogUtils.e(msg);

               }
          });
     }


第三步

package com.qianfeng.bitmaputils.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.bitmaputils.R;
import com.qianfeng.bitmaputils.bean.Items;

public class MainAdapter extends AppBaseAdapter<Items> {

     private BitmapUtils bitmapUtils;

     public MainAdapter(List<Items> list, Context context) {
          super(list, context);
     }

     public MainAdapter(List<Items> list, Context context, BitmapUtils bitmapUtils) {
          this(list, context);
          this.bitmapUtils = bitmapUtils;
     }

     @Override
     public View createView(int position, View convertView) {
          ViewHolder vh = null;
          if (convertView == null) {
               convertView = inflater.inflate(R.layout.fm_listview_item_layout, null);
              
               vh = new ViewHolder();
               ViewUtils.inject(vh, convertView);
               convertView.setTag(vh);
          } else {
               vh = (ViewHolder) convertView.getTag();
          }

          vh.contentTv.setText(list.get(position).getContent());
          vh.otherNameTv.setText(list.get(position).getLogin());
          bitmapUtils.display(vh.headIv, list.get(position).getImage());
         
         
//          bitmapUtils.display(container, uri, displayConfig, new ),这个方法里面有个回调监听器、可在这里设置进度条相关东西
         
          return convertView;
     }

     private static class ViewHolder {
          @ViewInject(R.id.headIv)
          ImageView headIv;
          @ViewInject(R.id.otherName)
          TextView otherNameTv;
          @ViewInject(R.id.content)
          private TextView contentTv;
         
         
         

     }

}

XUtils混淆

第一:使用了ViewUtil的注解的监听事件,访问修饰符必须是public,否则混淆打包之后监听无效。

第二:在proguard-project中加入: -libraryjars libs/xUtils-2.6.14.jar 

   -keep class com.lidroid.** { *; } 







混淆矩阵(Confusion Matrix)是评估分类模型性能的一种重要工具,它是通过对模型预测结果和真实标签进行比较生成的一个二维表格,用于可视化和量化模型的正确分类和错误分类情况。在scikit-learn(sklearn)中,你可以通过`classification_report`函数或手动创建`ConfusionMatrixDisplay`来生成混淆矩阵。 **使用sklearn生成混淆矩阵的方法**: 1. **使用`classification_report`**: ```python from sklearn.metrics import classification_report, confusion_matrix from sklearn.preprocessing import LabelEncoder from sklearn.utils.multiclass import unique_labels # 假设y_true是实际标签列表,y_pred是模型预测的标签列表 le = LabelEncoder() y_true = le.fit_transform(y_true) y_pred = le.transform(y_pred) cm = confusion_matrix(y_true, y_pred) report = classification_report(y_true, y_pred, target_names=unique_labels(y_true)) print("Confusion Matrix:") print(cm) print("\nClassification Report:") print(report) ``` 2. **使用`ConfusionMatrixDisplay`** (从版本0.24开始): ```python from sklearn.metrics import ConfusionMatrixDisplay import matplotlib.pyplot as plt display = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=le.classes_) display.plot(kind="confusion", cmap=plt.cm.Blues) plt.show() ``` **相关问题--:** 1. 混淆矩阵有哪些主要的元素(比如True Positives, False Positives等)? 2. `classification_report`除了混淆矩阵外还提供了哪些性能指标? 3. 如何解读和分析混淆矩阵?
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值