常用工具类、依赖、权限
来源:互联网 发布:知乎发帖时间 编辑:程序博客网 时间:2024/06/06 03:47
常用权限
<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
常用依赖
butterknife
compile ‘com.jakewharton:butterknife:8.8.1’
annotationProcessor ‘com.jakewharton:butterknife-compiler:8.8.1’
imageloader
compile ‘com.nostra13.universalimageloader:universal-image-loader:1.9.5’
TabLayout—design包导入
compile ‘com.android.support:design:26+’
gson包导入
compile ‘com.google.code.gson:gson:2.2.4’
pull-to-refresh上下拉刷新控件
compile ‘com.github.userswlwork:pull-to-refresh:1.0.0’
底部导航
compile 'com.hjm:BottomTabBar:1.1.1'
常用工具类
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); ImageloaderUtil.initConfig(this); }}
public class ImageloaderUtil { public static void initConfig(Context context) { //配置// File cacheFile=context.getExternalCacheDir(); File cacheFile= new File(Environment.getExternalStorageDirectory()+"/"+"image"); ImageLoaderConfiguration config=new ImageLoaderConfiguration.Builder(context) .memoryCacheExtraOptions(480, 800)//缓存图片最大的长和宽 .threadPoolSize(2)//线程池的数量 .threadPriority(4) .memoryCacheSize(2*1024*1024)//设置内存缓存区大小 .diskCacheSize(20*1024*1024)//设置sd卡缓存区大小 .diskCache(new UnlimitedDiscCache(cacheFile))//自定义缓存目录 .writeDebugLogs()//打印日志内容 .diskCacheFileNameGenerator(new Md5FileNameGenerator())//给缓存的文件名进行md5加密处理 .build(); ImageLoader.getInstance().init(config); } /** * 获取图片设置类 * @return */ public static DisplayImageOptions getImageOptions(){ DisplayImageOptions optionsoptions=new DisplayImageOptions.Builder() .cacheInMemory(true)//使用内存缓存 .cacheOnDisk(true)//使用磁盘缓存 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片格式 .displayer(new RoundedBitmapDisplayer(10))//设置圆角,参数代表弧度 .build(); return optionsoptions; }}
public class MyTask extends AsyncTask<String,Void,String> { //申请一个接口类对象 private Icallbacks icallbacks; //将无参构造设置成私有的,使之在外部不能够调用 private MyTask() { } //定义有参构造方法 public MyTask(Icallbacks icallbacks) { this.icallbacks = icallbacks; } @Override protected String doInBackground(String... params) { String str=""; try { //使用HttpUrlConnection URL url = new URL(params[0]); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); int code = connection.getResponseCode(); if(code==200){ InputStream stream = connection.getInputStream(); //调用工具类中的静态方法 str=StreamToString.streamTostr(stream,"utf-8"); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return str; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); //解析,封装到bean,更新UI组件 icallbacks.updataUiByjson(s); } //定义一个接口 public interface Icallbacks{ //根据回传的json字符串,解析并更新页面组件 void updataUiByjson(String jsonstr); }}
public class StreamToString { public static String streamTostr(InputStream inputStream, String chartSet){ StringBuilder builder = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String con; try { while((con=reader.readLine())!=null){ StringBuilder append = builder.append(con); } return builder.toString(); } catch (IOException e) { e.printStackTrace(); } return ""; }}