package com.tejia.lijin.app.util;
|
|
import android.content.Context;
|
import android.graphics.Bitmap;
|
import android.graphics.BitmapFactory;
|
import android.graphics.Matrix;
|
import android.graphics.drawable.Drawable;
|
import android.text.Html;
|
import android.text.Spannable;
|
import android.text.SpannableString;
|
import android.util.Log;
|
import android.widget.TextView;
|
|
import com.tejia.lijin.app.R;
|
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
|
public class ImageUtil {
|
/**
|
* 网络图片转换为Bitmap
|
*
|
* @Author: ChengBin
|
* @Time: 16/4/5 下午2:41
|
*/
|
public static Bitmap netPicToBmp(String src) {
|
try {
|
Log.d("FileUtil", src);
|
URL url = new URL(src);
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
connection.setDoInput(true);
|
connection.connect();
|
InputStream input = connection.getInputStream();
|
Bitmap myBitmap = BitmapFactory.decodeStream(input);
|
|
//设置固定大小
|
//需要的大小
|
float newWidth = 200f;
|
float newHeigth = 200f;
|
|
//图片大小
|
int width = myBitmap.getWidth();
|
int height = myBitmap.getHeight();
|
|
//缩放比例
|
float scaleWidth = newWidth / width;
|
float scaleHeigth = newHeigth / height;
|
Matrix matrix = new Matrix();
|
matrix.postScale(scaleWidth, scaleHeigth);
|
|
Bitmap bitmap = Bitmap.createBitmap(myBitmap, 0, 0, width, height, matrix, true);
|
return bitmap;
|
} catch (IOException e) {
|
// Log exception
|
return null;
|
}
|
}
|
|
/**
|
* textview首行前面添加图片
|
*
|
* @param mContext
|
* @param s
|
* @param shop
|
* @param tv
|
*/
|
public static void showImageFace(Context mContext, String s, int shop, TextView tv) {
|
// 创建一个SpannableString对象,以便插入用ImageSpan对象封装的图像
|
SpannableString spannableString;
|
VerticalImageSpan imageSpan;
|
if (shop == 11) {//天猫
|
imageSpan = new VerticalImageSpan(mContext, R.drawable.ic_tmall);
|
} else if (shop == 20 || shop == 21) {
|
imageSpan = new VerticalImageSpan(mContext, R.drawable.ic_jingdong);
|
} else if (shop == 30) {
|
imageSpan = new VerticalImageSpan(mContext, R.drawable.ic_pinduoduo);
|
}else if (shop == 40) {
|
imageSpan = new VerticalImageSpan(mContext, R.drawable.ic_vipshop);
|
}else if (shop == 50) {
|
imageSpan = new VerticalImageSpan(mContext, R.drawable.ic_suning);
|
} else {
|
imageSpan = new VerticalImageSpan(mContext, R.drawable.icon);
|
}
|
spannableString = new SpannableString(Html.fromHtml("## " + s));
|
// 用ImageSpan对象替换字符
|
spannableString.setSpan(imageSpan, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
tv.setText(spannableString);
|
}
|
|
|
public static void showImageFaceDialog(Context mContext, String s, int shop, TextView tv) {
|
// 创建一个SpannableString对象,以便插入用ImageSpan对象封装的图像
|
SpannableString spannableString;
|
VerticalImageSpan imageSpan;
|
if (shop == 11) {//天猫
|
Drawable myIcon = mContext.getResources().getDrawable(R.drawable.ic_tmall);
|
myIcon.setBounds(0, 0, 63 * tv.getLineHeight() / 31, tv.getLineHeight());
|
imageSpan = new VerticalImageSpan(myIcon);
|
} else if (shop == 20 || shop == 21) {
|
Drawable myIcon = mContext.getResources().getDrawable(R.drawable.ic_jingdong);
|
myIcon.setBounds(0, 0, 63 * tv.getLineHeight() / 31, tv.getLineHeight());
|
imageSpan = new VerticalImageSpan(myIcon);
|
} else if (shop == 30) {
|
Drawable myIcon = mContext.getResources().getDrawable(R.drawable.ic_pinduoduo);
|
myIcon.setBounds(0, 0, 86 * tv.getLineHeight() / 31, tv.getLineHeight());
|
imageSpan = new VerticalImageSpan(myIcon);
|
}else if (shop == 40) {
|
Drawable myIcon = mContext.getResources().getDrawable(R.drawable.ic_vipshop);
|
myIcon.setBounds(0, 0, 86 * tv.getLineHeight() / 31, tv.getLineHeight());
|
imageSpan = new VerticalImageSpan(myIcon);
|
}else if (shop == 50) {
|
Drawable myIcon = mContext.getResources().getDrawable(R.drawable.ic_suning);
|
myIcon.setBounds(0, 0, 109 * tv.getLineHeight() / 31, tv.getLineHeight());
|
imageSpan = new VerticalImageSpan(myIcon);
|
} else {
|
Drawable myIcon = mContext.getResources().getDrawable(R.drawable.icon);
|
myIcon.setBounds(0, 0, 63 * tv.getLineHeight() / 31, tv.getLineHeight());
|
imageSpan = new VerticalImageSpan(myIcon);
|
}
|
spannableString = new SpannableString(Html.fromHtml("## " + s));
|
// 用ImageSpan对象替换字符
|
spannableString.setSpan(imageSpan, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
tv.setText(spannableString);
|
}
|
|
|
/**
|
* textview首行前面添加图片
|
* 详情分享页面使用
|
*
|
* @param mContext
|
* @param s
|
* @param shop
|
* @param tv
|
*/
|
public static void showImageFace1(Context mContext, String s, int shop, TextView tv) {
|
// 创建一个SpannableString对象,以便插入用ImageSpan对象封装的图像
|
SpannableString spannableString;
|
VerticalImageSpan1 imageSpan;
|
Drawable drawable;
|
if (shop == 11) {//天猫
|
drawable = mContext.getResources().getDrawable(R.drawable.ic_tmall);
|
} else if (shop == 20 || shop == 21) {
|
drawable = mContext.getResources().getDrawable(R.drawable.ic_jingdong);
|
} else if (shop == 30) {
|
drawable = mContext.getResources().getDrawable(R.drawable.ic_pinduoduo);
|
} else if (shop == 40) {
|
drawable = mContext.getResources().getDrawable(R.drawable.ic_vipshop);
|
} else if (shop == 50) {
|
drawable = mContext.getResources().getDrawable(R.drawable.ic_suning);
|
} else {
|
drawable = mContext.getResources().getDrawable(R.drawable.icon);
|
}
|
tv.setIncludeFontPadding(false);
|
float scale = tv.getTextSize() / drawable.getIntrinsicHeight();//文字高度和图片高度百分比
|
drawable.setBounds(0, 0, (int) (drawable.getIntrinsicWidth() * scale)
|
, (int) tv.getTextSize());
|
imageSpan = new VerticalImageSpan1(drawable);
|
// if (shop == 11) {//天猫
|
// imageSpan = new VerticalImageSpan(mContext, R.drawable.ic_tmall);
|
// } else if (shop == 20||shop == 21) {
|
// imageSpan = new VerticalImageSpan(mContext, R.drawable.ic_jingdong);
|
// } else if (shop == 30) {
|
// imageSpan = new VerticalImageSpan(mContext, R.drawable.ic_pinduoduo);
|
// } else {
|
// imageSpan = new VerticalImageSpan(mContext, R.drawable.icon);
|
// }
|
spannableString = new SpannableString(Html.fromHtml("## " + s));
|
// 用ImageSpan对象替换字符
|
spannableString.setSpan(imageSpan, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
tv.setText(spannableString);
|
}
|
|
}
|