Android-绘制位图



我的测试Android应用程序有一个活动LoadImage,有两个方法:一个是onCreate方法,它用OpenCV处理图像,另一个是Display方法,它在屏幕上显示图像5秒。以下是我迄今为止对显示方法的了解:

[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>

如果我按原样运行代码,我会得到。。。空白屏幕。如何显示位图?

非常感谢。

试试这个:

<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中。因此,您可以在通过canvas编辑后直接设置位图

我将其添加到我的acitity_main.xml中(它是LinearLayout):

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

并将此代码添加到同一xml:的活动的OnCreate方法

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

位图图像显示并保留在屏幕上。感谢上面的海报

最新更新