admin
2021-05-11 e5ca87e89fef96c827ec37d1d91082f626cbb17d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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);
    }
 
}