Animation in android(make a man run) - android

I am relatively new to animation in android and just getting the feel of a couple of things.I want to know how i can make a "man"(image) run on android.The man is an icon.IF i use animation through the following code the icon just moves across the screen.How do i get the running motion.
public anima1(Context context)
{
super(context);
cloud=BitmapFactory.decodeResource(getResources(),R.drawable.androidicon);
// TODO Auto-generated constructor stub
}
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Rect re=new Rect();
re.set(0,0,canvas.getWidth(),canvas.getHeight());
Paint c=new Paint();
c.setColor(Color.WHITE);
c.setStyle(Paint.Style.FILL);
canvas.drawRect(re,c);
x=x+10;
if(x==canvas.getWidth())
{
y=y+10;
x=0;
}
if(y==canvas.getHeight())
{
x=0;
y=0;
}
canvas.drawBitmap(cloud, x, y,p);
invalidate();
}

To make the man appear to run, you should have several different images of the man with his feet in different positions and swap out the image on every loop. Alternatively, you could use an animated GIF, but I am not sure whether Android will actually play the animation.

Related

Can you draw multiple Bitmaps on a single View Method?

currently I am trying to make an animation where some fish move around. I have successfully add one fish and made it animate using canvas and Bitmap. But currently I am trying to add a background that I made in Photoshop and whenever I add it in as a bitmap and draw it to the canvas no background shows up and the fish starts to lag across the screen. I was wondering if I needed to make a new View class and draw on a different canvas or if I could use the same one? Thank you for the help!
Here is the code in case you guys are interested:
public class Fish extends View {
Bitmap bitmap;
float x, y;
public Fish(Context context) {
super(context);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fish1);
x = 0;
y = 0;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bitmap, x, y, null);
if (x < canvas.getWidth())
{
x += 7;
}else{
x = 0;
}
invalidate();
}
}
You can draw as many bitmaps as you like. Each will overlay the prior. Thus, draw your background first, then draw your other images. Be sure that in your main images, you use transparent pixels where you want the background to show through.
In your code, don't call Invalidate() - that's what causes Android to call onDraw() and should only be called from somewhere else when some data has changed and needs to be redrawn.
You can do something like this, where theView is the view containing your animation:
In your activity, put this code in onCreate()
myAnimation();
Then
private void myAnimation()
{
int millis = 50; // milliseconds between displaying frames
theView.postDelayed (new Runnable ()
{
#Override public void run()
{
theView.invalidate();
myAnimation(); // you can add a conditional here to stop the animation
}
}, millis);
}

How to draw multiple bitmaps on canvas?

I want to develop a game that shoots bullets on every touch of the canvas.
It works but when I touch the canvas after shooting, he takes the bullet and restarts the shooting.
I just want the bitmap to create new bullet at every touch. Here is my code:
public class MainActivity extends Activity implements OnTouchListener {
DrawBall d;
int x ;
int y;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
d = new DrawBall(this);
d.setOnTouchListener(this);
setContentView(d);
}
public class DrawBall extends View {
Bitmap alien;
public DrawBall(Context context) {
super(context);
alien = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
startDraw(canvas);
}
public void startDraw(Canvas canvas){
Rect ourRect = new Rect();
ourRect.set(0,0, canvas.getWidth(), canvas.getHeight());
Paint blue = new Paint();
blue.setColor(Color.BLACK);
blue.setStyle(Paint.Style.FILL);
canvas.drawRect(ourRect, blue);
if(y < canvas.getHeight()){
y-=5;
}
canvas.drawBitmap(alien, x, y,new Paint());
invalidate();
}
}
public boolean onTouch(View v, MotionEvent event) {
x = (int) event.getX();
y = (int) event.getY();
return false;
}
}
I only superficially read the code. It seems that you are only keeping track of one bullet using the x y coordinates.The coordinates are resetting at every touch event, and thus you lose the previous bullet.
Use a dynamic array, or a linked list to keep track of all the bullets on the screen.
When there's a new touch, add the x,y to the array.
When drawing the bullet, iterate through your array to draw and update every bullet.
If the y-coordinate of any bullet goes out of the screen, delete the bullet from the array.
Each draw starts with a blank canvas. So to draw multiple bitmaps you need to keep track of where to draw each bullet and call drawBitmap multiple times.
Also, calling invalidate in onDraw is a bad idea- it will immediately invalidate, leading you to have performance issues. I'd suggest invalidating on a timer instead. Drawing too frequently will lead to performance issues.

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

