Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
686 views
in Technique[技术] by (71.8m points)

image - How to make Canvas draw area transparent in android?

I would like to make Canvas area transparent because I would like to add an Image behind it so that Canvas actions happen above the image.My code for the canvas is here

public class Panel extends SurfaceView implements SurfaceHolder.Callback {

private ViewThread mThread;
private ArrayList<Element> mElements = new ArrayList<Element>();

public Panel(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    getHolder().addCallback(this); 
    mThread = new ViewThread(this); 
} 


public void doDraw(Canvas canvas) {
    canvas.drawColor(Color.TRANSPARENT);
    synchronized (mElements) {
        for (Element element : mElements) {
            element.doDraw(canvas);
        }
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    // TODO Auto-generated method stub
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    if (!mThread.isAlive()) {
        mThread = new ViewThread(this);
        mThread.setRunning(true);
        mThread.start();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    if (mThread.isAlive()) {
        mThread.setRunning(false);
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    synchronized (mElements) {
        mElements.add(new Element(getResources(), (int) event.getX(), (int) event.getY()));
    }
    return super.onTouchEvent(event);
}

}

How to achieve it, any snippets on it will be very much helpful.Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I got the output by this

 public Panel(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.setBackgroundColor(Color.TRANSPARENT);                 
    this.setZOrderOnTop(true); //necessary                
    getHolder().setFormat(PixelFormat.TRANSPARENT); 
    getHolder().addCallback(this); 
    mThread = new ViewThread(this); 

} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...