问题描述
我有DrawView.如果我触摸此视图,它将绘制一个小圆圈.我不会画圆,但不会触摸视图-具有帮助功能"setPoints".我的工作:
I have DrawView. If I touch this view it draws small circles. I wont to draw circles but not to touch view - with help function "setPoints". What I do:
package com.samples;
import ...
public class DrawView extends View {
ArrayList<Point> points = new ArrayList<Point>();
Paint paint = new Paint();
private int pSize = 5;
private int pColor = Color.BLACK;
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setOnTouchListener(this);
Point point = new Point();
point.x = event.getX();
point.y = event.getY();
points.add(point);
invalidate();
}
return true;
}
});
requestFocus();
}
@Override
public void onDraw(Canvas canvas) {
for (Point point : points) {
canvas.drawCircle(point.x, point.y, pSize, paint);
}
}
public void setPoints(Float xP, Float yP)
{
Point point = new Point();
point.x = xP;
point.y = yP;
points.add(point);
postInvalidate();
}
}
class Point {
float x, y;
@Override
public String toString() {
return x + ", " + y;
}
}
请告诉我,如何获取canvas的setPoints函数?
Please tell me, how get canvas out setPoints function?
更新:哇,这真是一个有趣的问题.我的DrawView包含在HorizontalScrollView中.因为如果我在此DrawView中设置正确的坐标,则没人会知道可绘制的圆在哪里.
Update:Wow, it's really interesting problem. My DrawView contains in HorizontalScrollView. Because if I set in this DrawView right coordinates, no one knows where are drawable circles.
推荐答案
您不能.画布由系统管理,并传递到您的onDraw()
.我不明白为什么您在外面需要它.只是这样重新声明setPoints
You can't. The canvas is managed by the system and is passed to your onDraw()
. I don't understand why you'd need it outside of there. Just redeclare setPoints
like this
public void setPoints(Canvas canvas, Float xP, Float Yp)
您可以保留以前的图形的缓存(或存储以前的点)
You can keep a cache of the previous drawings (or store the previous points)
这篇关于如何获得当前的画布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!