画布仅显示黑屏

发布于 2025-01-08 16:54:39 字数 1413 浏览 0 评论 0原文

我尝试将图像图块连接到一个画布中,

这是我的代码,

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最笨的告白 2025-01-15 16:54:39

看看 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));
那应该可以解决问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文