如何在Android上的ImageView上画线?

发布于 2024-12-26 12:11:55 字数 1968 浏览 1 评论 0原文

我正在尝试开发一个简单的地图应用程序,它将在屏幕上显示地图。

当用户在屏幕上移动光标时,我想在地图上显示 2 条垂直线。我尝试了很多例子来了解这一点,但不幸的是,没有成功。 我怎样才能做到这一点?

作为上一个问题这里我尝试过。但没有得到答案。有人可以指导我吗?

我的 main.xml 如下:

<?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" >

    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <ImageView android:id="@+id/main_imagemap"
        android:src="@drawable/worldmap"
        android:clickable="true"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/>
    </LinearLayout>

</LinearLayout>

和我的活动文件(我刚刚启动它..)

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

public class LineMapActivity extends Activity 
{


    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView map_image = (ImageView)findViewById(R.id.main_imagemap);

    }
}

正如在该链接中一样,我还添加了 MyImageView。

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.widget.ImageView;

public class MyImageView extends ImageView
{

    public MyImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        canvas.drawLine(0, 0, 20, 20, p);
        super.onDraw(canvas);
    }
}

现在我如何将此 MyImageView 添加到我的应用程序中?

I am trying to develop a simple map application, which will display a map in the screen.

When the user moves the cursor on screen, I want to display 2 perpendicular lines over my map. I had tried many examples to get an idea for that, but unfortunately, didn't succeed.
How can I make this?

And as one previous questionhere I had tried. but didn't get the answer. Can anyone guide me?

My main.xml is as following:

<?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" >

    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <ImageView android:id="@+id/main_imagemap"
        android:src="@drawable/worldmap"
        android:clickable="true"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/>
    </LinearLayout>

</LinearLayout>

And my activity file(i had just started it..)

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

public class LineMapActivity extends Activity 
{


    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView map_image = (ImageView)findViewById(R.id.main_imagemap);

    }
}

And as in that link, i had also added MyImageView.

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.widget.ImageView;

public class MyImageView extends ImageView
{

    public MyImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        canvas.drawLine(0, 0, 20, 20, p);
        super.onDraw(canvas);
    }
}

Now how can i add this MyImageView to my app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆之渊 2025-01-02 12:11:55

最后我想出了一个解决方案。这是一个在图像上画一条线的简单程序。

这是我的 main.xml 文件(com.ImageDraw.MyImageView 是我的自定义视图,代码如下):

<?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" >

    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <com.ImageDraw.MyImageView android:id="@+id/main_imagemap"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

这是主要活动:

import android.app.Activity;
import android.os.Bundle;

public class MyActivity extends Activity 
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

这是我的自定义图像视图 (MyImageView):

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MyImageView extends SurfaceView implements SurfaceHolder.Callback
{
    private CanvasThread canvasthread;

    public MyImageView(Context context) {
        super(context);
        getHolder().addCallback(this);
        canvasthread = new CanvasThread(getHolder(), this);
        setFocusable(true);
    }

    public MyImageView(Context context, AttributeSet attrs)
    {
        super(context,attrs);
        getHolder().addCallback(this);
        canvasthread = new CanvasThread(getHolder(), this);
        setFocusable(true);
    }

    protected void onDraw(Canvas canvas) {
        Log.d("ondraw", "ondraw");
        Paint p = new Paint();
        Bitmap mapImg = BitmapFactory.decodeResource(getResources(), R.drawable.mybitmap);
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(mapImg, 0, 0, null);
        p.setColor(Color.RED);
        canvas.drawLine(0, 0, 100, 100, p);
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }

    public void surfaceCreated(SurfaceHolder holder) {
        canvasthread.setRunning(true);
        canvasthread.start();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        canvasthread.setRunning(false);
        while (retry)
        {
            try
            {
                canvasthread.join();
                retry = false;
            }
            catch (InterruptedException e) {
                // TODO: handle exception
            }
        }
    }
}

