Browse Source

增加网络图片

tags/old_version_1
fyf 1 year ago
parent
commit
7b40a313e5
19 changed files with 356 additions and 20 deletions
  1. +2
    -2
      app/src/main/java/com/bonait/bnframework/MainApplication.java
  2. +24
    -0
      app/src/main/java/com/bonait/bnframework/business/ConfigData.java
  3. +48
    -0
      app/src/main/java/com/bonait/bnframework/common/image/MyBitmapUtils.java
  4. +66
    -0
      app/src/main/java/com/bonait/bnframework/common/image/utils/LocalCacheUtils.java
  5. +25
    -0
      app/src/main/java/com/bonait/bnframework/common/image/utils/MD5Encoder.java
  6. +58
    -0
      app/src/main/java/com/bonait/bnframework/common/image/utils/MemoryCacheUtils.java
  7. +118
    -0
      app/src/main/java/com/bonait/bnframework/common/image/utils/NetCacheUtils.java
  8. +4
    -7
      app/src/main/java/com/bonait/bnframework/modules/home/fragment/from/DiyActivity.java
  9. +4
    -8
      app/src/main/java/com/bonait/bnframework/modules/home/fragment/from/DiyUpdateActivity.java
  10. +6
    -2
      app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/QDListSectionAdapter.java
  11. +1
    -1
      app/src/main/res/layout/item/layout/note_rv_tree_second.xml
  12. BIN
     
  13. BIN
     
  14. BIN
     
  15. BIN
     
  16. BIN
     
  17. BIN
     
  18. BIN
     
  19. BIN
     

+ 2
- 2
app/src/main/java/com/bonait/bnframework/MainApplication.java View File

@@ -78,8 +78,8 @@ public class MainApplication extends Application {
//2.初始化SD卡,数据库DB //2.初始化SD卡,数据库DB
SdCart.getInstance().initSD(); SdCart.getInstance().initSD();


//4.初始化Main
ConfigData.getInstance();
//4.初始化Main,并且增加本地图片库
ConfigData.getInstance().AddImage(context);
// activity生命周期管理 // activity生命周期管理
ActivityLifecycleManager.get().init(this); ActivityLifecycleManager.get().init(this);




+ 24
- 0
app/src/main/java/com/bonait/bnframework/business/ConfigData.java View File

@@ -1,7 +1,11 @@
package com.bonait.bnframework.business; package com.bonait.bnframework.business;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log; import android.util.Log;


import com.bonait.bnframework.R;
import com.bonait.bnframework.common.constant.ConfigName; import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.constant.MessageName; import com.bonait.bnframework.common.constant.MessageName;
import com.bonait.bnframework.common.db.QueryDB; import com.bonait.bnframework.common.db.QueryDB;
@@ -11,6 +15,7 @@ import com.bonait.bnframework.common.helper.HttpUtils;
import com.bonait.bnframework.common.helper.Json; import com.bonait.bnframework.common.helper.Json;
import com.bonait.bnframework.common.helper.mode.OrderStatusChange; import com.bonait.bnframework.common.helper.mode.OrderStatusChange;
import com.bonait.bnframework.common.helper.mode.VersionMode; import com.bonait.bnframework.common.helper.mode.VersionMode;
import com.bonait.bnframework.common.image.utils.LocalCacheUtils;


import java.util.ArrayList; import java.util.ArrayList;


@@ -191,6 +196,25 @@ public class ConfigData {
ConfigUtil.write(ConfigName.getInstance().dishesCon,"Version", ConfigName.getInstance().Version); ConfigUtil.write(ConfigName.getInstance().dishesCon,"Version", ConfigName.getInstance().Version);
} }


/**
* 增加本地图片
*/
public void AddImage(Context context)
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=2;//宽高压缩为原来的1/2
options.inPreferredConfig=Bitmap.Config.ARGB_4444;
Bitmap bitmap1= BitmapFactory.decodeResource(context.getResources(), R.mipmap.image1,options);
Bitmap bitmap2= BitmapFactory.decodeResource(context.getResources(), R.mipmap.image2,options);
Bitmap bitmap3= BitmapFactory.decodeResource(context.getResources(), R.mipmap.image3,options);
Bitmap bitmap4= BitmapFactory.decodeResource(context.getResources(), R.mipmap.image4,options);
LocalCacheUtils localCacheUtils=new LocalCacheUtils();
localCacheUtils.setBitmapToLocal("image1.png",bitmap1);
localCacheUtils.setBitmapToLocal("image2.png",bitmap2);
localCacheUtils.setBitmapToLocal("image3.png",bitmap3);
localCacheUtils.setBitmapToLocal("image4.png",bitmap4);
}

/** /**
* 获取订单信息 * 获取订单信息
*/ */


