2012-02-08 34 views
0

我需要編輯harism的項目約page_curl https://github.com/harism/android_page_curl 有以下要求:我需要補充的TextView波紋管的Curl_View(當頁面翻轉的TextView的內容被改變)。 我做了與數據Android的頁面捲曲,增加的TextView引起的異常:

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

     public String getText(int index); 

     public TextView getTextView(); 
    } 

,並更新TextView的唯一的地方提供TextView的接口,當網頁是curles是onDrawFrame()梅索德

但我有以下異常:android.view.ViewRoot $ CalledFromWrongThreadException:只有創建視圖層次結構的原始線程可以觸及其視圖。

有一些解決方案說我應該使用處理程序,我的問題是如何在這種情況下使用處理程序?

回答

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