Html.ImageGetter TextView

前端 未结 5 1690
猫巷女王i
猫巷女王i 2020-11-27 17:58

So Im using ImageGetter to display the images from JSON blog posts. Im getting the correct source in the log but the URL changes when it reaches setBounds. Any ideas?

<
5条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 18:06

    If you have mTV (I mean TextView), you can calculate based on its dimensions (mTv.getWidth() and mTv.getHeight()) and dimension of created bitmap (bitmap.getWidth() and bitmap.getHeight()) and set these values as new dimensions of TextView (mTv).

            if (bitmap != null) {
                BitmapDrawable d = new BitmapDrawable(bitmap);
                mDrawable.addLevel(1, 1, d);
                int width = mTv.getWidth() < bitmap.getWidth() ? mTv.getWidth() : bitmap.getWidth();
                int height = bitmap.getHeight() * width / bitmap.getWidth();
                mDrawable.setBounds(0, 0, width, height);
                mDrawable.setLevel(1);
                // i don't know yet a better way to refresh TextView 
                // mTv.invalidate() doesn't work as expected 
                // but we can calculate new TextView dimensions
                mTv.setHeight(height);
                CharSequence t = mTv.getText();
                mTv.setText(t);
            } 
    

提交回复
热议问题