导包
compile
'de.hdodenhof:circleimageview:2.1.0'
加载它的工具
package com.sxzqsc.cfzone.utils;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.signature.StringSignature;
import com.sxzqsc.cfzone.AppContext;
import com.sxzqsc.cfzone.R;
import com.sxzqsc.cfzone.view.GlideCircleTransform;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by Administrator on 2017/2/9.
*/
public class BitmapHelper {
public static void displayImageCommonPlaceHolder(ImageView imageView,String url){
try {
Glide.with(UIUtils.getContext()).load(url).placeholder(R.drawable.common_loading).error(R.drawable.common_error).into(imageView);
}
catch (Exception e) {
e.printStackTrace();
LogUtils.d(
"加载图片出错"+e.toString());
}
}
public static void displayImageNoCache(ImageView imageView,String url,String sign){
try{
Glide.with(UIUtils.getContext())
.load(url)
.signature(
new StringSignature(sign)).
into(imageView);
}
catch (Exception e){
e.printStackTrace();
LogUtils.d(
"加载图片出错" + e.toString());
}
}
public static void displayImage(ImageView imageView, String url){
try{
Glide.with(UIUtils.getContext())
.load(url).
into(imageView);
}
catch (Exception e){
e.printStackTrace();
LogUtils.d(
"加载图片出错" + e.toString());
}
}
public static void displayHeadImage(ImageView circleImageView ,String url){
try{
Glide.with(UIUtils.getContext())
.load(url)
.dontAnimate()
.placeholder(R.drawable.default_avatar)
.error(R.drawable.default_avatar)
.transform(
new GlideCircleTransform(UIUtils.getContext()))
.into(circleImageView);
}
catch (Exception e){
e.printStackTrace();
LogUtils.d(
"加载头像图片出错" + e.toString());
}
}
public static void displayHappyLiveImage(ImageView imageView, String filePath){
try {
Glide.with(UIUtils.getContext()).load(filePath).placeholder(R.drawable.common_loading_happy).error(R.drawable.common_error_happy).into(imageView);
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void compressBmpFromBmp(Bitmap image, File file) {
ByteArrayOutputStream baos =
new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG,
80, baos);
try {
FileOutputStream fos =
new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void displayNewsBigImage(ImageView imageView, String url,
int placeholder,
int resourceError) {
try {
Glide.with(UIUtils.getContext()).load(url).placeholder(placeholder).error(resourceError).into(imageView);
}
catch (Exception e) {
e.printStackTrace();
LogUtils.d(
"加载图片出错" + e.toString());
}
}
public static void displayHeadImageBig(ImageView imageView, String url) {
try {
Glide.with(UIUtils.getContext()).load(url).centerCrop().placeholder(R.drawable.default_avatar).into(imageView);
}
catch (Exception e) {
e.printStackTrace();
LogUtils.d(
"加载图片出错" + e.toString());
}
}
public static void displayNewsSmallImage(ImageView imageView, String url) {
try {
Glide.with(UIUtils.getContext()).load(url).placeholder(R.drawable.common_news_loading_small).error(R.drawable.common_news_error_small).into(imageView);
}
catch (Exception e) {
e.printStackTrace();
LogUtils.d(
"加载图片出错" + e.toString());
}
}
public static Bitmap
getViewBitmap_ARGB565(View view,
int width,
int height){
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
view.measure(View.MeasureSpec.makeMeasureSpec(
0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(
0, View.MeasureSpec.UNSPECIFIED));
view.layout(
0,
0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.draw(
new Canvas(bitmap));
return bitmap;
}
public static void saveImageToGallery(
final Context context,
final String url){
if(TextUtils.isEmpty(url)){
UIHelper.showToast(context,
"无效图片");
return ;
}
Glide.with(context).load(url).asBitmap().into(
new SimpleTarget<Bitmap>() {
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
UIHelper.showToast(context,
"无效图片");
}
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<?
super Bitmap> glideAnimation) {
saveBitmapToGallery(context,resource,url);
}
});
}
public static void saveBitmapToGallery(Context context, Bitmap bmp, String url) {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(context,
"未找到sd卡,图片无法存储", Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(url) || bmp ==
null || bmp.getWidth() <=
0 || bmp.getHeight() <=
0){
Toast.makeText(context,
"无效的图片", Toast.LENGTH_SHORT).show();
return;
}
File appDir =
new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() +
"/chuangfenzone",
"chuangfen_image");
if (!appDir.exists()) {
appDir.mkdirs();
}
String imageName = url.substring(url.lastIndexOf(
"/"), url.length());
File file =
new File(appDir, imageName);
try {
FileOutputStream fos =
new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG,
100, fos);
fos.flush();
fos.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
context.sendBroadcast(
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
Toast.makeText(context,
"图片已保存至" + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
}
public static Bitmap
ImageRotate(String filePath) {
Bitmap bm = BitmapFactory.decodeFile(filePath);
ExifInterface exif =
null;
try {
exif =
new ExifInterface(filePath);
}
catch (IOException e) {
e.printStackTrace();
exif =
null;
}
int degree =
0;
if (exif !=
null) {
int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Log.d(
"rotate",
"ImageRotate: " + ori);
switch (ori) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree =
90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree =
180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree =
270;
break;
default:
degree =
0;
break;
}
}
if (degree !=
0) {
Matrix m =
new Matrix();
m.postRotate(degree);
return Bitmap.createBitmap(bm,
0,
0, bm.getWidth(),
bm.getHeight(), m,
true);
}
return null;
}
public static File
saveBitmapFile(Bitmap bitmap, String fileName) {
File file =
new File(fileName);
try {
BufferedOutputStream bos =
new BufferedOutputStream(
new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG,
100, bos);
bos.flush();
bos.close();
}
catch (IOException e) {
e.printStackTrace();
}
return file;
}
/** 保存方法 */
public static void saveBitmap(Context context ,String url) {
try{
Glide.with(AppContext.getContext()).load(url).asBitmap().into(
new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap bm, GlideAnimation<?
super Bitmap> glideAnimation) {
if(bm !=
null) {
LogUtils.d(
"保存图片");
File f =
new File(
"/sdcard",
"cfzoneshare.jpg");
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out =
new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG,
90, out);
out.flush();
out.close();
LogUtils.d(
"已经保存");
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
else{
LogUtils.d(
"保存图片bitmap为空");
}
}
});
}
catch (Exception e){
e.printStackTrace();
}
}
}