2012-09-10 130 views
0

我的测试Android应用程序有一个活动,LoadImage,有两种方法:一个onCreate方法处理图像与OpenCV,和一个显示方法,在屏幕上显示图像5秒。以下是我迄今为止的显示方法:Android - 绘制位图

[convert OpenCV matrix to a bitmap using an OpenCV method] 

Canvas canvas = new Canvas(bitmap); 
canvas.drawBitmap(bitmap, 0, 0, null); 

try { 
    synchronized (this) { 
     wait(5000); 
    } 
} catch (InterruptedException e) { 
} 

这里是为单一活动的XML文件(这是一个空白屏幕):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
</RelativeLayout> 

如果我运行代码原样是,我得到了...一个空白的屏幕。我如何让位图显示出来?

非常感谢。

+1

创建一个'ImageView',将它的源代码设置为你的'Bitmap',然后将它添加到你的'RelativeLayout'中? – Eric

回答

1

试试这个:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

<ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="2dp"/> 
</RelativeLayout> 

而且在Java代码中做到这一点:

onCreate() 
{ 
    ImageView imageView = (ImageView) findViewById(R.id.imageView1); 
} 

您要发送的位图到画布上吧?

所以在你的方法中,你正在绘制位图。

imageView.setImageBitmap(bitmap); 

不能直接设置画布到imageView1。

因为当你在现实生活中知道,画布只是一个刷。同样,这里画布也只是一个画笔。所以不必担心它。您的编辑图像现在只存储在bitmap中。所以,这就是为什么您可以直接设置位图的帆布 editng后。

+0

工程就像一个魅力!我甚至不需要明确地创建画布。 –

+0

很高兴为您效劳。谢谢 –

0

我说这个我acitity_main.xml(它的LinearLayout):

<ImageView 
android:id="@+id/imageView1" 
android:layout_width="200px" 
android:layout_height="200px" 
android:layout_marginLeft="2dp"/> 

与此代码的onCreate方法针对的是非常相同的XML活动:

Bitmap bitmap = BitmapFactory.decodeFile(fileAndPath); 
mImageView = (ImageView) findViewById(R.id.imageView1);  
mImageView.setImageBitmap(bitmap); 

位图图像出现并保持在屏幕上。感谢上面的海报