Why does this code only draw a circle once? I cannot for the life of me figure it out. Do I need to do some kind of refresh or something? I am able to get a red dot, to draw once, but any click after does not show a new dot, or even move the previous one.
package ball.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class BallActivity extends Activity {
/** Called when the activity is first created. */
BallView bv;
int i = 0;
TextView tv;
//float x = 20;
//float y = 20;
float r = 20;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.top);
LinearLayout main = (LinearLayout) findViewById(R.id.main_view);
//main.addView(new BallView(this, 20, 20, 20));
main.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
i++;
float x = event.getX();
float y = event.getY();
tv.setText("Clicks: " + i + "X: " + x + "Y: " + y);
LinearLayout ll = (LinearLayout) v;
ll.addView(new BallView(ll.getContext(), x, y, 25));
return false;
}
});
}
}
package ball.test;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.View;
public class BallView extends View{
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
float x;// = 20;
float y;// = 20;
float r;// = 20;
public BallView(Context context, float x, float y, float r) {
super(context);
this.x = x;
this.y = y;
this.r = r;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.RED);
//paint.setStyle(Style.FILL_AND_STROKE);
//canvas.drawColor(Color.WHITE);
canvas.drawCircle(x, y, r, paint);
}
}
Change your LinearLayout to an AbsoluteLayout. I think what's happening is that your first BallView is actually taking up the entirety of the LinearLayout view group and any other views you add to it are being pushed out of the layout.
Also, look into using addView(View, AbsoluteLayout.LayoutParams) instead, so you can set the size/position of the ball there as opposed to in BallView.onDraw, which will allow smaller regions of your layout to be marked dirty.
Related
I found some code to draw line and now i wand drawing line progressively so that i cloud see it being drawn.
This is the code
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
paint.setColor(Color.BLACK);
}
#Override
public void onDraw(Canvas canvas) {
canvas.drawLine(0, 0, 20, 20, paint);
canvas.drawLine(20, 0, 0, 20, paint);
}
}
How can i do that?
Tnx
Did you see that?
Look at source code ;)
http://www.curious-creature.com/2013/12/21/android-recipe-4-path-tracing/
You will want to break up your drawing into multiple steps. Inside your onDraw call, you will want to draw a part of your line, and update a variable so that the next line segment is drawn. Then you will want to make multiple onDraw() calls in an animation loop. You will need to be careful where you make your calls to the animation loop from. Read about the View class for more information, particular event handling and threading. http://developer.android.com/reference/android/view/View.html
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class DrawView extends View {
Paint paint = new Paint();
float x1 = 0;
float x2 = 20;
float y1 = 0;
float y2 = 20;
public DrawView(Context context) {
super(context);
paint.setColor(Color.BLACK);
}
#Override
public void onDraw(Canvas canvas) {
if(doClear) {
//clear canvas to begin new animation
}
canvas.drawLine(x1, y1, x2, y2, paint);
}
public void animateLoop() {
while(x1 < 500) {
x1 += 20;
y1 += 20;
x2 += 20;
y2 += 20;
//tell android this view needs to be redrawn
invalidate();
}
//when done set doClear to true so
}
If you really want to learn about animation, you should start with something like this example: http://developer.android.com/guide/topics/graphics/drawable-animation.html.
I use this codes for draw a polygon on a canvas. But I want to calculate the area of polygon. Of course give the measurements of each line. I see a lot of example on maps but ı don't convert/adapt on canvas. Can anyone showing a way or method ?
Thanks in advance.
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new DrawingView(MainActivity.this));
}
class DrawingView extends SurfaceView
{
private SurfaceHolder surfaceHolder;
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private List<Point> pointsList = new ArrayList<Point>();
public DrawingView(Context context)
{
super(context);
surfaceHolder = getHolder();
paint.setColor(Color.BLACK);
paint.setStyle(Style.FILL);
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
if (surfaceHolder.getSurface().isValid())
{
// Add current touch position to the list of points
pointsList.add(new Point((int) event.getX(), (int) event.getY()));
// Get canvas from surface
Canvas canvas = surfaceHolder.lockCanvas();
// Clear screen
canvas.drawColor(Color.WHITE);
// Iterate on the list
for (int i = 0; i < pointsList.size(); i++)
{
Point current = pointsList.get(i);
Point first = pointsList.get(0);
// Draw points
canvas.drawCircle(current.x, current.y, 5, paint);
// Draw line with next point (if it exists)
if (i + 1 < pointsList.size())
{
Point next = pointsList.get(i + 1);
canvas.drawLine(current.x, current.y, next.x, next.y, paint);
canvas.drawLine(next.x, next.y, first.x, first.y, paint);
c
}
}
// Release canvas
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
return false;
}
}
}
Here's a method for calculating the area of a polygon.
for (int i = 0; i < points.size(); i++) {
float addX = points.get(i).x;
float addY = points.get(i == points.size() - 1 ? 0 : i + 1).y;
float subX = points.get(i == points.size() - 1 ? 0 : i + 1).x;
float subY = points.get(i).y;
total += (addX * addY * 0.5);
total -= (subX * subY * 0.5);
}
return Math.abs(total);
I am developing mind mapping tool for android. Till now the objects are drawn on screen ontouch event. I want to create one main object and draw this type of lines from that object. The example is,
http://www.biggerplate.com/mapImages/xl/e666ca33-abf7-4cc7-acc9-4aea9487feef.png
Please Help.
This is the class to draw a node touch events
package com.example.mindmapping;
import java.util.LinkedList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
public class CreateBitmap extends Activity {
Bitmap bitmap;
SurfaceHolder holder;
//LinkedList<newNode> nodes;
LinkedList<Node> nodes;
float x, y;
Node node;
Boolean collision;
SurfaceView surfaceView;
Context baseCtx;
//DrawMindMap dm;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
baseCtx = this.getBaseContext();
setContentView(R.layout.drawmindmap);
nodes = new LinkedList<Node>();
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rectangle_small);
surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
holder = surfaceView.getHolder();
surfaceView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("TOUCH", "On touch start, total nodes: " + String.valueOf(nodes.size()));
x = event.getX();
Log.d("XPOS", "touch x" + String.valueOf(x));
y = event.getY();
Log.d("YPOS", "touch y" + String.valueOf(y));
Canvas c = holder.lockCanvas();
//c.drawColor(Color.BLACK);
Node n = new Node(bitmap, c, x, y);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(6f);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
collision = false;
//nodes.add(n);
if (!nodes.isEmpty()){
for (Node no : nodes) {
collision = no.isHere(x, y);
if (collision){
break;
}
}
}
if(!collision){
nodes.add(n);
}
c.drawColor(Color.BLACK);
if (!nodes.isEmpty()){
for (Node no : nodes) {
no.Draw();
c.drawLine(x, y, event.getX(), event.getY(), paint);
Log.d("Node POS","Node X,Y: " + String.valueOf(no.xPosition) + " " + String.valueOf(no.yPosition));
}
}
holder.unlockCanvasAndPost(c);
break;
}
return true;
}
});
}
In the class given below is used to handle and draw node
package com.example.mindmapping;
import android.graphics.Bitmap;
import android.graphics.Canvas;
public class Node {
public Bitmap img;
public Canvas c;
public float xPosition, yPosition, topLeft;
int width, height,gResId;
boolean isItOk;
public Node(Bitmap b,Canvas canv, float xP, float yP) {
img = b;
c = canv;
width = b.getWidth();
height = b.getHeight();
xPosition = xP - (width/2);
yPosition = yP - (height/2);
}
public void SetPos( float xP, float yP) {
xPosition = xP - (width/2);
yPosition = yP - (height/2);
}
public boolean isHere(float x, float y){
x = (float) (x + width * 0.5);
y = (float) (y + height * 0.5);
if((x > xPosition - width * 0.5) && (x < xPosition + width * 0.5) && (y > yPosition - height * 0.5) && (y < yPosition + height * 0.5) ){
return true;
}
return false;
}
//#SuppressWarnings("static-access")
public void Draw() {
c.drawBitmap(img, xPosition, yPosition, null);
}
}
Here I have an Activity that sets a custom view based on a separate class(menuAnimation).
package nick.game.breakout;
import android.app.Activity;
import android.os.Bundle;
public class GameMenu extends Activity {
menuAnimation myMenu;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
myMenu = new menuAnimation(this);
setContentView(myMenu);
}
}
This is the myMenu View. It simply bounces a ball image around the screen. My issue now is not knowing how to add a button to this view because I want this view to be a simple 2 button menu with the ball bouncing around in the background. I know how to add a button through XML with a content view that uses an xml layout but I am lost when using this custom view.
package nick.game.breakout;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class menuAnimation extends View {
Bitmap ball;
Paint paint = new Paint();
int dx;
int dy;
int vx = 5;
int vy = 5;
public menuAnimation(Context context) {
super(context);
this.setBackgroundColor(Color.parseColor("#2186ed"));
ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
}
public void onDraw(Canvas canvas) {
drawBall(dx,dy, canvas);
dx = dx + vx;
dy = dy + vy;
if (dx < 0 || dx > canvas.getWidth() - 10) vx = -vx;
if (dy < 0 || dy > canvas.getHeight() - 10) vy = -vy;
invalidate();
}
private void drawBall(int x2, int y2, Canvas canvas) {
canvas.drawBitmap(ball, x2, y2, paint);
}
}
Any help much appreciated.
In your code you are setting current view of the page to myMenu using following line.
setContentView(myMenu);
You will get a view of whatever you have in your myMenu object.
Your myMenu Object extends View object. So Basically myMenu is a view.
You can create a button and add it to myMenu object and it will show a button.
something like
Button button = new Button(context); // or use this
myMenu.addView(button);
I want to draw a circle on a canvas and have it leave a trail, rather draw a new circle each time. How would I go about doing this? All I can get it to do is move the circle around.
-CanvasTest Class
package canvas.test;
import android.app.Activity;
import android.os.Bundle;
public class CanvastestActivity extends Activity {
/** Called when the activity is first created. */
float x = 80;
float y = 20;
float r = 15;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Draw2D d = new Draw2D(this, x, y, r);
try {
Thread.sleep(100);
x++;
y++;
} catch(InterruptedException e) {}
setContentView(d);
}
}
--Draw2D Class
package canvas.test;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class Draw2D extends View {
float x;
float y;
float r;
public Draw2D(Context context, float x, float y, float r) {
super(context);
this.x = x;
this.y = y;
this.r = r;
}
#Override
protected void onDraw(Canvas c) {
super.onDraw(c);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
c.drawPaint(paint);
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
c.drawCircle(x, y, r, paint);
}
}
This is my most recent test. Why would the circle now move? It's does not move at all.
Are you doing something like canvas.drawColor(Color.TRANSPARENT) or canvas.drawColor(Color.BLACK) in the begining of your doDraw method?
If you omit that call it should not clear the canvas and leave the trails you are looking for.