Android的页面卷曲通过harism,TextView的增加引起的,异常:卷曲、异常、页面、Android

2023-09-04 05:20:23 作者:梅花很淒涼

我需要修改有关page_curl harism的项目 https://github.com/harism/android_page_curl 有以下要求:我需要添加的TextView娄Curl_View(当页面翻转的TextView的内容发生变化)。 我做了供应的TextView与数据接口

I need to edit harism's project about page_curl https://github.com/harism/android_page_curl with the following requirement : I need to add TextView bellow the Curl_View (when the page is flipped the content of the textView is changed). I made an Interface that supply the TextView with the data

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

        public String getText(int index);

        public TextView getTextView();
    }

和更新的TextView的唯一的地方时,页面是curles在onDrawFrame()梅索德

and the only place to update the TextView when the pages is curles is in onDrawFrame() methode

但我有以下异常: android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程

有一些解决方案,说我应该使用的处理器,我的问题是如何使用的处理器在这种情况下?

There is some solutions says that I should use the Handler , My Question is how to use the Handler in this case ?

推荐答案

我想这和它的工作:     私有类BitmapProvider实现CurlView.BitmapProvider {

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;
    }
}