2012-02-22 89 views
0

我嘗試concatenize圖像塊爲一個帆布帆布只顯示黑屏

這裏是我的代碼

Canvas createCanvas(Bitmap[][] array){ 

    int height = array[0][0].getHeight(); 
    int width = array[0][0].getWidth(); 
    Bitmap bitmap = Bitmap.createBitmap(3*height,3*width,Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(bitmap); 
    canvas.drawBitmap (array[0][0],0f,0f,new Paint(Paint.ANTI_ALIAS_FLAG)); 
    canvas.drawBitmap (array[0][1],width,0f,new Paint(Paint.FILTER_BITMAP_FLAG)); 
    //etc..etc..for all the tiles 

    return canvas; 
} 

調用此方法是這樣的:

//source File 
    Bitmap bMap = BitmapFactory.decodeResource(getResources(),R.drawable.image); 
    //Tile Source File 
    Bitmap [][] array_ref = helper_ref.createImageArrays(bMap); 

    //Invoke Method above 
    Canvas canvas = helper_ref.createCanvas(array_ref); 
    //Draw canvas 
    ImageView view_ref = (ImageView) findViewById(R.id.imageView1); 
    view_ref.draw(canvas); 

我還爲你提供了我想畫的視圖。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    /> 

+0

我認爲問題在於你爲canvas.drawBitmap中的最後一個參數傳遞null。嘗試傳遞一個繪畫對象。 – m1ntf4n 2012-02-22 16:39:58

+0

嗯它沒有工作,但它是一個線索。我傳入了一個默認的Object new Paint()。也許是錯的? – dan 2012-02-22 16:45:05

+0

我嘗試了2個Paint Constants沒有成功,我認爲不是這樣。我也爲你提供我畫的視圖。 – dan 2012-02-22 16:50:29

回答

1

看一看什麼Google docs說的方法「畫」你在最後一行調用:

void draw(Canvas canvas) 
    Manually render this view (and all of its children) to the given Canvas. 

所以這樣做的唯一事情就是繪製ImageView的(這是空的)到那個畫布。因此,實際發生的事情與您想實現的目的相反:您將ImageView繪製到該位圖中,而不是相反。
解決方案很簡單:最後,您的方法createCanvas不應返回畫布,而應返回您正在繪製的位圖。隨着位圖,這樣做:
view_ref.setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap));
這應該做的伎倆。

+0

它的工作原理+超級解釋!謝謝你的哥們! – dan 2012-02-22 17:28:46

+0

沒問題,老兄;) – m1ntf4n 2012-02-22 17:44:47