【问题标题】:Passing x,y coordinates of drawCircle() to drawLine()将 drawCircle() 的 x,y 坐标传递给 drawLine()
【发布时间】:2014-04-04 05:24:30
【问题描述】:

我正在尝试在两个圆圈之间画线。我在画布上画了两个圆圈,这两个圆圈有它们的 cx 和 cy 点。 我想将这两个圆的 cx 和 cy 传递给 drawLine() 的 startx,starty 和 stopx,stopy。所以应该在这两个圆之间画一条线。 这两个圆可以相互平行或相互水平。到目前为止我的计划是我应该将动作侦听器应用于圆圈以获得它们的 x 和 y 坐标。我在这里面临的问题是 drawCircle() 方法调用在 onDraw()lineDraw() 方法调用在 onTouch()。我在下面分享了我的代码,请提出解决方案,以便我可以将圆的坐标传递给 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;
    }

}

【问题讨论】:

    标签: android android-canvas actionlistener touch-event ondraw


    【解决方案1】:

    也将 lineDraw() 放入 onDraw() 中。将所有绘图操作放在 onDraw() 中,它们不属于其他任何地方。在 onTouch 中只保存 x/y,在 onDraw() 中在 x/y 处绘制。

    【讨论】:

    • 请您解释一下我如何在圆圈上应用动作监听器,这样每当我触摸一个圆圈时,它就应该返回它的 x、y 坐标。
    • 由于我使用循环来绘制圆圈网格,所以我将如何唯一识别单击了哪个圆圈。
    • 遍历所有圆圈并检查从触摸到圆圈 x/y 的距离。选择距离最小的圆圈。
    • 想法是线应该在两个相邻的圆之间,它们可以相互平行或相互垂直。
    • 问题是如何选择某个圈子?我对在 circle 上实现 actionListener 感到困惑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 2013-05-05
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多