传递x,y坐标画圆()来的drawLine的() [英] Passing x,y coordinates of drawCircle() to drawLine()
问题描述
我想两个圆圈之间画线。我画在画布上两个圆圈,这两个圈有自己的CX和CY点。 我想通过这些CX和CY两个圆来的drawLine()的运行startx,starty和stopx,stopy。使得一个行应这两个圆之间绘制。强>这两个圈可以是彼此平行或水平彼此。什么到目前为止,我已计划是,我应该申请动作监听到圈子中,以获得他们的X和Y坐标。我在这里面临的问题是,画圆()方法调用中的的onDraw()和画线()方法调用中的 onTouch()。我已经在下面分享我的code请提出解决方案,使我能通圆的坐标的drawLine()。
包com.example.circleline;进口android.app.Activity;
进口android.graphics.Bitmap;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.Display;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.View.OnTouchListener;
进口android.widget.ImageView;公共类MainActivity扩展活动实现OnTouchListener
{ ImageView的ImageView的;
位图位图;
帆布油画;
涂料粉刷;
涂料PDOT =新的油漆();
浮downx = 0,柔和= 0,UPX = 30,upy = 50;
INT COLS = 5;
INT行= 6;
@燮pressWarnings(德precation)
公共无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
ImageView的=(ImageView的)this.findViewById(R.id.imageView1); 显示currentDisplay = getWindowManager()getDefaultDisplay()。
浮DW = currentDisplay.getWidth();
浮DH = currentDisplay.getHeight();
位= Bitmap.createBitmap((INT)DW,(INT)DH,Bitmap.Config.ARGB_8888);
帆布=新的Canvas(位图);
油漆=新的油漆();
paint.setColor(Color.BLUE);
imageView.setImageBitmap(位图);
DW = canvas.getWidth()/(COLS + 1);
DH = canvas.getDensity()/(行+ 1);
对于(INT Y = 0; Y<行; Y ++)
{
为(中间体X = 0; X&下; COLS; X ++)
{
canvas.drawCircle((X + 1)* DW,(Y + 1)*(3 * DH),20,PDOT);
}
}
imageView.setOnTouchListener(本);
//canvas.drawCircle(upx,upy,20,PDOT);
}
公共布尔onTouch(视图V,MotionEvent五){
INT行动= e.getAction();
开关(动作)
{
案例MotionEvent.ACTION_DOWN:
downx = e.getX();
柔和= e.getY();
Log.d(奥马尔,将String.valueOf(downx));
Log.d(法鲁克,将String.valueOf(柔和));
打破;
案例MotionEvent.ACTION_MOVE:
打破;
案例MotionEvent.ACTION_UP:
UPX = e.getX();
upy = e.getY();
canvas.drawLine(downx,柔和,UPX,upy,油漆);
imageView.invalidate();
打破;
案例MotionEvent.ACTION_CANCEL:
打破;
默认:
打破;
}
返回true;
}}
把画线()中的onDraw()也是如此。放入的onDraw()所有的绘图操作,他们不属于任何其他地方。在onTouch只需保存X / Y,和的onDraw()在X / Y得出。
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;
}
}
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.
这篇关于传递x,y坐标画圆()来的drawLine的()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!