【问题标题】:How can I center an image inside a TextView while using ImageGetter?使用 ImageGetter 时如何在 TextView 中居中显示图像?
【发布时间】:2016-02-22 09:54:42
【问题描述】:

这是我的 ImageGetter 的主要内容,它在 TextView 内似乎可以很好地调整大小,但它总是左对齐。有一些很棒的资源可以让我在resizing 和处理base64 上取得成功。我遇到的最后一个障碍似乎很简单,有没有办法让 TextView 中的图像居中?

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;
        }
    }
}

图片:

【问题讨论】:

    标签: android image textview


    【解决方案1】:

    这是截断的代码,这将帮助您将图像以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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      相关资源
      最近更新 更多