1

Im trying to make a floating activity and on click open up transparent canvas for drawing. This content is drawing but i want make it transparent .

what i tried is:

final DrawingView drawingView = (DrawingView) findViewById(R.id.drawing);

My DrawingView Class:

import java.util.ArrayList; 
import android.content.Context;
import android.graphics.*;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.hardware.Camera.Size;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Pair;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class DrawingView extends View implements OnTouchListener {
    private Canvas m_Canvas;

    private Path m_Path;

    private Paint m_Paint;

    ArrayList<Pair<Path, Paint>> paths = new ArrayList<Pair<Path, Paint>>();

    ArrayList<Pair<Path, Paint>> undonePaths = new ArrayList<Pair<Path, Paint>>();

    private float mX, mY;

    private static final float TOUCH_TOLERANCE = 4;

    public static boolean isEraserActive = false; 

    public DrawingView(Context context, AttributeSet attr) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);
         this.setBackgroundColor(Color.TRANSPARENT);                

        this.setOnTouchListener(this);

        onCanvasInitialization();
    }

    public void onCanvasInitialization() {
        m_Paint = new Paint();
        m_Paint.setAntiAlias(true);
        m_Paint.setDither(true);
        m_Paint.setColor(Color.parseColor("#000000")); 
        m_Paint.setStyle(Paint.Style.STROKE);
        m_Paint.setStrokeJoin(Paint.Join.ROUND);
        m_Paint.setStrokeCap(Paint.Cap.ROUND);
        m_Paint.setStrokeWidth(30);

        m_Canvas = new Canvas();
        m_Canvas.drawColor(Color.argb(0, 255, 255, 255));

        m_Path = new Path();
        Paint newPaint = new Paint(m_Paint);
        paths.add(new Pair<Path, Paint>(m_Path, newPaint));

    }

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

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

    @Override
    protected void onDraw(Canvas canvas) {
        for (Pair<Path, Paint> p : paths) {
            canvas.drawPath(p.first, p.second);
        }
    }

    private void touch_start(float x, float y) {

        if (isEraserActive) {
            m_Paint.setColor(Color.WHITE);
            m_Paint.setStrokeWidth(6);
            Paint newPaint = new Paint(m_Paint); // Clones the mPaint object
            paths.add(new Pair<Path, Paint>(m_Path, newPaint));

        } else { 
            m_Paint.setColor(Color.BLACK);
            m_Paint.setStrokeWidth(30);
            Paint newPaint = new Paint(m_Paint); // Clones the mPaint object
            paths.add(new Pair<Path, Paint>(m_Path, newPaint));

        }


        m_Path.reset();
        m_Path.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) {
            m_Path.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            mX = x;
            mY = y;
        }
    }

    private void touch_up() {
        m_Path.lineTo(mX, mY);

        // commit the path to our offscreen
        m_Canvas.drawPath(m_Path, m_Paint);

        // kill this so we don't double draw
        m_Path = new Path();
        Paint newPaint = new Paint(m_Paint); // Clones the mPaint object
        paths.add(new Pair<Path, Paint>(m_Path, newPaint));
    }

My Activity Main XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawing_question"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:orientation="horizontal"
    android:visibility="visible" >

                <com.swap.handdrawing.DrawingView
                    android:id="@+id/drawing"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_marginBottom="0dp"
                    android:layout_marginRight="0dp"
                    android:background="@null" >

                </com.swap.handdrawing.DrawingView>

</LinearLayout>

& how can i make it like this: Like this

Abraham K
  • 624
  • 1
  • 8
  • 24
  • Have you tried searching for similar questions like this one: http://stackoverflow.com/questions/8703581/how-to-make-canvas-draw-area-transparent-in-android – Finnboy11 Jun 21 '14 at 12:38
  • @Finnboy11 Yah but thats SurfaceView and This implements View...or can it be done ? – Abraham K Jun 21 '14 at 12:42
  • Your question is about making the canvas transparent. Views should have nothing to do with making a canvas transparent but of course you want to make the View transparent too if you want the whole thing to be transparent. Also, SurfaceView extends View so the methods should be same... By the way, why do you have a separate canvas object m_Canvas if you don't show it? – Finnboy11 Jun 21 '14 at 12:51
  • In addition to that, please make sure that your activity is also transparent. http://stackoverflow.com/questions/2176922/how-to-create-transparent-activity-in-android – Finnboy11 Jun 21 '14 at 12:59

0 Answers0