簡體   English   中英

畫布僅顯示黑屏

[英]Canvas only shows black screen

我嘗試將圖像圖塊串聯到一個畫布中

這是我的代碼

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"
    />

看一下Google文檔對您在最后一行中調用的方法“ draw”的看法:

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));
這應該夠了吧。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM