Curl by harism android page adding TextView and exception:

I need to edit the harism project about page_curl https://github.com/harism/android_page_curl with the following requirement: I need to add the TextView below the Curl_View (when the page is flipped, the content of the textView will change). I have created an interface that feeds the TextView with data

public interface TextProvider{
        public int getTextCount();
        public void setText(int index);

        public String getText(int index);

        public TextView getTextView();
    }

      

and the only place to update the TextView when the pages are triggered is in the onDrawFrame () method

but I have the following exception: android.view.ViewRoot $ CalledFromWrongThreadException: Only the original thread that created the view hierarchy can touch its views.

Some solutions say that I should use a handler, My question is how to use a handler in this case?

0


source to share


1 answer


I tried this and it worked: private class BitmapProvider implements CurlView.BitmapProvider {



    private int[] mBitmapIds = {};


    public void setImageList( int[] value )
    {
        mBitmapIds = value;
    }


    public BitmapDrawable writeOnDrawable( int drawableId, String text, int x, int y, int width )
    {

        Bitmap bitmap = BitmapFactory.decodeResource( getResources(), drawableId ).copy( Bitmap.Config.ARGB_8888, true );

        Bitmap newBitmap = Bitmap.createBitmap( bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888 );
        Canvas canvas = new Canvas( newBitmap );
        canvas.drawBitmap( bitmap, new Matrix(), null);

        if( text != "" ){
            TextView tv = new TextView( getApplicationContext() );
            tv.setText( text );
            tv.setTextColor( 0xa00050ff );
            tv.setTextSize( 24 );
            tv.setTypeface( typeface );

            Bitmap txtBitmap = Bitmap.createBitmap( width, 768, Bitmap.Config.ARGB_8888 );

            Canvas c = new Canvas( txtBitmap );
            tv.layout( 0, 0, width, 300 );
            tv.draw( c );

            canvas.drawBitmap( txtBitmap, x, y, null );
        }


        return new BitmapDrawable( newBitmap );
    }

    //@Override
    public Bitmap getBitmap( int width, int height, int index ) 
    {
        BitmapDrawable drawable;
        Page currentPage = getCurrentPage( index );

        if( currentPage.getText() != null ){
            drawable = writeOnDrawable( mBitmapIds[ index ] , currentPage.getText(), currentPage.getTextFieldX(), currentPage.getTextFieldY(), currentPage.getTextFieldWidth() );
        } else {
            drawable = writeOnDrawable( mBitmapIds[ index ] , "", currentPage.getTextFieldX(), currentPage.getTextFieldY(), currentPage.getTextFieldWidth() );
        }

        Bitmap bitmap = drawable.getBitmap();

        return bitmap;  

    }

    //@Override
    public int getBitmapCount() {
        return mBitmapIds.length;
    }
}

      

0


source







All Articles