繁体   English   中英

如何在 Android 中创建刮刮卡?

[英]How to create a Scratch Card in Android?

我需要为我在学校的期末项目创建一个“刮刮卡”应用程序,但找不到如何实现刮擦事件的方法(如何创建背景图像并在其上放置灰色矩形,所以什么时候我会刮擦这些矩形我会看到他们下面的图片)

实现必须在 Android 中,因为我还不知道如何在 Objective-C 中开发。

我看到了 Objective-C Implementation 的参考资料,但是因为我不明白所以不好。

我的代码是:

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

我发现这个库非常有用。

https://github.com/sharish/ScratchView

非常容易集成

<com.cooltechworks.views.ScratchImageView
    android:id="@+id/sample_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:src="@drawable/img_sample2"
/>

在此处输入图像描述 在此处输入图像描述

您可以像这样制作“刮刮卡”应用程序。 您需要遵循以下代码,这是工作代码。 你只需要理解和实现你自己的逻辑。

public class MainActJava extends AppCompatActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        setContentView(new MyView(this, bitmap));
    }

    public class MyView extends View implements View.OnTouchListener {

        private static final float TOUCH_TOLERANCE = 4;
        private Paint mPaint;
        private Bitmap oBitmap;
        private Bitmap holder;
        private Canvas mCanvas;
        private Path mPath;
        private Paint mBitmapPaint;
        private float mX, mY;

        public MyView(Context context) {
            super(context);
        }

        public MyView(Context context, Bitmap bitmap) {
            super(context);
            setOnTouchListener(this);
            this.oBitmap = bitmap;
            this.mPaint = new Paint();
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
            init();
        }

        protected void init() {
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(35);
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            holder = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(holder);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            onDrawing(canvas);
        }

        private void onDrawing(Canvas canvas) {
            mCanvas.drawColor(0xFFAAAAAA);
            mCanvas.drawPath(mPath, mPaint);
            canvas.drawBitmap(oBitmap, getWidth()/2, getHeight()/2, mBitmapPaint);
            canvas.drawBitmap(holder, 0, 0, mBitmapPaint);
        }

        private void touch_start(float x, float y) {
            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);
            mCanvas.drawPath(mPath, mPaint);
        }


        @Override
        public boolean onTouch(View view, 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;
        }
    }
}

Output:

在此处输入图像描述

DHEvents 专业和狂热的程序员。 它是 100% 免费的。

有一个关于网站本身的问题? meta 是讨论诸如什么问题是合适的、我们应该使用什么标签等事情的地方。rrrr

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM