Question

I am trying to get input from user and display line on canvas. For that I declared a class named "Test". In this class there is an imageView, Canvas, Bitmap onTouch and onCreate method. I defined an imageView in activity_main.xml.I compiled this code and it is not showing line on my device's screen when onTouch Event occurs. I am sharing code. Please check this code and suggest me solution. My requirement is to display a straight line on canvas.

activity_main.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

Test Class:

    package com.example.test;

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Display;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.widget.ImageView;

    public class Test extends Activity implements OnTouchListener {

        ImageView imageView;
        Bitmap bitmap;
        Canvas canvas;
        Paint paint;
        float downx=0,downy=0,upx=0,upy=0;

        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    //      imageView=(ImageView)this.findViewById(R.id.ImageView);
            imageView = (ImageView) this.findViewById(R.id.imageView1);
            Display currentDisplay= getWindowManager().getDefaultDisplay();
            float dw=currentDisplay.getWidth();
            float dh=currentDisplay.getHeight();
            bitmap=Bitmap.createBitmap((int) dw, (int)dh, Bitmap.Config.ARGB_8888);
            canvas=new Canvas(bitmap);
            paint=new Paint();
            paint.setColor(Color.BLUE);
            imageView.setImageBitmap(bitmap);
            imageView.setOnTouchListener(this);
        }

        public boolean onTouch(View v, MotionEvent e) {
            int action=e.getAction();
            switch(action)
            {
            case MotionEvent.ACTION_DOWN:
                downx=e.getX();
                downy=e.getY();
                Log.d("Umar", String.valueOf(downx));
                Log.d("Farooq", String.valueOf(downy));
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                upx=e.getX();
                upy=e.getY();
                canvas.drawLine(downx, downy, upx, upy, paint);
                        imageView.invalidate();
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
                default:
                    break;
            }
            return true;
        }
            public void onDraw(Canvas canvas)
    {
        this.canvas=canvas;
        this.canvas.drawLine(downx, downy, upx, upy, paint);
    }
//   public void onDraw(Canvas canvas) {
//         canvas.drawColor(Color.WHITE);
//        // canvas.drawCircle(50, 80, 30, paint);
//         canvas.drawLine(downx, downy, upx, upy, paint);
//         canvas.drawText(""+canvas.getWidth()+", "+canvas.getHeight(), 0, 200,paint);
//     }
    }

LogCat output:

