Android:为手指画应用添加背景

时间:2013-03-20 06:37:07

标签: android

我有以下手指画的代码。问题是我需要添加背景图片。我尝试将其添加到位图中,但如果我运行擦除功能,它将清除背景图像。

以下是代码:

public class MainActivity extends Activity {

    private Paint   mPaint, mBitmapPaint;
    private Path    mPath;
    private Canvas  mCanvas;
    private Bitmap  mBitmap;
    public MyView   myView;
    int screenheight;
    int screenwidth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        myView = new MyView( this );
        myView.setDrawingCacheEnabled(true);
        setContentView( myView );

        mPaint = new Paint();
        mPaint.setAntiAlias( true );
        mPaint.setColor( Color.BLACK );
        mPaint.setStyle( Paint.Style.STROKE );
        mPaint.setStrokeJoin( Paint.Join.ROUND );
        mPaint.setStrokeCap( Paint.Cap.ROUND );
        mPaint.setStrokeWidth(3);
    }

    public class MyView extends View {

        public MyView(Context c) {
            super(c);

            DisplayMetrics displaymetrics = new DisplayMetrics();
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            screenheight = displaymetrics.heightPixels;
            screenwidth = displaymetrics.widthPixels;

            mBitmap = Bitmap.createBitmap(screenwidth, screenheight, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas( mBitmap );
            mPath = new Path();
            mBitmapPaint = new Paint( Paint.DITHER_FLAG );
            mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(Color.TRANSPARENT);
            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;
        }
    }

    private static final int SAVE_MENU_ID = Menu.FIRST;
    private static final int ERASE_MENU_ID = Menu.FIRST + 1;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, SAVE_MENU_ID, 0, "Save");
        menu.add(0, ERASE_MENU_ID, 0, "Erase");
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        mPaint.setXfermode(null);
        switch (item.getItemId()) {
            case SAVE_MENU_ID:
                return true;
            case ERASE_MENU_ID:
                mBitmap.eraseColor(Color.TRANSPARENT);
                mPath.reset();
                myView.invalidate();
                }
        return super.onOptionsItemSelected(item);
    }
}

请告知如何在位图下方添加背景图像(待绘制)而不会被位图阻挡。

2 个答案:

答案 0 :(得分:0)

只需使用myView.setBackground(Drawable)。

答案 1 :(得分:0)

而不是将MyPaint设置为MainActivity内容视图,而是将其放在其他RelativeLayout内,并使用您的图片作为该布局的背景。同时使MyView位图透明:

canvas.drawBitmap(mBitmap, 0, 0, Color.TRANSPARENT);