在surfaceview上添加进度条

时间:2010-10-11 13:38:24

标签: android

我是Android的新手,我得到了一些帮助。

这是我的Maingamepannel类,它是从surfaceview扩展而来的。我想添加一个Progressbar,可以在点击时显示两个自定义按钮的进度。

在这里,我已经添加了三个图像,如果我想添加一个进度条。

package net.obviam.walking;

import net.obviam.walking.model.CowAnimated;
import net.obviam.walking.model.ElaineAnimated;
import net.obviam.walking.model.FallAnimated;
import net.obviam.walking.model.SliderFrameLayout;

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.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.ProgressBar;

/**
 * @author impaler
 * This is the main surface that handles the ontouch events and draws
 * the image to the screen.
 */
public class MainGamePanel extends SurfaceView implements
        SurfaceHolder.Callback {


    private static final String TAG = MainGamePanel.class.getSimpleName();

    //public ProgressBar mImageProgressbar;

//  public MyProgressBar pgbar=new MyProgressBar(getContext());
    private SliderFrameLayout progressbar;
    private MainThread thread;
    private ElaineAnimated elaine;
    private CowAnimated cowobj;
    private FallAnimated fallingobj;
    public Bitmap cow=BitmapFactory.decodeResource(getResources(), R.drawable.cow);
    public Bitmap falling=BitmapFactory.decodeResource(getResources(), R.drawable.falling);


    // the fps to be displayed
    //private String avgFps;
    //public void setAvgFps(String avgFps) {
//      this.avgFps = avgFps;
//  }

    public MainGamePanel(Context context) {
        super(context);
        // adding the callback (this) to the surface holder to intercept events
        getHolder().addCallback(this);          

        //pgbar = new MyProgressBar(getContext());
//      pgbar.setIndeterminate(true);
//      int pad = 50;
//      pgbar.setPadding(pad, pad, pad, pad);
//      pgbar.layout(0, 0, 200, 200);
//      pgbar.setEnabled(true);
//      pgbar.bringToFront();
//      pgbar.setVisibility(VISIBLE);

        this.progressbar=new SliderFrameLayout(getContext(), null);

        // create Elaine and load bitmap
        elaine = new ElaineAnimated(
                    //BitmapFactory.decodeResource(getResources(), R.drawable.jump)
                  BitmapFactory.decodeResource(getResources(), R.drawable.walk) 
                , BitmapFactory.decodeResource(getResources(), R.drawable.jump) 
                , BitmapFactory.decodeResource(getResources(), R.drawable.falling) 
                , 10, 200   // initial position
                , 20, 47    // width and height of sprite
                //,28,45
                //,35,60
                //,this.elaine.getSpriteWidth(),this.elaine.getSpriteHeight()
                //,elaine.getSpriteWidth(),elaine.getSpriteHeight()
                , 10, 6);   // FPS and number of frames in the animation

        cowobj = new CowAnimated(

              BitmapFactory.decodeResource(getResources(), R.drawable.cow)

            , 350, 150  // initial position
            , cow.getWidth(), cow.getHeight()   // width and height of sprite

            , 1, 1);    // FPS and number of frames in the animation

        fallingobj = new FallAnimated(

                  BitmapFactory.decodeResource(getResources(), R.drawable.falling)

                , 340, 160  // initial position
                , falling.getWidth(), falling.getHeight()   // width and height of sprite

                , 1, 1);    // FPS and number of frames in the animation

        // create the game loop thread


        thread = new MainThread(getHolder(), this);

        // make the GamePanel focusable so it can handle events
        setFocusable(true);
    }

     //MainGamePanel mnp=new MainGamePanel(getContext());

     protected  void onDraw(Canvas canvas)//
     {
         progressbar.draw(canvas);
        //mImageProgressbar.draw(canvas);

        // elaine.draw(canvas);
         //cowobj.draw(canvas);

//         elaine.update(System.currentTimeMillis());
//          if(elaine.x>=350)
//          cowobj.update(System.currentTimeMillis());
//   
           //  this.update();


     }//


    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // at this point the surface is created and
        // we can safely start the game loop
        thread.setRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) 
    {
        Log.d(TAG, "Surface is being destroyed");
        // tell the thread to shut down and wait for it to finish
        // this is a clean shutdown
        boolean retry = true;
        while (retry) 
        {
            try 
            {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // try again shutting down the thread
            }
        }
        Log.d(TAG, "Thread was shut down cleanly");
    }


//  public boolean onTouchEvent(MotionEvent event) 
//  {
//      if (event.getAction() == MotionEvent.ACTION_DOWN)
//      {
//          // handle touch
//      }
//      return true;
//  }

    public void render(Canvas canvas)
    {
        Bitmap log=BitmapFactory.decodeResource(getResources(), R.drawable.log);
        Bitmap bkg=BitmapFactory.decodeResource(getResources(), R.drawable.bg);
        Paint paint=new Paint();

        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(bkg,0,0,paint);
        canvas.drawBitmap(log,300,240,paint);


        //pgbar.draw(canvas);

        //pgbar.setProgress( (int)(System.currentTimeMillis()%1000) );
        progressbar.draw(canvas);
        elaine.draw(canvas);
        cowobj.draw(canvas);
        if(elaine.getX()>333)
        fallingobj.draw(canvas);
        //this.onDraw(canvas);
        // display fps
        //displayFps(canvas, avgFps);
    }

    /**
     * This is the game update method. It iterates through all the objects
     * and calls their update method if they have one or calls specific
     * engine's update method.
     */
    int cowMoveCount=0;
    public void update()
    {
        elaine.update(System.currentTimeMillis());
        if(elaine.x>=333)
        {  
            cowMoveCount++;
            if(cowobj.getX()<=370)
            {  
                cowobj.setX(cowobj.getX()+1);
                cowMoveCount=0;
            }
            cowobj.update(System.currentTimeMillis());
            fallingobj.update(System.currentTimeMillis());
        }
    }

    private void displayFps(Canvas canvas, String fps)
    {
        if (canvas != null && fps != null)
        {
            Paint paint = new Paint();
            paint.setARGB(255, 255, 255, 255);
            canvas.drawText(fps, this.getWidth() - 50, 20, paint);
        }
    }   
}

1 个答案:

答案 0 :(得分:4)

您无法在ProgressBar上加SurfaceView。您可以将ProgressBarSurfaceView置于相同的布局中,如果使用RelativeLayout,则可以让ProgressBar浮动在SurfaceView之上