Drawables with setBounds not keeping their size within an ImageSpan

505 views Asked by At

I am trying to add images inline to an EditText. I am doing this via an ImageSpan. These images could potentially be either BitmapDrawables or VectorDrawables and are stored as Drawable objects.

Here is the code I am using:

smilie.setBounds(0, 0, editor.getLineHeight(), editor.getLineHeight());
spanObject = new ImageSpan(smilie, similieObject.imageUrl);

There are two problems with this method. The first is that the 'viewing area' of the image appears to change but the actual scale of the image doesn't (e.g a corner of the image is only visible). The second problem is that as soon as I insert another ImageSpan - regardless of the position the first ImageSpan reverts to the intrinsic height and width:

Example

I have tried the above method, also first converting the drawable to a ScaleDrawable and converting the drawable to a BitmapDrawable, however this makes the image 'disappear' (or at least become extremely small).

(For reference here is the Bitmap conversion code):

smilie.setBounds(0, 0, editor.getLineHeight(),editor.getLineHeight());

Bitmap bitmap;

bitmap = Bitmap.createBitmap(smilie.getIntrinsicWidth(), smilie.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bitmap);
smilie.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
smilie.draw(canvas);

bitmap = Bitmap.createScaledBitmap(bitmap, editor.getLineHeight(), editor.getLineHeight(), true);
smilie = new BitmapDrawable(bitmap);

spanObject = new ImageSpan(smilie);
0

There are 0 answers