Commit 49394bb6 by gao.chao

picasso升级

parent cf5feab0
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" android:theme="@style/Theme.AppCompat.Light.NoActionBar"
tools:replace="android:appComponentFactory"> tools:replace="android:appComponentFactory">
......
...@@ -40,7 +40,7 @@ public abstract class BaseStartActivity<T extends BaseActivity, T1 extends BaseA ...@@ -40,7 +40,7 @@ public abstract class BaseStartActivity<T extends BaseActivity, T1 extends BaseA
gotoActivityAndFinish(getLoginActivity()); gotoActivityAndFinish(getLoginActivity());
} else { } else {
// 有本地缓存 免登录 直接到首页 // 有本地缓存 免登录 直接到首页
UserManager.INSTANCE.getUserInfo(); // UserManager.INSTANCE.getUserInfo();
gotoActivityAndFinish(getMainActivity()); gotoActivityAndFinish(getMainActivity());
} }
} }
......
package com.mayi.demo.page //package com.mayi.demo.page
//
import android.text.TextUtils //import android.text.TextUtils
//
import com.alibaba.fastjson.JSON //import com.alibaba.fastjson.JSON
import com.mayi.fastdevelop.comnon.Key //import com.mayi.fastdevelop.comnon.Key
import com.mayi.fastdevelop.util.SpUtil //import com.mayi.fastdevelop.util.SpUtil
//
object UserManager { //object UserManager {
//
private var userInfo: UserInfo? = null // private var userInfo: UserInfo? = null
//
/** // /**
* 获取本地的用户信息 // * 获取本地的用户信息
* // *
* @return 用户信息 // * @return 用户信息
*/ // */
fun getUserInfo(): UserInfo? { // fun getUserInfo(): UserInfo? {
if (userInfo == null) { // if (userInfo == null) {
synchronized(UserManager::class.java) { // synchronized(UserManager::class.java) {
if (userInfo == null) { // if (userInfo == null) {
val s = SpUtil.get(Key.USER_INFO, "") // val s = SpUtil.get(Key.USER_INFO, "")
if (!TextUtils.isEmpty(s)) { // if (!TextUtils.isEmpty(s)) {
userInfo = JSON.parseObject(s, UserInfo::class.java) // userInfo = JSON.parseObject(s, UserInfo::class.java)
} // }
} // }
} // }
} // }
return userInfo // return userInfo
} // }
//
/** // /**
* 设置用户信息 // * 设置用户信息
* // *
* @param json // * @param json
*/ // */
fun setUserInfo(json: String) { // fun setUserInfo(json: String) {
if (!TextUtils.isEmpty(json)) { // if (!TextUtils.isEmpty(json)) {
synchronized(UserManager::class.java) { // synchronized(UserManager::class.java) {
SpUtil.set(Key.USER_INFO, json) // SpUtil.set(Key.USER_INFO, json)
userInfo = JSON.parseObject(json, UserInfo::class.java) // userInfo = JSON.parseObject(json, UserInfo::class.java)
} // }
} // }
} // }
//
/** // /**
* 设置用户信息 // * 设置用户信息
* // *
* @param userInfo // * @param userInfo
*/ // */
fun setUserInfo(userInfo: UserInfo?) { // fun setUserInfo(userInfo: UserInfo?) {
if (userInfo != null) { // if (userInfo != null) {
synchronized(UserManager::class.java) { // synchronized(UserManager::class.java) {
SpUtil.set(Key.USER_INFO, JSON.toJSONString(userInfo)) // SpUtil.set(Key.USER_INFO, JSON.toJSONString(userInfo))
UserManager.userInfo = userInfo // UserManager.userInfo = userInfo
} // }
} // }
} // }
//
/** // /**
* 清除用户信息 // * 清除用户信息
*/ // */
fun clearUserInfo() { // fun clearUserInfo() {
synchronized(UserManager::class.java) { // synchronized(UserManager::class.java) {
SpUtil.set(Key.USER_INFO, null) // SpUtil.set(Key.USER_INFO, null)
userInfo = null // userInfo = null
} // }
} // }
} //}
...@@ -11,6 +11,7 @@ import android.graphics.drawable.Drawable; ...@@ -11,6 +11,7 @@ import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Environment; import android.os.Environment;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import android.view.View; import android.view.View;
import com.mayi.fastdevelop.R; import com.mayi.fastdevelop.R;
...@@ -19,6 +20,7 @@ import com.squareup.picasso.Target; ...@@ -19,6 +20,7 @@ import com.squareup.picasso.Target;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
public class ImageUtil { public class ImageUtil {
...@@ -50,7 +52,7 @@ public class ImageUtil { ...@@ -50,7 +52,7 @@ public class ImageUtil {
} }
@Override @Override
public void onBitmapFailed(Drawable errorDrawable) { public void onBitmapFailed(Exception e, Drawable errorDrawable) {
Context c = weakReference.get(); Context c = weakReference.get();
if (c != null) { if (c != null) {
ToastUtil.show(c, c.getString(R.string.save_img_failed)); ToastUtil.show(c, c.getString(R.string.save_img_failed));
...@@ -61,7 +63,102 @@ public class ImageUtil { ...@@ -61,7 +63,102 @@ public class ImageUtil {
public void onPrepareLoad(Drawable placeHolderDrawable) { 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 { ...@@ -95,22 +192,6 @@ public class ImageUtil {
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
// 拷贝图片,否则在setDrawingCacheEnabled(false)以后该图片会被释放掉 // 拷贝图片,否则在setDrawingCacheEnabled(false)以后该图片会被释放掉
Bitmap cacheBitmap = view.getDrawingCache(); 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 matrix = new Matrix();
// 缩小 // 缩小
matrix.setScale(320f / cacheBitmap.getWidth(), 320f / cacheBitmap.getWidth()); matrix.setScale(320f / cacheBitmap.getWidth(), 320f / cacheBitmap.getWidth());
......
...@@ -18,9 +18,9 @@ public class LoadingPictures { ...@@ -18,9 +18,9 @@ public class LoadingPictures {
//加载圆角图片 //加载圆角图片
public static void loadUriCircleCornerForm(String url, ImageView img) { public static void loadUriCircleCornerForm(String url, ImageView img) {
if (TextUtils.isEmpty(url)) { 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 { } else {
Picasso.with(img.getContext()) Picasso.get()
.load(url) .load(url)
.resize(2000, 2000) .resize(2000, 2000)
.onlyScaleDown() // 如果图片规格大于2000*2000,将只会被resize .onlyScaleDown() // 如果图片规格大于2000*2000,将只会被resize
...@@ -34,9 +34,9 @@ public class LoadingPictures { ...@@ -34,9 +34,9 @@ public class LoadingPictures {
//加载圆形图片 //加载圆形图片
public static void loadUriCircleTransform(String url, ImageView img) { public static void loadUriCircleTransform(String url, ImageView img) {
if (TextUtils.isEmpty(url)) { 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 { } else {
Picasso.with(img.getContext()) Picasso.get()
.load(url) .load(url)
.resize(2000, 2000) .resize(2000, 2000)
.onlyScaleDown() // 如果图片规格大于2000*2000,将只会被resize .onlyScaleDown() // 如果图片规格大于2000*2000,将只会被resize
...@@ -50,9 +50,9 @@ public class LoadingPictures { ...@@ -50,9 +50,9 @@ public class LoadingPictures {
//加载网络图片 //加载网络图片
public static void loadUri(String url, ImageView img) { public static void loadUri(String url, ImageView img) {
if (TextUtils.isEmpty(url)) { if (TextUtils.isEmpty(url)) {
Picasso.with(img.getContext()).load(error).into(img); Picasso.get().load(error).into(img);
} else { } else {
Picasso.with(img.getContext()) Picasso.get()
.load(url) .load(url)
.resize(2000, 2000) .resize(2000, 2000)
.onlyScaleDown() // 如果图片规格大于2000*2000,将只会被resize .onlyScaleDown() // 如果图片规格大于2000*2000,将只会被resize
...@@ -65,9 +65,9 @@ public class LoadingPictures { ...@@ -65,9 +65,9 @@ public class LoadingPictures {
//加载文件图片 //加载文件图片
public static void loadFile(File file, ImageView img) { public static void loadFile(File file, ImageView img) {
if (file == null) { if (file == null) {
Picasso.with(img.getContext()).load(error).into(img); Picasso.get().load(error).into(img);
} else { } else {
Picasso.with(img.getContext()) Picasso.get()
.load(file) .load(file)
.resize(2000, 2000) .resize(2000, 2000)
.placeholder(placeholder) .placeholder(placeholder)
......
...@@ -16,13 +16,13 @@ public class PicassoImageLoader extends ImageLoader { ...@@ -16,13 +16,13 @@ public class PicassoImageLoader extends ImageLoader {
@Override @Override
public void displayImage(Context context, Object path, ImageView imageView) { public void displayImage(Context context, Object path, ImageView imageView) {
if (path instanceof String) { if (path instanceof String) {
Picasso.with(context).load((String) path).into(imageView); Picasso.get().load((String) path).into(imageView);
} else if (path instanceof Integer) { } 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) { } 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) { } 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