How to Rotate circle animation in Canvas - android

I want to rotate circle continuously in canvas on android. i am drawing circle using canvas and i am rotate circle continuously.it is possible,if possible how do it
with code or example can help me with greatly appreciated!
Here's my code for draw circle on canvas:
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
public class AnimationActivity extends Activity {
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
public class SampleView extends View
{
public SampleView(Context context)
{
super(context);
// TODO Auto-generated constructor stub
}
#Override
protected void onDraw(Canvas canvas)
{
Paint mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(10);
mPaint.setColor(Color.RED);
canvas.drawCircle(75, 75, 75, mPaint);
}
}
}
Thanks in Advance!

You can use Animation to rotate the circle you've drawn (using Canvas). The code below works. I've modified your code and added necessary changes.
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
public class AnimationActivity extends Activity {
public class SampleView extends View {
Paint mPaint = new Paint();
private Animation anim;
public SampleView(Context context) {
super(context);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(10);
mPaint.setColor(Color.RED);
}
private void createAnimation(Canvas canvas) {
anim = new RotateAnimation(0, 360, getWidth()/2, getHeight()/2);
anim.setRepeatMode(Animation.RESTART);
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(10000L);
startAnimation(anim);
}
protected void onDraw(Canvas canvas) {
int cx = getWidth()/2; // x-coordinate of center of the screen
int cy = getHeight()/2; // y-coordinate of the center of the screen
// Starts the animation to rotate the circle.
if (anim == null)
createAnimation(canvas)
canvas.drawCircle(cx, cy, 150, mPaint); // drawing the circle.
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
}
Enjoy!

canvas.rotate(-rotate_angle, rotate_center_x, rotate_center_y);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
RectF oval3 = new RectF(rotate_center_x-150, rotate_center_y-50, rotate_center_x+150, rotate_center_y+50);
canvas.drawOval(oval3, paint);
//resume original angle
canvas.rotate(rotate_angle, rotate_center_x, rotate_center_y);
For More Information, click here.. :)

Related

android dynamic 2D drawing on a Canvas

I'm trying to understand how to dynamically generate 2D graphics on Android. I haven't been able to find an example on the web of someone doing this.
My example below randomly draws 1000 lines of random colors. I'd like to see all of the lines being drawn, but I don't see anything until the setContentView() call (at which point all of the lines are already drawn).
I've tried many permutations, including moving the setContentView() to before the draw() call, and adding ourView.invalidate() inside the for loop, but in every case the screen stays white until after all the lines are drawn.
If someone can point to an example of this working, I'd be happy to go research it and add the correct answer here myself.
package com.example.graphicsdemo;
+imports
public class MainActivity extends Activity {
ImageView ourView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw();
setContentView(ourView);
}
public void draw() {
Bitmap blankBitmap = Bitmap.createBitmap(600,600,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(blankBitmap);
ourView = new ImageView(this);
ourView.setImageBitmap(blankBitmap);
Paint paint = new Paint();
Random random = new Random();
for(int i = 0 ; i < 1000 ; i++) {
paint.setColor(Color.argb(255, random.nextInt(255), random.nextInt(255), random.nextInt(255)));
canvas.drawLine(random.nextInt(600), random.nextInt(600), random.nextInt(600), random.nextInt(600), paint);
}
}
}
I hope this Example will help :
first make your own View and lets call it DynamicView :
import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class DynamicView extends View {
int i = 0;
public DynamicView(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
Random random = new Random();
paint.setColor(Color.argb(255, random.nextInt(255),
random.nextInt(255), random.nextInt(255)));
canvas.drawLine(random.nextInt(600), random.nextInt(600),
random.nextInt(600), random.nextInt(600), paint);
if (i < 1000) {
i++;
invalidate();
}
}
}
this view will invalidate a 1000 time after each draw
now here is your main activity's onCreate method :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DynamicView myView = new DynamicView(this);
setContentView(myView);
}
as simple as this.
Responding to your question this is a better code and more complicated but still easy to understand
All we have to do is to draw the lines on a bitmap and then draw that bitmap on the view, so the bitmap will not change and will have all previous drawn lines and still draw on it
now here is the updated code :
DynamicView
import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
public class DynamicView extends View {
int i = 0;
Bitmap frame ;
Canvas frameDrawer;
Rect bounds;
Paint paint ;
Random random ;
int width , height;
public DynamicView(Context context ,int width ,int height) {
super(context);
this.width = width;
this.height = height;
frame = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
frameDrawer = new Canvas(frame);
bounds = new Rect(0 , 0, width,height);
//this initialization will make the frameDrawer draw on the frame bitmap
//always avoid allocating new objects in the draw method to optimize the performance
paint = new Paint();
random = new Random();
}
#Override
protected void onDraw(Canvas canvas) {
paint.setColor(Color.argb(255, random.nextInt(255),
random.nextInt(255), random.nextInt(255)));
//instead of drawing on the view canvas draw on the frame bitmap
frameDrawer.drawLine(random.nextInt(width), random.nextInt(height),
random.nextInt(width), random.nextInt(height), paint);
// and then draw the bitmap on the view canvas
canvas.drawBitmap(frame, null, bounds , null);
if (i < 1000) {
i++;
invalidate();
}
}
}
now the new thing in the MainActivity is that now we can take controll over the view drawing bounds but still not the view's bounds which take all the screen space (if you want to adjust the view's bounds just implement the onLayout method and enter your value's )
MainActivity
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DynamicView myView = new DynamicView(this,320,480);
setContentView(myView);
}
}

