Android 使用ImageGetter时,如何将图像居中放置在TextView中?
这是我的ImageGetter的主要部分,它在TextView中的大小调整似乎很好,但是它总是左对齐的。有一些很好的资源可以帮助我达到现在的水平,也可以帮助我处理问题。我遇到的最后一个障碍似乎很直截了当,有没有办法在TextView中居中显示图像Android 使用ImageGetter时,如何将图像居中放置在TextView中?,android,image,textview,Android,Image,Textview,这是我的ImageGetter的主要部分,它在TextView中的大小调整似乎很好,但是它总是左对齐的。有一些很好的资源可以帮助我达到现在的水平,也可以帮助我处理问题。我遇到的最后一个障碍似乎很直截了当,有没有办法在TextView中居中显示图像 public Drawable getDrawable(String source) { Bitmap bitmap; if(source.matches("data:image.*base64.*")) { Strin
public Drawable getDrawable(String source) {
Bitmap bitmap;
if(source.matches("data:image.*base64.*")) {
String base_64_source = source.replaceAll("data:image.*base64", "");
byte[] data = Base64.decode(base_64_source, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Drawable image = new BitmapDrawable(context.getResources(), bitmap);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
return image;
} else {
URLDrawable urlDrawable = new URLDrawable();
ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask(urlDrawable);
asyncTask.execute(source);
return urlDrawable; //return reference to URLDrawable where We will change with actual image from the src tag
}
}
public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
URLDrawable urlDrawable;
public ImageGetterAsyncTask(URLDrawable d) {
this.urlDrawable = d;
}
@Override
protected Drawable doInBackground(String... params) {
String source = params[0];
return fetchDrawable(source);
}
@Override
protected void onPostExecute(Drawable result) {
urlDrawable.setBounds(0, 0, width, height);//set the correct bound according to the result from HTTP call
urlDrawable.drawable = result; //change the reference of the current drawable to the result from the HTTP call
URLImageParser.this.container.invalidate(); //redraw the image by invalidating the container
container.setText(container.getText());
}
public Drawable fetchDrawable(String urlString) {
try {
DisplayMetrics metrics;
new DisplayMetrics();
metrics = Resources.getSystem().getDisplayMetrics();
InputStream is = (InputStream) new URL(urlString).getContent();
Bitmap bmp = BitmapFactory.decodeStream(is);
Drawable drawable = new BitmapDrawable (context.getResources(), bmp);
//Need logic here to calculate maximum width of image vs height so it doesnt strech
int originalWidthScaled = (int) (drawable.getIntrinsicWidth() * metrics.density);
int originalHeightScaled = (int) (drawable.getIntrinsicHeight() * metrics.density);
if (originalWidthScaled > (metrics.widthPixels * 70) / 100) {
width = (metrics.widthPixels * 70) / 100;
height = drawable.getIntrinsicHeight() * width
/ drawable.getIntrinsicWidth();
}else {
height = originalHeightScaled;
width = originalWidthScaled;
}
drawable.setBounds(0, 0, width, height);
return drawable;
} catch (Exception e) {
return null;
}
}
}
publicDrawable-getDrawable(字符串源){
位图;
if(source.matches(“数据:image.*base64.*)){
字符串base_64_source=source.replaceAll(“数据:image.*base64”,”);
字节[]数据=Base64.decode(Base64\u源,Base64.DEFAULT);
位图=位图工厂.decodeByteArray(数据,0,数据.length);
Drawable image=新的BitmapDrawable(context.getResources(),位图);
setBounds(0,0,image.getIntrinsicWidth(),image.getIntrinsicHeight());
返回图像;
}否则{
URLDAWABLE URLDAWABLE=新的URLDAWABLE();
ImageGetterAsyncTask asyncTask=新ImageGetterAsyncTask(URLDawable);
asyncTask.execute(源代码);
return urlDrawable;//返回对urlDrawable的引用,其中我们将使用src标记中的实际图像进行更改
}
}
公共类ImageGetterAsyncTask扩展异步任务{
URLDrawable URLDrawable;
公共ImageGetterAsyncTask(URLDawable d){
this.urlDrawable=d;
}
@凌驾
受保护的可抽出式doInBackground(字符串…参数){
字符串源=参数[0];
返回可提取的(源);
}
@凌驾
受保护的void onPostExecute(可提取结果){
urlDrawable.setBounds(0,0,宽度,高度);//根据HTTP调用的结果设置正确的边界
urlDrawable.drawable=result;//将当前drawable的引用更改为HTTP调用的结果
URLImageParser.this.container.invalidate();//通过使容器无效来重新绘制图像
container.setText(container.getText());
}
公共可提取可提取(字符串urlString){
试一试{
显示度量;
新的DisplayMetrics();
metrics=Resources.getSystem().getDisplayMetrics();
InputStream为=(InputStream)新URL(urlString).getContent();
位图bmp=BitmapFactory.decodeStream(is);
Drawable Drawable=新的BitmapDrawable(context.getResources(),bmp);
//这里需要逻辑来计算图像的最大宽度和高度,这样它就不会拉伸
int originalWidthScaled=(int)(drawable.getIntrinsicWidth()*metrics.density);
int originalHeightScaled=(int)(drawable.getIntrinsicHeight()*metrics.density);
如果(原始宽度缩放>(metrics.widthPixels*70)/100){
宽度=(metrics.widthPixels*70)/100;
高度=可绘制。getIntrinsicHeight()*宽度
/getIntrinsicWidth();
}否则{
高度=原始高度比例;
宽度=原始宽度比例;
}
可拉伸立根(0,0,宽度,高度);
回拉;
}捕获(例外e){
返回null;
}
}
}
图片:
这是截取的代码,这将帮助您通过
Html.ImageGetter
将图像居中
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (bitmap != null) {
LevelListDrawable mDrawable = (LevelListDrawable) params[1];
TextView textView = (TextView) params[2];
BitmapDrawable d = new BitmapDrawable(((Context)params[3]).getResources(),bitmap);
mDrawable.addLevel(1, 1, d);
// [ Start : Centering the image by calculating Display width and Image width
WindowManager wm = (WindowManager) ((Context)params[3]).getSystemService(WINDOW_SERVICE);
final DisplayMetrics displayMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
int totalGap = width - bitmap.getWidth()-120/*My Text view Padding*/;
mDrawable.setBounds(totalGap/=2, 0, bitmap.getWidth()+totalGap, bitmap.getHeight());
mDrawable.setLevel(1);
// now image will show in center :End ]
// i don't know yet a better way to refresh TextView
// mTv.invalidate() doesn't work as expected
CharSequence text = textView.getText();
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(text);
}
}