Search code examples
androidbitmaptextviewscreenshotemoji

How can i take screenshot of rotated TextView with emoji?


I'm trying to make a screenshot of the rotated TextView which contain emoji icons. But on resulting bitmap i see that emoji are not rotated! Why is this happening? How can i make a screenshot with rotated emoji ?

What i expect:

enter image description here

And this is what i get:

enter image description here

I'm using this method to get screenshot of view:

layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache();
Bitmap bitmap = null;
if (layout.getDrawingCache() != null)
    bitmap = layout.getDrawingCache().copy(Bitmap.Config.ARGB_8888, false);
layout.setDrawingCacheEnabled(false);
layout.destroyDrawingCache();

Update: as i figured, if I set textView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); then emoji will not be rotated even in the TextView (if you rotate TextView - they will not be rotated, they will be just moving around like a carousel), but still I don't really understand why is this happening, or rotation of emoji (on first picture) is only because of hardware acceleration?


Solution

  • ok, i couldnt find a way to solve this really annoying issue, so i had to hack it a bit. imageviews works perfectly with rotation. so i basically do all the manipulations with image view - and setting it's image out of the emoji text i want using this method:

    private Bitmap generateBitmapFromText(int size, String res) {
        TextView tv = new TextView(getContext());
        tv.setText(res);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 150f);
        tv.setTextColor(0xffffffff);
        tv.measure(size, size);
        int questionWidth = tv.getMeasuredWidth();
        int questionHeight = tv.getMeasuredHeight();
        Bitmap bitmap = Bitmap.createBitmap(questionWidth, questionHeight, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bitmap);
        tv.layout(0, 0, questionWidth, questionHeight);
        tv.draw(c);
        return bitmap;
    }
    

    and then i call

        Bitmap bitmap = generateBitmapFromText(stickersStartingSize, res);
        imageview.setImageBitmap(bitmap);