Draw Square on exist bitmap image

I have a bitmap image and i want to draw a Square over this image for special position , i researched but i could not found any solution , all solutions are for create Square on empty view , please advise a i am beginner
public class DrawView extends View
{
Paint paint = new Paint();
public DrawView(Context context)
{
super(context);
}
#Override
public void onDraw(Canvas canvas)
{
Paint myPaint = new Paint();
myPaint.setColor(Color.WHITE);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(2);
canvas.drawRect(10, 10, 100, 100, myPaint);
}
}
should i convert my bit map image to convas?how?
Try this, It does what you want and some more:
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);
}
#Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawRect(30, 30, 80, 80, paint);
paint.setStrokeWidth(0);
paint.setColor(Color.CYAN);
canvas.drawRect(33, 60, 77, 77, paint );
paint.setColor(Color.YELLOW);
canvas.drawRect(33, 33, 77, 60, paint );
}
}
To draw it:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
public class StartDraw extends Activity {
DrawView drawView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
}
}
Source: Android canvas draw rectangle

Android - Draw a rect using canvas which should raise from bottom

I want to draw a stack which should raise from the bottom. For example the stack height is 400, from the height 10 it should grow till it reaches 400.
I want to do this using paint and canvas.
I don't want to do it with image view/bitmap and scale animation.
Is it possible to do this with canvas and paint? if so how to achieve it?
Thanks in advance.
You could try using something like this (have not tested myself):
Paint paint = new Paint();
paint.setColor(Color.BLACK);
for(int i = 10; i < 400; i = i + 10)
{
try
{
// To slow the for loop down, can change 100 accordingly or remove altogther
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
canvas.drawRect(0, i, 10, 0, paint); // this will make 10 x 10 square starting from bottom
invalidate();
}
What ever you do with canvas, even if you try for loop or invalidate, sleep, after all its drawing the shape in single stretch. I found the result in following way. May be it would be helpful for someone else, so adding the code here.
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
public class AndroidDraw extends Activity {
private DrawView drawView;
private int height = 300;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_draw);
CountDownTimer timer = new CountDownTimer(2000, 50) {
#Override
public void onTick(long millisUntilFinished) {
height = height - 10;
drawView = new DrawView(AndroidDraw.this, height);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
}
#Override
public void onFinish() {
}
};
timer.start();
}
}
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();
private int height;
public DrawView(Context context, int height) {
super(context);
this.height = height;
}
#Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.RED);
canvas.drawRect(30, height, 60, 300, paint );
}
}

rotate() in canvas has no effect

When I use the rotate method with my canvas object, the canvas object doesn't rotate. Why is this happening? Here is my code
package com.example.hello;
import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.view.View; import android.widget.LinearLayout;
public class CanvasDrawExample extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.example);
LinearLayout rl=(LinearLayout)findViewById(R.id.rl);
rl.addView(new CircleView(this));
}
public class CircleView extends View
{
public CircleView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
Paint p=new Paint(Paint.ANTI_ALIAS_FLAG);
p.setStrokeWidth(100);
p.setStyle(Paint.Style.STROKE);
p.setColor(Color.BLUE);
canvas.drawRect(200, 100, 200, 100, p);
canvas.save();
canvas.rotate((float)145);
canvas.restore();
}
}
}
You save your canvas, rotate it and then restore it without doing any drawing on it. If you are trying to rotate the Rectangle 145ยบ to the right, do the following:
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.save();
canvas.rotate((float)-145,canvas.getWidth()/2,canvas.getHeight()/2);
Paint p=new Paint(Paint.ANTI_ALIAS_FLAG);
p.setStrokeWidth(100);
p.setStyle(Paint.Style.STROKE);
p.setColor(Color.BLUE);
canvas.drawRect(200, 100, 200, 100, p);
canvas.restore();
}
Also, it is not recommended to instantiate and define a Paint inside onDraw(). You should declare and define it in the constructor and then, reuse it.
In order this to work you need to comment the canvas.restore();

Increase speed to redraw the Android Screen

I am new to Android.My application requires to redraw a canvas circle over and over again. However, the speed of redraw is less than what I want. How may I increase the same.
My code is as follows:
--> ImagePracActivity.java
package com.pkg.ImagPrac;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class ImagePracActivity extends Activity {
//DrawView drawView;
movement mv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mv=new movement(getApplicationContext());
setContentView(mv);
mv.requestFocus();
}
}
--> movement.java
package com.pkg.ImagPrac;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
public class movement extends View implements OnTouchListener{
Display display;
float x=0,y=0;
Paint paint=new Paint();
private boolean flag;
public movement(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
display = ((WindowManager)
context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
x = 0;
y = display.getHeight();
}
#Override
protected void onDraw(Canvas canvas) {
if(x<(display.getWidth()/2))
{
canvas.drawCircle(x++, y--, 5, paint);
}
else if(x<(display.getWidth()))
{
canvas.drawCircle(x++, y++, 5, paint);
}
this.invalidate();
}
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
}
A would advice you to use SurfaceView class instead of View, because SurfaceView faster and allows you to have all drawing logic in separate thread. Read more about SurfaceView : http://developer.android.com/guide/topics/graphics/2d-graphics.html
try this
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(5);

Categories

Resources