+ 48
- 0
app/src/main/java/com/bonait/bnframework/common/image/MyBitmapUtils.java View File

@@ -0,0 +1,48 @@
package com.bonait.bnframework.common.image;

import android.graphics.Bitmap;
import android.widget.ImageView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.image.utils.LocalCacheUtils;
import com.bonait.bnframework.common.image.utils.MemoryCacheUtils;
import com.bonait.bnframework.common.image.utils.NetCacheUtils;

/**
* 自定义的BitmapUtils,实现三级缓存
*/
public class MyBitmapUtils {
private NetCacheUtils mNetCacheUtils;
private LocalCacheUtils mLocalCacheUtils;
private MemoryCacheUtils mMemoryCacheUtils;

public MyBitmapUtils(){
mMemoryCacheUtils=new MemoryCacheUtils();
mLocalCacheUtils=new LocalCacheUtils();
mNetCacheUtils=new NetCacheUtils(mLocalCacheUtils,mMemoryCacheUtils);
}

public void disPlay(ImageView ivPic, String url) {
ivPic.setImageResource(R.mipmap.loading2);
Bitmap bitmap;
//内存缓存
bitmap=mMemoryCacheUtils.getBitmapFromMemory(url);
if (bitmap!=null){
ivPic.setImageBitmap(bitmap);
System.out.println("从内存获取图片啦.....");
return;
}

//本地缓存
bitmap = mLocalCacheUtils.getBitmapFromLocal(url);
if(bitmap !=null){
ivPic.setImageBitmap(bitmap);
System.out.println("从本地获取图片啦.....");
//从本地获取图片后,保存至内存中
mMemoryCacheUtils.setBitmapToMemory(url,bitmap);
return;
}
//网络缓存
mNetCacheUtils.getBitmapFromNet(ivPic,url);
}
}

+ 66
- 0
app/src/main/java/com/bonait/bnframework/common/image/utils/LocalCacheUtils.java View File

@@ -0,0 +1,66 @@
package com.bonait.bnframework.common.image.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import com.bonait.bnframework.common.constant.ConfigName;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
* 三级缓存之本地缓存
*/
public class LocalCacheUtils {

private static final String CACHE_PATH= Environment.getExternalStorageDirectory().getAbsolutePath()+"/hblxiaochaodb/WebImage";

/**
* 从本地读取图片
* @param url
*/
public Bitmap getBitmapFromLocal(String url){
String fileName = null;//把图片的url当做文件名,并进行MD5加密
try {
fileName = MD5Encoder.encode(url);
File file=new File(CACHE_PATH,fileName);

Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(file));

return bitmap;
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

/**
* 从网络获取图片后,保存至本地缓存
* @param url
* @param bitmap
*/
public void setBitmapToLocal(String url,Bitmap bitmap){
try {
String fileName = MD5Encoder.encode(url);//把图片的url当做文件名,并进行MD5加密
File file=new File(CACHE_PATH,fileName);

//通过得到文件的父文件,判断父文件是否存在
File parentFile = file.getParentFile();
if (!parentFile.exists()){
parentFile.mkdirs();
}

if(!file.exists())//文件不存在
{
//把图片保存至本地
bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(file));
}
} catch (Exception e) {
e.printStackTrace();
}

}
}

+ 25
- 0
app/src/main/java/com/bonait/bnframework/common/image/utils/MD5Encoder.java View File

@@ -0,0 +1,25 @@
package com.bonait.bnframework.common.image.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Encoder {
public static String encode(String pwd) {
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] bytes = digest.digest(pwd.getBytes());
StringBuffer sb = new StringBuffer();
for(int i = 0;i<bytes.length;i++){
String s = Integer.toHexString(0xff&bytes[i]);
if(s.length()==1){
sb.append("0"+s);
}else{
sb.append(s);
}
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
return "buhuifasheng";
}
}
}

+ 58
- 0
app/src/main/java/com/bonait/bnframework/common/image/utils/MemoryCacheUtils.java View File

@@ -0,0 +1,58 @@
package com.bonait.bnframework.common.image.utils;

import android.graphics.Bitmap;
import android.util.LruCache;

