Friday, 7 September 2012

Android Animation Example 2

                    Android Animation Example 2



SurfaceActivity.java

package a.b;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class SurfaceActivity extends Activity  implements OnTouchListener {
Myview abc;
float x,y;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        abc=new Myview(this);
        abc.setOnTouchListener(this);
        setContentView(abc);
        x=0;
        y=0;
    }
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
abc.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
abc.resume();
}
@Override
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
x=event.getX();
y=event.getY();
return false;
}
public class Myview extends SurfaceView implements Runnable{
SurfaceHolder myholder;
Thread t=null ;
boolean isRunning=false;

public Myview(Context context) {
// TODO Auto-generated constructor stub
super(context);
myholder=getHolder();
}
public void pause()
{
isRunning=false;
while(true)
{
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
t=null;
}
public void resume()
{
isRunning=true;
t=new Thread(this);
t.start();
}

@Override
public void run() {
// TODO Auto-generated method stub
while(isRunning)
{
if(!myholder.getSurface().isValid())
continue;
Canvas canvas=myholder.lockCanvas();
canvas.drawRGB(02, 02, 1);
if(x !=0 && y !=0)
{
Bitmap test=BitmapFactory.decodeResource(getResources(),R.drawable.f);
canvas.drawBitmap(test,x,y,null);
}
myholder.unlockCanvasAndPost(canvas);
}
}

}

}



No comments:

Post a Comment