最后这是我的 CanvasThread:

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class CanvasThread extends Thread
{
    private SurfaceHolder surfaceHolder;
    private MyImageView myImageView;
    private boolean run = false;

    public CanvasThread(SurfaceHolder s, MyImageView m)
    {
        surfaceHolder = s;
        myImageView = m;
    }

    public void setRunning(boolean r)
    {
        run = r;
    }

    public void run()
    {
        Canvas c;
        while(run)
        {
            c=null;
            try
            {
                c= surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder) {
                    myImageView.onDraw(c);
                }
            }
            finally
            {
                if(c!=null)
                {
                    surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
}

您可以找到有关更多详细信息,请参阅此教程

Finally I figured out a solution. It's a simple program which draws a line over an Image.

Here is my main.xml file (com.ImageDraw.MyImageView is my custom view, code below):

<?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" >

    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <com.ImageDraw.MyImageView android:id="@+id/main_imagemap"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

And here is the main activity:

import android.app.Activity;
import android.os.Bundle;

public class MyActivity extends Activity 
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

and this is my custom image view (MyImageView):

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MyImageView extends SurfaceView implements SurfaceHolder.Callback
{
    private CanvasThread canvasthread;

    public MyImageView(Context context) {
        super(context);
        getHolder().addCallback(this);
        canvasthread = new CanvasThread(getHolder(), this);
        setFocusable(true);
    }

    public MyImageView(Context context, AttributeSet attrs)
    {
        super(context,attrs);
        getHolder().addCallback(this);
        canvasthread = new CanvasThread(getHolder(), this);
        setFocusable(true);
    }

    protected void onDraw(Canvas canvas) {
        Log.d("ondraw", "ondraw");
        Paint p = new Paint();
        Bitmap mapImg = BitmapFactory.decodeResource(getResources(), R.drawable.mybitmap);
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(mapImg, 0, 0, null);
        p.setColor(Color.RED);
        canvas.drawLine(0, 0, 100, 100, p);
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }

    public void surfaceCreated(SurfaceHolder holder) {
        canvasthread.setRunning(true);
        canvasthread.start();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        canvasthread.setRunning(false);
        while (retry)
        {
            try
            {
                canvasthread.join();
                retry = false;
            }
            catch (InterruptedException e) {
                // TODO: handle exception
            }
        }
    }
}

And finally here is my CanvasThread:

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class CanvasThread extends Thread
{
    private SurfaceHolder surfaceHolder;
    private MyImageView myImageView;
    private boolean run = false;

    public CanvasThread(SurfaceHolder s, MyImageView m)
    {
        surfaceHolder = s;
        myImageView = m;
    }

    public void setRunning(boolean r)
    {
        run = r;
    }

    public void run()
    {
        Canvas c;
        while(run)
        {
            c=null;
            try
            {
                c= surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder) {
                    myImageView.onDraw(c);
                }
            }
            finally
            {
                if(c!=null)
                {
                    surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
}

You can find more details in this tutorial

極樂鬼 2025-01-02 12:11:55

由于您尝试将 ImageView 对象转换为 MyImageView 对象,因此出现错误。

Error you are getting because you are trying to casting ImageView object to MyImageView object.

单身狗的梦 2025-01-02 12:11:55

只需修复 xml 以包含您的对象而不是像这样的 imageview (请注意, com.your.package.MyImageView 应该是包含您的类的包的完整包名称,然后是类名称)

<?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" >

    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <com.your.package.MyImageView android:id="@+id/main_imagemap"
        android:src="@drawable/worldmap"
        android:clickable="true"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/>
    </LinearLayout>

</LinearLayout>

Just fix the xml to include your object instead of an imageview like so (note that com.your.package.MyImageView should be the full package name of the package containing your class and then the class name)

<?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" >

    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <com.your.package.MyImageView android:id="@+id/main_imagemap"
        android:src="@drawable/worldmap"
        android:clickable="true"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/>
    </LinearLayout>

</LinearLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文