Commit 49394bb6 by gao.chao

picasso升级

parent cf5feab0
......@@ -25,6 +25,7 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
tools:replace="android:appComponentFactory">
......
......@@ -40,7 +40,7 @@ public abstract class BaseStartActivity<T extends BaseActivity, T1 extends BaseA
gotoActivityAndFinish(getLoginActivity());
} else {
// 有本地缓存 免登录 直接到首页
UserManager.INSTANCE.getUserInfo();
// UserManager.INSTANCE.getUserInfo();
gotoActivityAndFinish(getMainActivity());
}
}
......
package com.mayi.demo.page
import android.text.TextUtils
import com.alibaba.fastjson.JSON
import com.mayi.fastdevelop.comnon.Key
import com.mayi.fastdevelop.util.SpUtil
object UserManager {
private var userInfo: UserInfo? = null
/**
* 获取本地的用户信息
*
* @return 用户信息
*/
fun getUserInfo(): UserInfo? {
if (userInfo == null) {
synchronized(UserManager::class.java) {
if (userInfo == null) {
val s = SpUtil.get(Key.USER_INFO, "")
if (!TextUtils.isEmpty(s)) {
userInfo = JSON.parseObject(s, UserInfo::class.java)
}
}
}
}
return userInfo
}
/**
* 设置用户信息
*
* @param json
*/
fun setUserInfo(json: String) {
if (!TextUtils.isEmpty(json)) {
synchronized(UserManager::class.java) {
SpUtil.set(Key.USER_INFO, json)
userInfo = JSON.parseObject(json, UserInfo::class.java)
}
}
}
/**
* 设置用户信息
*
* @param userInfo
*/
fun setUserInfo(userInfo: UserInfo?) {
if (userInfo != null) {
synchronized(UserManager::class.java) {
SpUtil.set(Key.USER_INFO, JSON.toJSONString(userInfo))
UserManager.userInfo = userInfo
}
}
}
/**
* 清除用户信息
*/
fun clearUserInfo() {
synchronized(UserManager::class.java) {
SpUtil.set(Key.USER_INFO, null)
userInfo = null
}
}
}
//package com.mayi.demo.page
//
//import android.text.TextUtils
//
//import com.alibaba.fastjson.JSON
//import com.mayi.fastdevelop.comnon.Key
//import com.mayi.fastdevelop.util.SpUtil
//
//object UserManager {
//
// private var userInfo: UserInfo? = null
//
// /**
// * 获取本地的用户信息
// *
// * @return 用户信息
// */
// fun getUserInfo(): UserInfo? {
// if (userInfo == null) {
// synchronized(UserManager::class.java) {
// if (userInfo == null) {
// val s = SpUtil.get(Key.USER_INFO, "")
// if (!TextUtils.isEmpty(s)) {
// userInfo = JSON.parseObject(s, UserInfo::class.java)
// }
// }
// }
// }
// return userInfo
// }
//
// /**
// * 设置用户信息
// *
// * @param json
// */
// fun setUserInfo(json: String) {
// if (!TextUtils.isEmpty(json)) {
// synchronized(UserManager::class.java) {
// SpUtil.set(Key.USER_INFO, json)
// userInfo = JSON.parseObject(json, UserInfo::class.java)
// }
// }
// }
//
// /**
// * 设置用户信息
// *
// * @param userInfo
// */
// fun setUserInfo(userInfo: UserInfo?) {
// if (userInfo != null) {
// synchronized(UserManager::class.java) {
// SpUtil.set(Key.USER_INFO, JSON.toJSONString(userInfo))
// UserManager.userInfo = userInfo
// }
// }
// }
//
// /**
// * 清除用户信息
// */
// fun clearUserInfo() {
// synchronized(UserManager::class.java) {
// SpUtil.set(Key.USER_INFO, null)
// userInfo = null
// }
// }
//}
......@@ -11,6 +11,7 @@ import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import com.mayi.fastdevelop.R;
......@@ -19,6 +20,7 @@ import com.squareup.picasso.Target;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;
public class ImageUtil {
......@@ -50,7 +52,7 @@ public class ImageUtil {
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
Context c = weakReference.get();
if (c != null) {
ToastUtil.show(c, c.getString(R.string.save_img_failed));
......@@ -61,7 +63,102 @@ public class ImageUtil {
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Picasso.with(context).load(url).into(target);
Picasso.get().load(url).into(target);
}
/**
* 保存网络图片
*
* @param context
* @param url
* @param fileName
* @param showTips 是否提示信息
*/
public static void saveBitmap(Context context, final String url, final String fileName, final boolean showTips) {
if (TextUtils.isEmpty(url)) {
if (showTips) {
ToastUtil.show(context, context.getString(R.string.save_img_failed_2));
}
return;
}
final WeakReference<Context> weakReference = new WeakReference<>(context);
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Context c = weakReference.get();
if (c != null) {
File file = new File(Environment.getExternalStorageDirectory().toString() + "/" + c.getPackageName());
if (!file.exists()) {
file.mkdir();
}
File newFile = new File(file,"/"+fileName);
Log.i("imageUrl","newFile:"+newFile.getAbsolutePath());
if (newFile.exists()){
newFile.delete();
}
BitmapUtil.saveBitmapToFormat(bitmap, file.toString() + "/" + fileName);
//刷新相册
c.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString() + "/" + c.getPackageName()))));
if (showTips) {
ToastUtil.show(c, c.getString(R.string.save_img_success));
}
}
}
@Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
Context c = weakReference.get();
if (c != null) {
if (showTips) {
ToastUtil.show(c, c.getString(R.string.save_img_failed));
}
}
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Picasso.get().load(url).into(target);
}
public static void saveBitmapNew(final Context context, final String url, final String fileName, final boolean showTips) {
if (TextUtils.isEmpty(url)) {
if (showTips) {
ToastUtil.show(context, context.getString(R.string.save_img_failed_2));
}
return;
}
new Thread(new Runnable() {
@Override
public void run() {
try {
Bitmap b = Picasso.get().load(url).get();
File file = new File(Environment.getExternalStorageDirectory().toString() + "/" + context.getPackageName());
if (!file.exists()) {
file.mkdir();
}
File newFile = new File(file,"/"+fileName);
Log.i("imageUrl","newFile:"+newFile.getAbsolutePath());
if (newFile.exists()){
newFile.delete();
}
BitmapUtil.saveBitmapToFormat(b, file.toString() + "/" + fileName);
//刷新相册
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(file)));
if (showTips) {
ToastUtil.show(context, context.getString(R.string.save_img_success));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
......@@ -95,22 +192,6 @@ public class ImageUtil {
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
// 拷贝图片,否则在setDrawingCacheEnabled(false)以后该图片会被释放掉
Bitmap cacheBitmap = view.getDrawingCache();
//方案1 (图片显示模糊)
// image = Bitmap.createBitmap(cacheBitmap);
//方案2 (图片显示模糊)
// float scale = 320f / cacheBitmap.getWidth();
// int height = (int) (cacheBitmap.getHeight() * scale);
// image = Bitmap.createScaledBitmap(cacheBitmap, 320, height, true);
//方案3
// image = scaleImageCavans(cacheBitmap, DensityUtil.dip2px(view.getContext(), 320), DensityUtil.dip2px(view.getContext(), 240));
// image = scaleImageCavans(cacheBitmap, 320, 240);
//方案4
// Matrix类进行图片处理(缩放)
Matrix matrix = new Matrix();
// 缩小
matrix.setScale(320f / cacheBitmap.getWidth(), 320f / cacheBitmap.getWidth());
......
......@@ -18,9 +18,9 @@ public class LoadingPictures {
//加载圆角图片
public static void loadUriCircleCornerForm(String url, ImageView img) {
if (TextUtils.isEmpty(url)) {
Picasso.with(img.getContext()).load(error).transform(new CircleCornerForm()).into(img);
Picasso.get().load(error).transform(new CircleCornerForm()).into(img);
} else {
Picasso.with(img.getContext())
Picasso.get()
.load(url)
.resize(2000, 2000)
.onlyScaleDown() // 如果图片规格大于2000*2000,将只会被resize
......@@ -34,9 +34,9 @@ public class LoadingPictures {
//加载圆形图片
public static void loadUriCircleTransform(String url, ImageView img) {
if (TextUtils.isEmpty(url)) {
Picasso.with(img.getContext()).load(error).transform(new CircleTransform()).into(img);
Picasso.get().load(error).transform(new CircleTransform()).into(img);
} else {
Picasso.with(img.getContext())
Picasso.get()
.load(url)
.resize(2000, 2000)
.onlyScaleDown() // 如果图片规格大于2000*2000,将只会被resize
......@@ -50,9 +50,9 @@ public class LoadingPictures {
//加载网络图片
public static void loadUri(String url, ImageView img) {
if (TextUtils.isEmpty(url)) {
Picasso.with(img.getContext()).load(error).into(img);
Picasso.get().load(error).into(img);
} else {
Picasso.with(img.getContext())
Picasso.get()
.load(url)
.resize(2000, 2000)
.onlyScaleDown() // 如果图片规格大于2000*2000,将只会被resize
......@@ -65,9 +65,9 @@ public class LoadingPictures {
//加载文件图片
public static void loadFile(File file, ImageView img) {
if (file == null) {
Picasso.with(img.getContext()).load(error).into(img);
Picasso.get().load(error).into(img);
} else {
Picasso.with(img.getContext())
Picasso.get()
.load(file)
.resize(2000, 2000)
.placeholder(placeholder)
......
......@@ -16,13 +16,13 @@ public class PicassoImageLoader extends ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
if (path instanceof String) {
Picasso.with(context).load((String) path).into(imageView);
Picasso.get().load((String) path).into(imageView);
} else if (path instanceof Integer) {
Picasso.with(context).load((Integer) path).into(imageView);
Picasso.get().load((Integer) path).into(imageView);
} else if (path instanceof Uri) {
Picasso.with(context).load((Uri) path).into(imageView);
Picasso.get().load((Uri) path).into(imageView);
} else if (path instanceof File) {
Picasso.with(context).load((File) path).into(imageView);
Picasso.get().load((File) path).into(imageView);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment