繁体   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