In the below example I have created Animate Bitmap program. By using animated bitmap function we will get the position and distance along a path. In below, code will animate image on screen. In my below code I have clearly describe how to make Animate a Bitmap app.
Step(1)MainActivity-
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.menu_main, menu);
return true;
}
}
Step(2)-Create a animate class-
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.img1);
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
}
}
0 Comment(s)