如何在Android中创建刮刮卡? [英] How to create a Scratch Card in Android?

查看:96
本文介绍了如何在Android中创建刮刮卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为学校的最后一个项目创建一个刮刮卡"应用,但找不到实现刮擦事件的方法(如何创建背景图片并在其上方放置灰色矩形,所以当我将刮开这些矩形,我会看到它们下面的图片

I need to create a "Scratch Card" App for my final project in school and can't find the way how to implement the scratching event (How do I create background image and put Grey rectangles over it, so when I will scratch those rectangles I will see the picture under them)

该实现必须在Android中使用,因为我还不知道如何在Objective-C中进行开发.

The Implementation must be in Android because I don't how to develop In Objective-C yet.

我看到了Objective-C实现的参考,但这不好,因为我不理解.

I saw a reference for Objective-C Implementation, but it's no good as I don't understand it.

我的代码是:

public class FingerPaint extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            MyView myView = new MyView(this);
            myView.requestFocus();
            myView.PaintObjectInit();
            // setContentView(myView);

            LinearLayout upper = (LinearLayout) findViewById(R.id.LinearLayout01);
            upper.addView(myView);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        // MyImageView myImageView = new MyImageView(this);
        // setContentView(myImageView);
    }
}



public class MyView extends View {

    private Paint mPaint;
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Path mPath;
    private Paint mBitmapPaint;

    public MyView(Context context) {
        super(context);
        this.mPaint = new Paint();
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    }

    protected void PaintObjectInit() {
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        //mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        //mPaint.setColor(0xFFFF0000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);
    }

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

        try
        {


        //Bitmap bm1 = BitmapFactory.decodeResource(this.getResources(),R.drawable.scratch).copy(Bitmap.Config.ARGB_8888, true);;
        //Bitmap bm2 = BitmapFactory.decodeResource(this.getResources(),R.drawable.main).copy(Bitmap.Config.ARGB_8888, true);;

        //mBitmap = toTransparency(bm1, 0xFFAAAAAA, true, bm2);

        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        }
        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawColor(0xFFAAAAAA);
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        canvas.drawPath(mPath, mPaint);
    }

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

    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.reset();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            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;
    }
}

请对此提供帮助.

推荐答案

我最近遇到了这个问题,然后我为此创建了一个库,以便每个人都可以快速实现草稿视图,希望这对仍在寻找内容的人有所帮助寻求答案 https://github.com/winsontan520/Android-WScratchView

I come across this problem recently and then I created a library for this so everyone can have a quick implementation of scratch view, hope this can help those who still looking for answer https://github.com/winsontan520/Android-WScratchView

这篇关于如何在Android中创建刮刮卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