我编写了一个自定义视图类,该类将设法绘制网格并选择示例触摸的正确xy坐标,现在的主要思想是逐像素绘制,我已经花了一分钟时间了,但是尚未找到方法。
public class PixelView extends View {
private static final int DEFAULT_COLOR = Color.BLACK ;
Paint mPaint;
int mViewHeight,mViewWidth;
int mXCor , mYCor = 0;
int mPixels=5;
boolean mBoxNumber=false;
Paint nPaint;
List<Corrdinates> list = new ArrayList<>();
private String TAG="TAGGED";
public PixelView(Context context) {
this(context, null);
}
public PixelView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(DEFAULT_COLOR);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.MITER);
mPaint.setStrokeCap(Paint.Cap.SQUARE);
nPaint = new Paint();
nPaint.setAntiAlias(true);
nPaint.setDither(true);
nPaint.setColor(Color.RED);
nPaint.setStyle(Paint.Style.FILL);
nPaint.setStrokeJoin(Paint.Join.ROUND);
nPaint.setStrokeCap(Paint.Cap.ROUND);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
mViewWidth = MeasureSpec.getSize(widthMeasureSpec);
mViewHeight = MeasureSpec.getSize(heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float recWidth = (float) mViewWidth/mPixels;
float recHeight = (float) mViewHeight/mPixels;
Log.d(TAG, "Coordinates of touch On Draw called");
for (int i = 0; i < mPixels; i++) {
for (int j = 0; j < mPixels; j++) {
float left = i * recWidth;
float top = j * recHeight;
if (mBoxNumber) {
if(list.isEmpty()){
break;
}else {
for (int k = 0; k < list.size(); k++) {
Corrdinates a = list.get(k);
if (a.getX() == i && a.getY() == j) {
canvas.drawRect(left, top, recHeight, recWidth, nPaint);
}
}
}
} else {
canvas.drawRect(left, top, recHeight, recWidth, mPaint);
canvas.save();
}
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
float x = event.getX();
float y = event.getY();
float disY=0;
float boxWidth = (float) mViewWidth/mPixels;
float boxHeight = (float) mViewHeight/mPixels;
int a,b = 0;
a = (int) (y/boxHeight);
b = (int) (x/boxWidth);
mBoxNumber = true;
mXCor = a;
mYCor = b;
Corrdinates corrdinates = new Corrdinates(a,b);
list.add(corrdinates);
invalidate();
}
return true;
}
}
我的座标课:
class Corrdinates {
int x=0;
int y=0;
public Corrdinates(int x, int y) {
this.x = x;
this.y = y;
}
public Corrdinates() {
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
这会产生不可预测的行为和错误的行为,我想要的是一种窗口逐像素绘制的画图,有什么见解或想法吗?