android两个版本显示两个结果

时间:2014-12-31 10:07:31

标签: android picasso

首先,抱歉我的英语。

我是Android的新手,我有一个显示新闻的活动,内容来自网络服务器,内容包含图片。我使用TextView显示它,我希望图像适合sreen,所以我写了这些代码。

public class CustomImageGetter implements Html.ImageGetter {
    private Activity context;
    private Picasso picasso;
    private TextView textView;

public CustomImageGetter(Activity context, Picasso picasso, TextView textView) {
    this.context = context;
    this.picasso = picasso;
    this.textView = textView;
}

@Override
public Drawable getDrawable(final String source) {
    final PlaceHolderBitmapDrawable result = new PlaceHolderBitmapDrawable();
    new AsyncTask<Void, Void, Bitmap>() {

        @Override
        protected Bitmap doInBackground(Void... params) {
            try {
                return picasso
                        .load(Config.WEB_SERVER + source.substring(1))
                        .placeholder(R.drawable.placeholder)
                        .error(R.drawable.placeholder)
                        .get();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            try {
                BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap);
                DisplayMetrics metrics = new DisplayMetrics();
                context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
                int width = metrics.widthPixels;
                int height = width * bitmap.getHeight() / bitmap.getWidth();
                drawable.setBounds(0, 0, width, height);
                result.setDrawable(drawable);
                result.setBounds(0, 0, width, height);
                textView.setText(textView.getText());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }.execute((Void) null);
    return result;
}

private static class PlaceHolderBitmapDrawable extends BitmapDrawable {
    protected Drawable drawable;

    @Override
    public void draw(Canvas canvas) {
        if (drawable != null)
            drawable.draw(canvas);
    }

    public void setDrawable(Drawable drawable) {
        this.drawable = drawable;
    }
}
}

图片的宽度是正确的,但在android 4.4中高度太短,但在android L中是正常的。

左图在android 4.4中运行,右图在android L中运行。

我该如何解决问题?

0 个答案:

没有答案