Android Program to Animate a Bitmap

«
»
This Android Program lets you create an Application to Animate a Bitmap.

Here is source code of the Program to create an Application to Animate a Bitmap using Java. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

Main Activity

 
package com.example.animatingbitmap;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
 
public class MainActivity extends Activity {
 
    animate var;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        var = new animate(this);
        setContentView(var);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
}

Animate

 
package com.example.animatingbitmap;
 
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.graphics.Rect;
import android.view.View;
 
public class animate extends View{
 
    Bitmap bm;
    int x, y;
    public animate(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        bm=BitmapFactory.decodeResource(getResources(), R.drawable.image4);
        x = 0; y = 0;
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        Rect myrect = new Rect(0, 0, canvas.getWidth(),canvas.getHeight()/2);
        Paint pa = new Paint();
        pa.setColor(Color.BLUE);
        pa.setStyle(Paint.Style.FILL);
        canvas.drawRect(myrect, pa);
 
        if (x < canvas.getWidth()) {
            x += 10;
        }
        else {
            x = 0;
        }
        if (y < canvas.getHeight()) {
            y += 10;
        }
        else {
            y = 0;
        }
        canvas.drawBitmap(bm, x, y, new Paint());
        invalidate();//calls this method again and again
    }
 
}

Capture

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.

If you wish to look at all Tutorials, go to Java Android Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.