如何在canvas android中设置验证?

前端之家收集整理的这篇文章主要介绍了如何在canvas android中设置验证?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用画布应用程序,我创建了一个带有颜色选择器,撤消,重做和清除功能的画布,它也工作正常.现在,我想验证用户当前是否在画布上的背景线上绘制一条线.

例如,
我有一个带虚线的背景图像,现在我希望用户在虚线上绘图,我必须验证它是否正确?在画布中是否可以,我该如何处理?

有人可以给出任何关于这个的建议,这对我来说真的很有帮助,我可以继续我的应用程序的下一步,我的代码在这里,

public class HomeScreen extends Activity implements ColorPickerDialog.OnColorChangedListener {
    public int width;
    public int height;
    public Arrays paths1;
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Path mPath;
    private Paint mBitmapPaint;
    Context context;
    private Paint circlePaint;
    private Path circlePath;
    /* MyView mv; */
    DrawingPanel dp;
    AlertDialog dialog;
    private ArrayList<Path> undonePaths = new ArrayList<Path>();
    private ArrayList<Path> paths = new ArrayList<Path>();
    FrameLayout frmLayout;
    Canvas canvas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_home_screen);
        dp = new DrawingPanel(this);

        frmLayout = (FrameLayout) findViewById(R.id.frameLayout);

        frmLayout.addView(dp);

        ((Button) findViewById(R.id.Clear)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                paths = new ArrayList<Path>();

                if (paths != null)
                    paths.clear();
                if (dp != null)
                    dp.invalidate();
            }
        });

        ((Button) findViewById(R.id.Undo)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (paths.size() > 0) {
                    undonePaths.add(paths.remove(paths.size() - 1));
                    dp.invalidate();
                }
            }
        });

        ((Button) findViewById(R.id.Redo)).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (undonePaths.size() > 0) {
                    paths.add(undonePaths.remove(undonePaths.size() - 1));
                    dp.invalidate();
                }
            }
        });

        ((Button) findViewById(R.id.letters)).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent(HomeScreen.this,Letters.class));
            }
        });

        ((Button) findViewById(R.id.color)).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                  mCanvas=new Canvas();
                new ColorPickerDialog(HomeScreen.this,HomeScreen.this,mPaint.getColor()).show();

            }
        });

        dp.setDrawingCacheEnabled(true);
        Bundle extras = getIntent().getExtras();
        Integer userName;
        if (extras != null) {
            userName = extras.getInt("name");
            // and get whatever type user account id is
        }

        dp.setBackgroundResource(R.drawable.atemplate);// set the back
        // ground if you wish
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFFFF0000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(20);

        mEmboss = new EmbossMaskFilter(new float[] { 1,1,1 },0.4f,6,3.5f);
        // mBlur = new BlurMaskFilter(8,BlurMaskFilter.Blur.NORMAL);
    }

    private Paint mPaint;
    private MaskFilter mEmboss;
    private MaskFilter mBlur;

    public void colorChanged(int color) {
        mPaint.setColor(color);
    }

    public class DrawingPanel extends View implements OnTouchListener {

        private Canvas mCanvas;
        private Path mPath;
        private Paint mPaint,circlePaint,outercirclePaint;

        // private ArrayList<Path> undonePaths = new ArrayList<Path>();
        private float xleft,xright,xtop,xbottom;

        public DrawingPanel(Context context) {
            super(context);
            setFocusable(true);
            setFocusableInTouchMode(true);
            this.setOnTouchListener(this);
            circlePaint = new Paint();
            mPaint = new Paint();
            outercirclePaint = new Paint();
            outercirclePaint.setAntiAlias(false);
            circlePaint.setAntiAlias(false);
            mPaint.setAntiAlias(false);
            mPaint.setColor(0xFF000000);
            outercirclePaint.setColor(0x44FFF000);
            circlePaint.setColor(0xF57F35);
            outercirclePaint.setStyle(Paint.Style.FILL_AND_STROKE);
            circlePaint.setStyle(Paint.Style.FILL);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.MITER);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(10);
            outercirclePaint.setStrokeWidth(10);
            mCanvas = new Canvas();
            mPath = new Path();
            paths.add(mPath);
        }

        public void colorChanged(int color) {
            mPaint.setColor(color);
        }

        @Override
        protected void onSizeChanged(int w,int h,int oldw,int oldh) {
            super.onSizeChanged(w,h,oldw,oldh);
        }

        @Override
        protected void onDraw(Canvas canvas) {

            for (Path p : paths) {
                canvas.drawPath(p,mPaint);
            }

        }

        private float mX,mY;
        private static final float TOUCH_TOLERANCE = 0;

        private void touch_start(float x,float y) {
            mPath.reset();
            mPath.moveTo(x,y);
            mX = x;
            mY = y;
        }

        private void touch_move(float x,float y) {
            float dx = Math.abs(x - mX);
            float dy = Math.abs(y - mY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                mPath.quadTo(mX,mY,(x + mX) / 2,(y + mY) / 2);
                mX = x;
                mY = y;
            }
        }

        private void touch_up() {
            mPath.lineTo(mX,mY);
            // commit the path to our offscreen
            mCanvas.drawPath(mPath,mPaint);
            // kill this so we don't double draw
            mPath = new Path();
            paths.add(mPath);
        }

        @Override
        public boolean onTouch(View arg0,MotionEvent event) {
            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // if (x <= cx+circleRadius+5 && x>= cx-circleRadius-5) {
                // if (y<= cy+circleRadius+5 && cy>= cy-circleRadius-5){
                // paths.clear();
                // return true;
                // }
                // }
                touch_start(x,y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(x,y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                break;
            }
            return true;
        }
    }
}

解决方法

您是否正在考虑创建一个类似于模式锁定器的应用程序,它们在Android设备中,尝试以这种方式实现.
@Override
public boolean onTouchEvent(MotionEvent event) {
    // MotionEvent object holds X-Y values
    if(event.getAction() == MotionEvent.ACTION_DOWN) {
        String text = "You click at x = " + event.getX() + " and y = " + event.getY();
        Toast.makeText(this,text,Toast.LENGTH_LONG).show();
    }

    return super.onTouchEvent(event);
}

this link may help

猜你在找的Android相关文章