Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could do something like this, using your own image(the image of the man):</p> <p>Main Class:</p> <pre><code>package com.android.animation; import android.app.Activity; import android.os.Bundle; public class Main extends Activity { Animation myView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myView = new Animation(this); setContentView(myView); } } </code></pre> <p>Animation Class:</p> <pre><code>package com.android.animation; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.view.View; public class Animation extends View { Bitmap gBall; float changingY; public Animation(Context content) { super(content); gBall = BitmapFactory.decodeResource(getResources(), R.drawable.ball); changingY = 0; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.BLACK); canvas.drawBitmap(gBall, (canvas.getWidth()/2), changingY, null); if(changingY &lt; canvas.getHeight()) changingY += 10; else changingY = 0; invalidate(); } } </code></pre> <p>XML file: </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>In fact, if you want you can go ahead and copy paste my code and see how it works(make sure to put an image in the drawable-hdpi folder)... you should be able to use it as a template for your project. Hope it helps!</p> <p>P.S You could of course, change the <code>ChangingY</code> variable to <code>ChangingX</code> (for example; of course you would have to change a couple other things like the <code>drawBitmap()</code> method.. not hard though) to make the ball move in a horizontal line... see how it works out for you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload