【问题标题】:Android page curl by harism , adding TextView caused and exception :Android页面卷曲通过harism,添加TextView引起和异常:
【发布时间】:2012-02-08 14:58:00
【问题描述】:

我需要编辑harism 的关于page_curl 的项目 https://github.com/harism/android_page_curl 具有以下要求:我需要在 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:只有创建视图层次结构的原始线程才能触及其视图。

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

【问题讨论】:

    标签: android page-curl


    【解决方案1】:

    我试过了,它奏效了: 私有类 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 1970-01-01
      相关资源
      最近更新 更多