Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int currentAnimationFrame = 0;
- int visImage[] = { R.drawable.img_1, R.drawable.img_2, R.drawable.img_3,
- R.drawable.img_4, R.drawable.img_5, R.drawable.img_6,
- R.drawable.img_7 };
- class AnimationThread extends Thread {
- private boolean mRun;
- private SurfaceHolder mSurfaceHolder;
- public AnimationThread(SurfaceHolder surfaceHolder) {
- mSurfaceHolder = surfaceHolder;
- }
- @Override
- public void run() {
- while (mRun) {
- Canvas c = null;
- try {
- c = mSurfaceHolder.lockCanvas(null);
- synchronized (mSurfaceHolder) {
- doDraw(c);
- }
- } finally {
- if (c != null) {
- mSurfaceHolder.unlockCanvasAndPost(c);
- }
- }
- }
- }
- private void doDraw(Canvas canvas) {
- currentAnimationFrame++;
- if (currentAnimationFrame >= visImage.length) {
- currentAnimationFrame = 0;
- }
- Bitmap background = BitmapFactory.decodeResource(getResources(),
- visImage[currentAnimationFrame]);
- canvas.drawBitmap(background, 0, 0, null);
- canvas.restore();
- }
- public void setRunning(boolean b) {
- mRun = b;
- }
- }
- private AnimationThread thread;
- public AnimatedSurface(Context context, AttributeSet attrs) {
- super(context, attrs);
- SurfaceHolder holder = getHolder();
- holder.addCallback(this);
- thread = new AnimationThread(holder);
- }
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- }
- public void surfaceCreated(SurfaceHolder holder) {
- thread.setRunning(true);
- thread.start();
- }
- public void surfaceDestroyed(SurfaceHolder holder) {
- boolean retry = true;
- thread.setRunning(false);
- while (retry) {
- try {
- thread.join();
- retry = false;
- } catch (InterruptedException e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement