一直更新 imageView ,Android
Updating an imageView all the time , Android
我正在做一个 2d 渲染 class(渲染一个 int 像素数组),我想在屏幕上显示这些像素。我试过使用 ImageView 但它不起作用; android 上是否有类似 canvas 的方法?或者如何创建一个始终更新的图像? (我已经实现了runnable class)
这是我所做的:
@Override
public void run()
{
Imageview exampleImage = (ImageView) findViewById(R.id.exampleId);
// This is a simple method to do the int array image;
int[] img = new int[100]
for(int i = 0; i < 100; i++){
img[i] = i*100;
}
Bitmap bitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
bitmap.setPixels(img, 0, 10, 0, 0, 100, 100);
exampleImage.setImageBitmap(bitmap);
}
嗯,屏幕上什么也没有显示
是的,您应该创建一个自定义 View
并在 Canvas
上绘制,这将填充下面的 Bitmap
。
覆盖 View#onDraw(Canvas) and draw the pixels with the Canvas#drawPoints(float[], Paint) method. Then you can use View#postInvalidateDelayed(long) 或其某些同级方法以强制 View
重绘自身,直到动画完成。
您可以在 this API 指南中找到有关 Canvas 和 Drawable 的更多信息。
我正在做一个 2d 渲染 class(渲染一个 int 像素数组),我想在屏幕上显示这些像素。我试过使用 ImageView 但它不起作用; android 上是否有类似 canvas 的方法?或者如何创建一个始终更新的图像? (我已经实现了runnable class)
这是我所做的:
@Override
public void run()
{
Imageview exampleImage = (ImageView) findViewById(R.id.exampleId);
// This is a simple method to do the int array image;
int[] img = new int[100]
for(int i = 0; i < 100; i++){
img[i] = i*100;
}
Bitmap bitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
bitmap.setPixels(img, 0, 10, 0, 0, 100, 100);
exampleImage.setImageBitmap(bitmap);
}
嗯,屏幕上什么也没有显示
是的,您应该创建一个自定义 View
并在 Canvas
上绘制,这将填充下面的 Bitmap
。
覆盖 View#onDraw(Canvas) and draw the pixels with the Canvas#drawPoints(float[], Paint) method. Then you can use View#postInvalidateDelayed(long) 或其某些同级方法以强制 View
重绘自身,直到动画完成。
您可以在 this API 指南中找到有关 Canvas 和 Drawable 的更多信息。