/**
* 三级缓存之内存缓存
*/
public class MemoryCacheUtils {

// private HashMap<String,Bitmap mMemoryCache=new HashMap< ();//1.因为强引用,容易造成内存溢出,所以考虑使用下面弱引用的方法
// private HashMap<String, SoftReference<Bitmap mMemoryCache = new HashMap< ();//2.因为在Android2.3+后,系统会优先考虑回收弱引用对象,官方提出使用LruCache
private LruCache<String,Bitmap> mMemoryCache;

public MemoryCacheUtils(){
long maxMemory = Runtime.getRuntime().maxMemory()/8;//得到手机最大允许内存的1/8,即超过指定内存,则开始回收
//需要传入允许的内存最大值,虚拟机默认内存16M,真机不一定相同
mMemoryCache=new LruCache<String,Bitmap> ((int) maxMemory){
//用于计算每个条目的大小
@Override
protected int sizeOf(String key, Bitmap value) {
int byteCount = value.getByteCount();
return byteCount;
}
};
}

/**
* 从内存中读图片
* @param url
*/
public Bitmap getBitmapFromMemory(String url) {
//Bitmap bitmap = mMemoryCache.get(url);//1.强引用方法
/*2.弱引用方法
SoftReference<Bitmap bitmapSoftReference = mMemoryCache.get(url);
if (bitmapSoftReference != null) {
Bitmap bitmap = bitmapSoftReference.get();
return bitmap;
}
*/
Bitmap bitmap = mMemoryCache.get(url);
return bitmap;

}

/**
* 往内存中写图片
* @param url
* @param bitmap
*/
public void setBitmapToMemory(String url, Bitmap bitmap) {
//mMemoryCache.put(url, bitmap);//1.强引用方法
/*2.弱引用方法
mMemoryCache.put(url, new SoftReference< (bitmap));
*/
mMemoryCache.put(url,bitmap);
}
}

+ 118
- 0
app/src/main/java/com/bonait/bnframework/common/image/utils/NetCacheUtils.java View File

@@ -0,0 +1,118 @@
package com.bonait.bnframework.common.image.utils;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
* 三级缓存之网络缓存
*/
public class NetCacheUtils {

private LocalCacheUtils mLocalCacheUtils;
private MemoryCacheUtils mMemoryCacheUtils;

public NetCacheUtils(LocalCacheUtils localCacheUtils, MemoryCacheUtils memoryCacheUtils) {
mLocalCacheUtils = localCacheUtils;
mMemoryCacheUtils = memoryCacheUtils;
}

/**
* 从网络下载图片
* @param ivPic 显示图片的imageview
* @param url 下载图片的网络地址
*/
public void getBitmapFromNet(ImageView ivPic, String url) {
new BitmapTask().execute(ivPic, url);//启动AsyncTask

}

/**
* AsyncTask就是对handler和线程池的封装
* 第一个泛型:参数类型
* 第二个泛型:更新进度的泛型
* 第三个泛型:onPostExecute的返回结果
*/
class BitmapTask extends AsyncTask<Object, Void, Bitmap> {

private ImageView ivPic;
private String url;

/**
* 后台耗时操作,存在于子线程中
* @param params
* @return
*/
@Override
protected Bitmap doInBackground(Object[] params) {
ivPic = (ImageView) params[0];
url = (String) params[1];

return downLoadBitmap(url);
}

/**
* 更新进度,在主线程中
* @param values
*/
@Override
protected void onProgressUpdate(Void[] values) {
super.onProgressUpdate(values);
}

/**
* 耗时方法结束后执行该方法,主线程中
* @param result
*/
@Override
protected void onPostExecute(Bitmap result) {
if (result != null) {
ivPic.setImageBitmap(result);
System.out.println("从网络缓存图片啦.....");

//从网络获取图片后,保存至本地缓存
mLocalCacheUtils.setBitmapToLocal(url, result);
//保存至内存中
mMemoryCacheUtils.setBitmapToMemory(url, result);

}
}
}

/**
* 网络下载图片
* @param url
* @return
*/
private Bitmap downLoadBitmap(String url) {
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setConnectTimeout(10000);//超时时间为10秒
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");//使用GET方法访问网络

int responseCode = conn.getResponseCode();
if (responseCode == 200) {
//图片压缩
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=2;//宽高压缩为原来的1/2
options.inPreferredConfig=Bitmap.Config.ARGB_4444;
Bitmap bitmap = BitmapFactory.decodeStream(conn.getInputStream(),null,options);
return bitmap;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
conn.disconnect();
}

return null;
}
}

+ 4
- 7
app/src/main/java/com/bonait/bnframework/modules/home/fragment/from/DiyActivity.java View File

