Passing x,y coordinates of drawCircle() to drawLine()

瘦欲@ 提交于 2019-12-13 05:47:46

问题


I am trying to draw line between two circles. I have drawn two circles on canvas, These two circles have their cx and cy points. I want to pass these cx and cy of both circles to drawLine()'s startx,starty and stopx,stopy. So that a line should be drawn between these two circles. These two circles can be parallel to each other or horizontal to each other. What I have planned so far is that I should apply action-listener to circles in order to get their x and y coordinates. The problem I am facing here is that drawCircle() method call is in onDraw() and lineDraw() method call is in onTouch(). I have shared my code below please suggest solution so that I can pass circle's coordinates to drawLine().

package com.example.circleline;

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;
    Paint pDot  = new Paint();
    float downx=0,downy=0,upx=30,upy=50;
    int cols = 5;
    int rows = 6;
    @SuppressWarnings("deprecation")
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        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);
        dw=canvas.getWidth()/(cols+1);
        dh=canvas.getDensity()/(rows+1);
        for (int y=0;y<rows;y++)
        {
            for (int x=0;x<cols;x++)
            {
                canvas.drawCircle((x + 1) * dw, (y + 1) *(3* dh), 20, pDot);
            }
        }
        imageView.setOnTouchListener(this);
        //canvas.drawCircle(upx, upy, 20, pDot);
    }


    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;
    }

}

回答1:


Put lineDraw() in onDraw() as well. Put all your drawing operations in onDraw(), they don't belong anywhere else. In onTouch just save the x/y, and in onDraw() draw at x/y.



来源:https://stackoverflow.com/questions/22854225/passing-x-y-coordinates-of-drawcircle-to-drawline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!