Pixelated Drawing Android - PullRequest
       22

Pixelated Drawing Android

0 голосов
/ 10 мая 2019

Я написал собственный класс представления, который сможет рисовать сетку и выбирать правильные координаты 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;
    }
}

Это приводит к непредсказуемому поведению и неправильному поведению. Что мне нужно, так это вид рисования окон попиксельным рисунком, какие-либо идеи или идеи?

...