2012-09-10 219 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); 

位圖圖像出現並保持在屏幕上。感謝上面的海報