@@ -243,14 +243,11 @@ public class DiyActivity extends BaseActivity implements MyClickListener {
* @param arrayList * @param arrayList
*/ */
private void Drawable_Get(List arrayList) { private void Drawable_Get(List arrayList) {
//从drawable文件夹下获取到事先准备的图片,在这里演示三张图片
Drawable drawable = getResources().getDrawable(R.mipmap.hgr);
Drawable drawable1 = getResources().getDrawable(R.mipmap.hgr);
Drawable drawable2 = getResources().getDrawable(R.mipmap.hgr);
//把他们存放到一个list集合中 //把他们存放到一个list集合中
arrayList.add(drawable);
arrayList.add(drawable1);
arrayList.add(drawable2);
arrayList.add(getResources().getDrawable(R.mipmap.image1));
arrayList.add(getResources().getDrawable(R.mipmap.image2));
arrayList.add(getResources().getDrawable(R.mipmap.image3));
arrayList.add(getResources().getDrawable(R.mipmap.image4));
//调用轮播图设置方法 //调用轮播图设置方法
Banner_Set(Banner_list); Banner_Set(Banner_list);
} }


+ 4
- 8
app/src/main/java/com/bonait/bnframework/modules/home/fragment/from/DiyUpdateActivity.java View File

@@ -271,14 +271,10 @@ public class DiyUpdateActivity extends BaseActivity {
* @param arrayList * @param arrayList
*/ */
private void Drawable_Get(List arrayList) { private void Drawable_Get(List arrayList) {
//从drawable文件夹下获取到事先准备的图片,在这里演示三张图片
Drawable drawable = getResources().getDrawable(R.mipmap.hgr);
Drawable drawable1 = getResources().getDrawable(R.mipmap.hgr);
Drawable drawable2 = getResources().getDrawable(R.mipmap.hgr);
//把他们存放到一个list集合中
arrayList.add(drawable);
arrayList.add(drawable1);
arrayList.add(drawable2);
arrayList.add(getResources().getDrawable(R.mipmap.image1));
arrayList.add(getResources().getDrawable(R.mipmap.image2));
arrayList.add(getResources().getDrawable(R.mipmap.image3));
arrayList.add(getResources().getDrawable(R.mipmap.image4));
//调用轮播图设置方法 //调用轮播图设置方法
Banner_Set(Banner_list); Banner_Set(Banner_list);
} }


+ 6
- 2
app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/QDListSectionAdapter.java View File

@@ -13,6 +13,7 @@ import android.widget.TextView;
import com.bonait.bnframework.R; import com.bonait.bnframework.R;
import com.bonait.bnframework.common.constant.MessageName; import com.bonait.bnframework.common.constant.MessageName;
import com.bonait.bnframework.common.helper.I.MyClickListener; import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.image.MyBitmapUtils;
import com.bonait.bnframework.common.message.MessageManager; import com.bonait.bnframework.common.message.MessageManager;
import com.bonait.bnframework.common.utils.AlertDialogUtils; import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.bonait.bnframework.common.utils.ToastUtils; import com.bonait.bnframework.common.utils.ToastUtils;
@@ -24,12 +25,14 @@ import com.qmuiteam.qmui.widget.section.QMUISection;
public class QDListSectionAdapter extends QMUIDefaultStickySectionAdapter { public class QDListSectionAdapter extends QMUIDefaultStickySectionAdapter {
private TextView tvTag ,tvNote,tvAccount,Sc_text,delete_text; private TextView tvTag ,tvNote,tvAccount,Sc_text,delete_text;


private RelativeLayout ImageUrl;//图片
private ImageView ImageUrl;//图片


private ImageView sc_image;//是否收藏 private ImageView sc_image;//是否收藏


public boolean IsSC=false; public boolean IsSC=false;


//图标
private MyBitmapUtils myBitmapUtils=new MyBitmapUtils();
/** /**
* 点击事件 * 点击事件
*/ */
@@ -75,7 +78,8 @@ public class QDListSectionAdapter extends QMUIDefaultStickySectionAdapter {
tvTag.setText(name); tvTag.setText(name);
tvNote.setText("时间:"+ ((SectionItem)section.getItemAt(itemIndex)).getNote()+"秒"); tvNote.setText("时间:"+ ((SectionItem)section.getItemAt(itemIndex)).getNote()+"秒");
//设置图片 //设置图片
ImageUrl.setBackground(holder.itemView.getContext().getResources().getDrawable(R.mipmap.hgr));
//ImageUrl.setBackground(holder.itemView.getContext().getResources().getDrawable(R.mipmap.hgr));
myBitmapUtils.disPlay(ImageUrl,"https://hbl-test-1305371387.cos.ap-chengdu.myqcloud.com/Franchisee/001/goods/133329989655737022.jpg");
//上传按钮点击 //上传按钮点击
Sc_text.setOnClickListener(new View.OnClickListener() { Sc_text.setOnClickListener(new View.OnClickListener() {
@Override @Override


+ 1
- 1
app/src/main/res/layout/item/layout/note_rv_tree_second.xml View File

@@ -13,7 +13,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
<RelativeLayout
<ImageView
android:id="@+id/ImageUrl" android:id="@+id/ImageUrl"
android:layout_margin="3dp" android:layout_margin="3dp"
android:layout_width="match_parent" android:layout_width="match_parent"


BIN
View File


BIN
View File


BIN
View File


BIN
View File


BIN
View File


BIN
View File


BIN
View File


BIN
View File


Loading…
Cancel
Save