0

次の要件で、page_curl https://github.com/harism/android_page_curlに関する harism のプロジェクトを編集する必要があります。Curl_View の 下に TextView を追加する必要があります (ページが反転すると、textView のコンテンツが変更されます)。TextView にデータを提供するインターフェイスを作成しました

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

        public String getText(int index);

        public TextView getTextView();
    }

ページがカールしているときに TextView を更新する唯一の場所は onDrawFrame() メソッドにあります

しかし、次の例外があります: android.view.ViewRoot$CalledFromWrongThreadException: ビュー階層を作成した元のスレッドのみがそのビューにアクセスできます。

Handler を使用する必要があるといういくつかの解決策があります。私の質問は、この場合の Handler の使用方法です。

4

1 に答える 1

0

私はこれを試しましたが、うまくいきました:プライベートクラスBitmapProviderは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;
    }
}
于 2012-03-27T04:54:32.833 に答える