GUI, widgets, UI: how would I go about creating a needle gauge?

OK, Brief recap, Was asked to create an app for work that records data specific data and display it to the screen when finished. So it would function like so.
press start > press stop > display results.
However, I have just been told by the IT director of my company that he wants to display information in needle graphs (g-force, average speed, top speed) and also wants a flashy way of displaying the others (time taken, distance traveled)
My initial idea is this:
create a needle gauge like this, but on a smaller scale and have the digit value display below or beside the graph and to just display the distance traveled and time taken displayed as alarm clock style digits. This would all run down the left hand side of the screen in a thin column and then hava a map displaying the starting location and end location with the route taken for the journey
basically I would like it to look like this (sorry for the crudeness of the drawing)
Something along these lines would be perfect!
I think I could work out the map business and the digits for the time and distance readouts but I have never done any really fancy UI stuff.
How would I get started making the needle gauge?
I was thinking of trying a horizontal bar gauge forst maybe? Incase I cant get the needle gauge to work.
Also, I only have a til tuesday! :S
invision the following very basic idea:
We have our Custom View with a background image which is the gauge without the needle!
So we first implement this using a class that extends View
public class ourGauge extends View {
private Bitmap bgImage = null;
public ourGauge(Context context, Bitmap bgImage) {
super(context);
this.bgImage = bgImage;
}
public ourGauge(Context context) {
super(context);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bgImage, 0, 0, null);
}
}
Now lets add a needle
public class ourGauge extends View {
private Bitmap bgImage = null;
private int indicator;
Paint paint = new Paint();
public ourGauge(Context context, Bitmap bgImage) {
super(context);
this.bgImage = bgImage;
}
public ourGauge(Context context) {
super(context);
}
public void setIndicator(int indicator){
this.indicator = indicator;
invalidate();
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bgImage, 0, 0, null);
//you could set color based on indicator (speed or sth)
paint.setColor(Color.BLACK);
canvas.drawLine(0, 0, 20, 20, paint);
//you have to find the formula to get from where to where the line should drawn
}
}
To make it better
Don't draw the needle using drawLine but rather make it a shape
with dimensions
To create dynamic labels for speeds, you should draw them too
etc

How to provide alpha animation to each symbol instead of the whole view in android

I am creating a Typing Tutor application that has the options to type numbers, alphabets and characters. On selecting a particular option, the array gets loaded with either symbols. The following is my problem:
The activity that loads the array displays the symbols and display those on a random location of the activity. For this I have provided alpha animation for each symbol that appears for a period of time and vanishes off, the other symbol appearing after it. But I am not getting the desired result. The alpha animation is applied to the whole canvas view rather than each symbol. I want alpha animation for each symbol rather than the whole canvas view. Here is the code:
/*
* Create a view that displays the animation.
* This view will display the alpha animation
* The array is loaded and the elements are
* displayed in the random manner
*/
class AnimationView extends View
{
Animation animation;
String str;
Random randomLocationXY, randomValues;
public AnimationView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
private void createAnimation(Canvas canvas)
{
animation = new AlphaAnimation(0.0f, 1.0f);
animation.setRepeatCount(50);
animation.setRepeatMode(Animation.REVERSE);
animation.setInterpolator(new AccelerateDecelerateInterpolator());
animation.setDuration(10000l);
startAnimation(animation);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
randomLocationXY = new Random();
//randomValues = new Random();
if(animation == null)
{
createAnimation(canvas);
}
Paint p = new Paint();
p.setColor(Color.RED);
p.setTextSize(40);
p.setTypeface(Typeface.SANS_SERIF);
for(int i=0; i< sel_array.length;i++)
{
int x = randomLocationXY.nextInt(canvas.getWidth());
int y = randomLocationXY.nextInt(canvas.getHeight());
str = sel_array[i];
//draws the text at random location
canvas.drawText(str, x, y, p);
}
}
}
hmm.. As far as I know in that case you have to create your animation (probably moving the postion of Text string as per your needs so that it looks like an animation. An another but processor intensive solution would be to provide the animation using different bitmaps (using canvas.drawBitMap).

Categories

Resources