i need some help on my android project.
i have a canvas and i fill it with some bitmap.
and i have a pointer that drawn a line on the canvas.
my problem is how to clean the line that i've drawn before?
what method should i call on canvas?
i've tried Canvas.drawColor(), invalidate() and that's not working.
and what is the function of Canvas.drawColor() and
Please help me solve my problem.
thanks in advance
UPDATE!
if i made the code like this:
#Override
protected void onDraw(Canvas canvas) {
// fills the canvas with black
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, p);
obaby.draw(canvas);
}
where i place invalidate() in my code?
and what code should i use if i want to clear the canvas using a button?
UPDATE!
i wrote my onDraw like this:
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
if(letsdraw){
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, p);
obaby.draw(canvas);
}
}
and the method in reset button is like this:
public void rst(){
letsdraw = false;
invalidate();
Log.v("tag", "this method called");
}
but no change in the canvas when i called the method.
did i wrote something wrong on the code above?
Make your logic like this. Draw the line on the Canvas with some condition. Check if you want to draw the line, then draw the line.
Skeleton code -
#Override
protected void onDraw(Canvas canvas)
{
if(needToDrawLine)
{
//draw the line
}
// Other drawing stuff
}
Now just update your needToDrawLine variable and call invalidate(). You'll get your result. Let me know if it works.
Update:
onDraw() method will call everytime you call the invalidate(). So everyting inside the onDraw() will execute. The way is, you have to prevent it from drawing some of the part. You'll call invalidate() when you want to redraw the whole view, for example - button for clear the canvas.
canvas.drawColor(Color.BLACK); this line clear your whole view to BLACK color.
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); this line draw the bitmap at (0,0).
canvas.drawPath(mPath, p); this line draw the path mPath.
obaby.draw(canvas); some other object draw itself.
Now - for example you want to clear the screen, when button pressed. Just initialize a variable if it draw everything. And update the variable in button click.
public boolean drawEverything = true;
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
if(drawEverything)
{
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, p);
obaby.draw(canvas);
}
}
public void buttonClicked( ... )
{
drawEverything = false;
}
Iam useing this errage the paint in my activity
mBitmap.eraseColor(Color.TRANSPARENT);
mPath.reset();
mView.invalidate();
Related
Is it possible to rotate an ImageButton using the onCreate() function? Or do you have to use an Animation which starts onCreate()? Because with an Animation i can see a little "flick" on Activity start...
you can use ViewCompat.setRotation(buttonInstance, rotationAngle);. From the documentation
Sets the degrees that the view is rotated around the pivot point.
You can override the onDraw() method using a custom class that extends ImageButton (which I'm sure you have).
#Override
protected void onDraw(#NonNull Canvas canvas) {
super.onDraw(canvas);
// Rotate a Bitmap
final Matrix matrix = new Matrix();
matrix.setRotate(angle, imageCenterX, imageCenterY);
canvas.drawBitmap(bitmap, matrix, null);
/*
* OR
**/
// Rotate the canvas
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.rotate(-angle);
canvas.drawBitmap(bitmap, left, top, null);
canvas.restore();
}
Choose one or the other solution, not both together ;)
EDIT
After some quick reflection, that could also work (not tested):
#Override
protected void onDraw(#NonNull Canvas canvas) {
canvas.rotate(-angle);
super.onDraw(canvas);
}
I have 3 ImageViews. 1st and 2nd connected with red line. Also I have simple button. Here is a picture:
I want to connect 2nd & 3rd ImageViews with new Path line and change the first line color (for example to Green) when i clicking my button. Here is parts of my code:
public class SkillPath extends View {
Paint paint;
Path path;
... constructors
#Override
protected void onDraw(Canvas canvas) {
addPath (canvas);
}
//Here is my RED line
void addPath (Canvas canvas){
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStrokeWidth(3);
path.moveTo(110, 110);
path.lineTo(210, 110);
canvas.drawPath(path, paint);
Log.d ("Page 2","onDraw");
}
I can get all coordinates of all Views, but how can I redraw existing canvas? I suspect, I need to use invalidate(), but I do not know enough to do this. Need help.
The invalidate() method forces the View to be re-drawn.
So just apply the modification you need on your canvas and call invalidate() on the related View after these modifications.
I would like to send each time different Drawable shape to view and then add it to my activity.
The problem is I can't find a way to add the drawable shape into the canvas when I'm overriding the onDraw method.
The design supposed to be reusable, I can draw rect first and then draw circle...
I want to find a way to send the view different shape. Is it possible?
#Override
protected void onDraw(Canvas canvas) {
// Draw the ball
ballBounds.set(ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius);
paint.setColor(Color.GREEN);
// canvas.drawOval(ballBounds, paint);
canvas.drawRect(ballBounds, paint);
}
Although, your question needs more explanation on what you need, but with what I could understand is you want rect, or circle on desire, here it is,
Add, global variable int ShapeStructure = 0;
#Override
protected void onDraw(Canvas canvas)
{
// Draw the ball
ballBounds.set(ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius);
paint.setColor(Color.GREEN);
if(ShapeStructure == 0){
canvas.drawOval(ballBounds, paint);
}else{
canvas.drawRect(ballBounds, paint);
}
}
Now that, you have a variable at your disposal, you can control what you want to paint.
Hope, this helps, let me know if you want something else.
I'm extending LinearLayout:
public class MyLinearLayout extends LinearLayout {
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(0xFFFF0000);
canvas.drawLine(0, 0, getWidth(), getHeight(), paint);
}
}
If the layout has child elements, shouldn't the red line above be drawn on top of them? Considering a layout like:
<MyLinearLayout>
<ImageView />
</MyLinearLayout>
I'd expect to get the red line drawn above the child ImageView. But it gets drawn below it. I was assuming all child drawing would have been completed after the super.onDraw() line is finished.
Is there a way to get to the canvas and draw something on it after all child drawing has been completed?
Thanks
Layouts don't draw unless you call setWillNotDraw(false);. They do that for efficiency reasons.
EDIT:
onDraw() really is meant to just allow you to modify the Canvas before the drawing operation happens. It doesn't actually draw. What you want to do is override draw() like so:
#Override
protected void draw(Canvas canvas) {
super.draw(canvas);
Paint paint = new Paint();
paint.setColor(0xFFFF0000);
canvas.drawLine(0, 0, getWidth(), getHeight(), paint);
}
Calling the super will draw all the children in the view. Then it will draw all the lines. I do still believe you need setWillNotDraw(false) to be called though.
I'm rewriting my answer as I hadn't seen your line of code where you were drawing to the canvas.
The canvas like some other graphics environments is inverted. So calling getHeight() is actually drawing the line at the bottom. Instead call
canvas.drawLine( 0, 0, getWidth(), 0, paint);
This means draw the line at the top, across the width of the view.
Also, calling super first paints the children prior to any custom painting so your fine there.
If you have a LinearLayout and you need:
draw all of its children first, like buttons, image views
then, on top of those components, draw something directly on the canvas.
You can try this:
public class YourClass extends LinearLayout
{
#Override
protected void dispatchDraw(Canvas canvas)
{
super.dispatchDraw(canvas);
// do your custom drawings afterwards
canvas.drawCircle...
canvas.drawLine...
This is the most simplified version of my view.
public class MyView extends View {
private int mBackgroundColor = android.R.color.white;
#Override
public void setBackgroundColor(int color) {
super.setBackgroundColor(color);
mBackgroundColor = color;
invalidate();
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(mBackgroundColor);]
//canvas.drawColor(mBackgroundColor, Mode.CLEAR);
//canvas.drawBitmap(mBitmap, mRectSrc, mRectDst, mPaint);
}
}
the problem is the background color does not change when the setBackgroundColor is called from the activity. i guess i'am not seeing the obvious.
SOLVED:
took hint from nmJohn about clipRect
//Clear the screen
canvas.clipRect(0, 0, viewWidth, viewHeight, Region.Op.REPLACE);
mPaint.setColor(mBackgroundColor);
canvas.drawRect(0, 0, viewWidth, viewHeight, mPaint);
//Draw the image
canvas.clipRect(mRectDst.left, mRectDst.top, mRectDst.right, mRectDst.bottom, Region.Op.REPLACE);
canvas.drawBitmap(mBitmap, mRectSrc, mRectDst, mPaint);
one more hint for the future seekers
please make sure that android.graphics.Color i.e hex format is supplied rather that android.R.color.[White/Black/..] took be unaware for a while.
drawColor just sets the background color of the current clip. Try setting clipRect to the area of the canvas.
Also, make sure your view is actually getting invalidated, otherwise onDraw will not get called.