I want to make the squares in a row - android

Hi friends I want to make the squares in a row but I could not. My code is below. Can you help me?
int x=10,y=10;
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint p= new Paint();
for(int i=1;i<6;i++)
{
canvas.drawBitmap(kutu, x,y, p);
x +=50;
}
invalidate();
}
Thank you for helping.

Two issues:
You need to reset x at the start of your onDraw method (Ideally, x should be a local variable, not a field)
Remove the call to invalidate

Related

Android - redraw 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.

Insert Drawable shape to View

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.

How do i clean drawn path on canvas in android

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();

Android graphics outside of onDraw method

Okay, so I'm trying to draw to a canvas on Android from outside of the onDraw method.
It's just easiest to show my code:
public class TestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Paint p = new Paint();
p.setColor(Color.GREEN);
Panel a = new Panel(this,150,150,50,p);
a.drawThing();
setContentView(a);
}
class Panel extends View{
private float radius, x, y;
private Canvas CAN;
private Paint p;
public Panel(Context context, float x, float y, float radius, Paint p){
super(context);
this.x = x;
this.y = y;
this.radius = radius;
this.p = p;
}
#Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
CAN = canvas;
}
public void drawThing(){
CAN.drawCircle(x, y, radius, p);
}
}
}
Do you see what I'm trying to do? But for some reason it throws a NullPointerException
Many of the graphics resources are explicitly freed/released after they've been used. I'm not exactly sure why they do this, but whatever the reason, they don't you to do what you're trying.
Instead of drawing outside of the onDraw method, use some kind of flag to change what the onDraw method is doing. When you want to draw some specific thing, you can set the right flag, and call invalidate().
#Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
if (doThing) {
canvas.drawCircle(x, y, radius, p);
}
}
EDIT
Something else to consider is drawing to and "off-scrren" source. This means using some kind of graphics representation like a bitmap as a buffer that you can draw to in other code. This won't update your gui, but it will give you the chance to do some heavy duty drawing without locking up the user's device. Once you are done drawing to the bitmap (or whatever) you can invalidate your view and draw it to the screen in the onDraw(Canvas) method.
I'm pretty sure that the null pointer happens because you're calling drawSomething before onDraw ever gets called. So CAN is null.
You can draw onto canvas outside of the onDraw. See this Can we have two canvases in an activity ? (OR) Having a canvas outside the onDraw() is not working for more info.

Android OnTouch events numerous Objects

ok I'm playing w/ ontouch events extending a view.
what I've done is made a circle on touch.. the cirlce will follow as you move. As you move another circle is made and will sit in the postion decrementing the radius until it disappears.. (right now up to like 10 circles). I can also handle multiple fingers touching at one point in time. Here's the problem.. THE CODE IS NASTY!
To create multiple circle This is my paint method:
public void onDraw(Canvas canvas)
{
paint.setColor(Color.RED);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(stroke);
canvas.drawCircle(x,y,radius,paint);
canvas.drawCircle(x1,y1,radius1,paint);
canvas.drawCircle(x2,y2,radius2,paint);
canvas.drawCircle(x3,y3,radius3,paint);
canvas.drawCircle(x4,y4,radius4,paint);
canvas.drawCircle(x5,y5,radius5,paint);
canvas.drawCircle(x6,y6,radius6,paint);
paint.setColor(Color.BLUE);
canvas.drawCircle(x7,y7,radius7,paint);
canvas.drawCircle(x8,y8,radius8,paint);
paint.setColor(Color.YELLOW);
canvas.drawCircle(x9,y9,radius9,paint);
canvas.drawCircle(x10,y10,radius10,paint);
}
so as you can see this by far inefficient and makes for some long nasty code.. Part of the issue is the fact I'm bound to only being able to change coordinates in Ontouch.. and invalidate. Anyoone know a way I can do this more efficently (in a more object orriented type approach).
First things first, start with this:
public class Circle {
public int x;
public int y;
public double radius;
public Paint paint;
/* constructors, getters & setters if you feel like ...*/
}
And put all your circles in a
ArrayList<Circle> circles = new ArrayList();
public void onDraw(Canvas canvas)
{
/*...*/
Iterator iterator = circles.iterator();
while(iterator.hasNext()) {
drawCircle(iterator.next());
}
}
public void drawCircle(Canvas canvas, Circle circle) {
canvas.drawCircle(circle.x, circle.y, circle.raidus, circle.paint);
}

Categories

Resources