简体   繁体   中英

Canvas only shows black screen

I try to concatenize image tiles into one canvas

here is my code

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

invoke this method like this:

    //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);

I also provide you the view in which I want to draw.

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

Have a look what the Google docs say about the method "draw" you call in the last line:

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

So the only thing this does is drawing the ImageView (which is empty) to that canvas. So what's actually happening is the opposite of what you want to achieve: You're drawing the ImageView into that bitmap, not the other way round.
The solution is easy: At the end, your method createCanvas shouldn't return the the canvas but the bitmap you were drawing to. With the bitmap, do this:
view_ref.setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap));
That should do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM