画布仅显示黑屏
我尝试将图像图块连接到一个画布中,
这是我的代码,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看 Google 文档 关于“draw”方法的说明在最后一行调用:
所以它所做的唯一的事情就是将 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:
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.