Reputation: 573
I am trying to create a program wherein when I touch the screen of the device, a ball will come out and will animate. I put the image in a separate class because later on i will be needing multiple instances of this MyImage class so whenever i touch the screen another ball will come out.
I have MyImage class wherein it contains an image. this class is extended to View class. The override method onDraw is where the image is being animated (bouncing on the screen).
public class MyImage extends View {
Bitmap g;
float posY = 0;
float posX = 0;
boolean ud = true, lr = true;
public MyImage(Context context) {
super(context);
g = BitmapFactory.decodeResource(getResources(), R.drawable.ba);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(g, posX, posY, null);
...... //code for animating
invalidate();
}
}
I want to create an object of this class so that this will animate to my other canvas from class MainActivity.
public class MainActivity extends Activity implements OnTouchListener{
..... //declarations
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mySurfaceView = new MySurfaceView(this);
mySurfaceView.setOnTouchListener(this);
setContentView(mySurfaceView);
}
@Override
protected void onPause() {
....
}
@Override
protected void onResume() {
....
}
Thread myThread;
Canvas canvas;
public class MySurfaceView extends SurfaceView implements Runnable{
SurfaceHolder myHolder;
boolean isRunnable = false;
public MySurfaceView(Context context) {
super(context);
myHolder = getHolder();
}
public void pause(){
....
}
public void resume(){
isRunnable = true;
myThread = new Thread(this);
myThread.start();
}
@Override
public void run() {
while(isRunnable){
if(!myHolder.getSurface().isValid()){
continue;
}
canvas = myHolder.lockCanvas();
canvas.drawRGB(100, 0, 0);
myHolder.unlockCanvasAndPost(canvas);
}
}
}
@Override
public boolean onTouch(View v, MotionEvent me) {
.....
switch(me.getAction()){
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
mi = new MyImage(this);
canvas.drawBitmap(..) ;//Is it possible to convert object to Bitmap?
break;
}
return true;
}
}
The problem is that I dont know how wil I make the object of MyImage to Bitmap so that I could draw it into the canvas. And also I am not really sure if what i did is right because in my MyImage class i already have a canvas which i think will cover the canvas from the main activity. Could anyone help with this?
Upvotes: 0
Views: 1776
Reputation: 10977
Yeah, I would probably change that approach. Creating a View for each ball makes not much sense. I would create one custom View. This View can then contain a List of balls. In the onDraw()
method of that View you can draw all the balls. The balls can be just Point
or PointF
objects with a default radius in the beginning.
Something along the line of
private class MyView extends View {
List<Point> balls;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for(Point p : balls){
canvas.drawCircle(p.x, p.y, 10, myPaint);
}
}
}
Upvotes: 2