2

在我的应用程序中,我需要实现两个分层视图。一个包含位图图像,另一个是普通的背景颜色。当用户进行任何触摸事件时,它将擦除颜色,背景图像将逐渐出现在前面。为此,我实现了 Canvas。但我不能这样做。请指导我这种方法是否好或有其他方法可以做这种事情。

这是我的代码,

package com.drawline.view;
        import com.drawline.R;
        import android.content.Context;
        import android.graphics.Bitmap;
        import android.graphics.BitmapFactory;
        import android.graphics.Canvas;
        import android.graphics.Color;
        import android.graphics.Paint;
        import android.graphics.Paint.Style;
        import android.graphics.Path;
        import android.graphics.PorterDuff;
        import android.graphics.PorterDuffXfermode;
        import android.util.Log;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.MotionEvent;
        import android.view.View;

        public class imageview extends View
        {

            private Paint paint;

           Context ctx;
           Bitmap bmpImg;
           private Canvas  mCanvas;
           private Path    mPath;
            public imageview(Context context)
            {
                super(context);
                this.ctx=context;
                mCanvas = new Canvas();
                mPath=new Path();
                init(ctx);    
            }

            private void init(Context ctx)
            {

                paint = new Paint();
                paint.setColor(Color.RED);
                paint.setStyle(Style.STROKE);
                paint.setStrokeWidth(6);
                paint.setAntiAlias(true);
                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
                android.content.res.Resources res = ctx.getResources();
                bmpImg=BitmapFactory.decodeResource(res, R.drawable.priyanka);      
            }
            @Override
            protected void onDraw(Canvas canvas)
            {

                canvas.drawColor(Color.BLUE); 
                canvas.drawPath(mPath, paint);
            }
            public int  getLineArrayIndex(){
                return lineArrIndex;
            }
            float endPosX; 
            float endPosY ; 
            float startPosX;
            float startPosY;
            int lineArrIndex = 0;
            float prevX=0;
            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);
                mCanvas.drawPath(mPath, paint);
                mCanvas.drawColor(Color.TRANSPARENT);
                // kill this so we don't double draw
                mPath.reset();
            }
            public boolean onTouch(View v,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;
            }

        }
4

0 回答 0