04-02 14:45:14.496: D/ActivityThread(17367): ACT-AM_ON_PAUSE_CALLED ActivityRecord{417cf7a0 token=android.os.BinderProxy@417cf000 {com.example.test/com.example.test.MainActivity}}
04-02 14:45:14.500: D/ActivityThread(17367): ACT-PAUSE_ACTIVITY handled : 0 / android.os.BinderProxy@417cf000
04-02 14:45:14.502: D/ActivityThread(17367): ACT-STOP_ACTIVITY_SHOW handled : 0 / android.os.BinderProxy@417cf000
04-02 14:46:25.519: D/ActivityThread(17367): ACT-AM_ON_RESUME_CALLED ActivityRecord{417cf7a0 token=android.os.BinderProxy@417cf000 {com.example.test/com.example.test.MainActivity}}
04-02 14:46:25.519: D/ActivityThread(17367): ACT-RESUME_ACTIVITY handled : 0 / android.os.BinderProxy@417cf000
04-02 14:46:25.546: D/ActivityThread(17367): ACT-AM_ON_PAUSE_CALLED ActivityRecord{417cf7a0 token=android.os.BinderProxy@417cf000 {com.example.test/com.example.test.MainActivity}}
04-02 14:46:25.549: D/ActivityThread(17367): ACT-PAUSE_ACTIVITY handled : 0 / android.os.BinderProxy@417cf000
04-02 14:46:25.552: D/ActivityThread(17367): ACT-STOP_ACTIVITY_SHOW handled : 0 / android.os.BinderProxy@417cf000
04-02 14:46:45.986: D/ActivityThread(17367): ACT-AM_ON_RESUME_CALLED ActivityRecord{417cf7a0 token=android.os.BinderProxy@417cf000 {com.example.test/com.example.test.MainActivity}}
04-02 14:46:45.986: D/ActivityThread(17367): ACT-RESUME_ACTIVITY handled : 0 / android.os.BinderProxy@417cf000
04-02 14:46:46.016: D/ActivityThread(17367): ACT-AM_ON_PAUSE_CALLED ActivityRecord{417cf7a0 token=android.os.BinderProxy@417cf000 {com.example.test/com.example.test.MainActivity}}
04-02 14:46:46.021: D/ActivityThread(17367): ACT-PAUSE_ACTIVITY handled : 0 / android.os.BinderProxy@417cf000
04-02 14:46:46.025: D/ActivityThread(17367): ACT-STOP_ACTIVITY_SHOW handled : 0 / android.os.BinderProxy@417cf000
04-02 14:46:51.057: D/ActivityThread(17367): ACT-AM_ON_RESUME_CALLED ActivityRecord{417cf7a0 token=android.os.BinderProxy@417cf000 {com.example.test/com.example.test.MainActivity}}
04-02 14:46:51.057: D/ActivityThread(17367): ACT-RESUME_ACTIVITY handled : 0 / android.os.BinderProxy@417cf000
04-02 14:46:51.086: D/ActivityThread(17367): ACT-AM_ON_PAUSE_CALLED ActivityRecord{417cf7a0 token=android.os.BinderProxy@417cf000 {com.example.test/com.example.test.MainActivity}}
04-02 14:46:51.090: D/ActivityThread(17367): ACT-PAUSE_ACTIVITY handled : 0 / android.os.BinderProxy@417cf000
04-02 14:46:51.092: D/ActivityThread(17367): ACT-STOP_ACTIVITY_SHOW handled : 0 / android.os.BinderProxy@417cf000
Was it helpful?

Solution

Initially I was calling lineDraw() in onDraw() method. It was not showing line on canvas. I changed lineDraw() location to onTouch(). And it is working perfectly now. Here is code which will give you insight.

Code

//package com.example.test;
package com.example.test;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnTouchListener {

    ImageView imageView;
    Bitmap bitmap;
    Canvas canvas;
    Paint paint;
    float downx=0,downy=0,upx=0,upy=0;

    @SuppressWarnings("deprecation")
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView=(ImageView)this.findViewById(R.id.imageView1);
      //imageView = (ImageView) this.findViewById(R.id.ImageView);
        Display currentDisplay= getWindowManager().getDefaultDisplay();
        float dw=currentDisplay.getWidth();
        float dh=currentDisplay.getHeight();
        bitmap=Bitmap.createBitmap((int) dw, (int)dh, Bitmap.Config.ARGB_4444);
        canvas=new Canvas(bitmap);
        paint=new Paint();
        paint.setColor(Color.BLACK);
        imageView.setImageBitmap(bitmap);
        imageView.setOnTouchListener(this);
    }


    public boolean onTouch(View v, MotionEvent e) {
        int action=e.getAction();
        switch(action)
        {
        case MotionEvent.ACTION_DOWN:
            downx=e.getX();
            downy=e.getY();
            Log.d("Umar", String.valueOf(downx));
            Log.d("Farooq", String.valueOf(downy));
            break;
        case MotionEvent.ACTION_MOVE:
            break;
        case MotionEvent.ACTION_UP:
            upx=e.getX();
            upy=e.getY();
            canvas.drawLine(downx, downy, upx, upy, paint);
            imageView.invalidate();
            break;
        case MotionEvent.ACTION_CANCEL:
            break;
            default:
                break;
        }
        return true;
    }

}

OTHER TIPS

You can't draw in UI handler thread. There is special method, that you should overload, to draw.Read this first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top