如何擦除画布颜色并显示背景图像

时间:2012-06-14 11:19:07

标签: android android-layout android-canvas android-gesture

在我的应用程序中,我需要实现两个分层视图。一个包含位图图像,另一个包含正常的背景颜色。当用户进行任何触摸事件时,它将擦除颜色,背景图像将逐渐显示在前面。为此,我实现了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;
            }

        }

0 个答案:

没有答案