如何在使用ImageGetter时将图像置于TextView中心?

时间:2015-11-20 01:59:12

标签: android image textview

这是我的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;
        }
    }
}

图像: enter image description here

1 个答案:

答案 0 :(得分:0)

这是摘录的代码,这将帮助您通过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);
    }
}