How to draw lines over ImageView on Android?

前端 未结 3 1034
执念已碎
执念已碎 2020-12-10 17:37

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

相关标签:
3条回答
  • 2020-12-10 17:42

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

    0 讨论(0)
  • 2020-12-10 17:49

    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>
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题