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 } }
advertisement
advertisement
Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.
If you wish to look at all Tutorials, go to Java Android Tutorials.
Related Posts:
- Check Programming Books
- Practice BCA MCQs
- Check Java Books
- Practice Programming MCQs
- Apply for Computer Science Internship