Android 中点击绘图

发布于 2024-10-14 00:50:21 字数 1172 浏览 5 评论 0原文

我正在尝试制作一个程序,使用来自 2 个 EditText 字段的用户输入值制作图表。程序应使用此输入从轴中心到信息指定的点绘制一条线。我有一个按钮,每次单击它时都应该在指定的点上创建一条新线(因此可以有多条线)我创建了一个自定义视图来保存轴,但显然它利用了它的 onDraw 方法,所以我也不能用它来画新线。

这是我的自定义视图的代码:

package android.physicsengine;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class AxisDrawing extends View{

public AxisDrawing(Context context){

    super(context);
}
public AxisDrawing(Context context, AttributeSet attrs){

    super(context, attrs);

}
public AxisDrawing(Context context, AttributeSet attrs, int defStyle){

    super(context, attrs, defStyle);
}

@Override
protected void onDraw(Canvas canvas){
    canvas.drawColor(Color.BLACK);
    Paint linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    linePaint.setColor(Color.RED);
    canvas.drawLine(canvas.getWidth()/2, canvas.getHeight()/2-200,        canvas.getWidth()/2 ,canvas.getHeight()/2+100, linePaint);
    canvas.drawLine(canvas.getWidth()/2-150, canvas.getHeight()/2-75, canvas.getWidth()/2+150 ,canvas.getHeight()/2-75, linePaint);
}
}

I am trying to make a program the makes a graph using user input values from 2 EditText Fields. The program should use this input to draw a line from the center of an axis to a point specified by the information. I have a button that each time it is clicked should make a new line to the point specified (so there can be more than one line) I have created a custom view to hold the axis, but that utilizes its onDraw method, obviously, so I cant use it also to draw the new line.

Here is the code for my custom view:

package android.physicsengine;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class AxisDrawing extends View{

public AxisDrawing(Context context){

    super(context);
}
public AxisDrawing(Context context, AttributeSet attrs){

    super(context, attrs);

}
public AxisDrawing(Context context, AttributeSet attrs, int defStyle){

    super(context, attrs, defStyle);
}

@Override
protected void onDraw(Canvas canvas){
    canvas.drawColor(Color.BLACK);
    Paint linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    linePaint.setColor(Color.RED);
    canvas.drawLine(canvas.getWidth()/2, canvas.getHeight()/2-200,        canvas.getWidth()/2 ,canvas.getHeight()/2+100, linePaint);
    canvas.drawLine(canvas.getWidth()/2-150, canvas.getHeight()/2-75, canvas.getWidth()/2+150 ,canvas.getHeight()/2-75, linePaint);
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2024-10-21 00:50:21

如果您的自定义视图类是在活动中定义的,那么它是该活动的内部类,并且可以访问在活动级别上定义的变量和数组。

每次用户单击按钮时,您都应该处理信息并将其存储到 onDraw 可以访问的这些公共变量或数组中,并从中计算下一行或整个图形。如果您的自定义视图是一个单独的类,那么您需要传递数据,一种方法是使用静态变量..

让 onDraw() 方法再次绘制您需要声明的图形:

myCustomView.invalidate();

在为图表设置新数据后的按钮单击事件中。

If your custom view class is defined in the activity, then it is an inner class of that activity and has access to variables and arrays which are defined on the activity level.

Every time the user clicks the button, you should process and store the information into these common variables or arrays which onDraw can access and from them calculate the next line or the entire graph. If your custom view is a separate class, then you need to pass the data, one way to do it is to use static varibales..

to make onDraw() method draw again your graph you need to state:

myCustomView.invalidate();

in the button click event, just after you set the new data for the graph.

暖阳 2024-10-21 00:50:21

您只需在自定义视图中设置数据(全局)并调用 invalidate 即可重绘视图。

importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.util.AttributeSet;
importandroid.view.View;


@Override
protectedvoidonDraw(Canvascanvas
{

    canvas.drawColor(Color.BLACK);
        PaintlinePaint=newPaint(Paint.ANTI_ALIAS_FLAG);
    linePaint.setColor(Color.RED);
    canvas.drawLine(data,data,getWidtt()-data,getHeight()-data,linePaint);

    //you can also pplaceinvalidate() here which will recursively redraw the canvas in aloop

}

publicvoidsetData(intdata)
{
    this.data=data;
    invalidate();
}
}

You simply need to set the data in the custum view (Globally ) and call invalidate which will redraw the view.

importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.util.AttributeSet;
importandroid.view.View;


@Override
protectedvoidonDraw(Canvascanvas
{

    canvas.drawColor(Color.BLACK);
        PaintlinePaint=newPaint(Paint.ANTI_ALIAS_FLAG);
    linePaint.setColor(Color.RED);
    canvas.drawLine(data,data,getWidtt()-data,getHeight()-data,linePaint);

    //you can also pplaceinvalidate() here which will recursively redraw the canvas in aloop

}

publicvoidsetData(intdata)
{
    this.data=data;
    invalidate();
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文