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

当页面是 curles 时,唯一更新 TextView 的地方是 onDrawFrame() 方法

但我有以下异常:android.view.ViewRoot$CalledFromWrongThreadException: 只有创建视图层次结构的原始线程才能触摸它的视图。

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

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 回答