2010-10-11 106 views
1

我是Android的新手,我经过一些帮助。在surfaceview上添加进度条

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

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

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); 
     } 
    } 
} 
+0

Hi Pramod - 你应该尽量缩短你的代码片段,缩短一点(很多),而是用文字更清晰地描述你的问题 - 因为大多数人不愿意通读大量的代码来回答题。 – Justin 2010-10-11 14:03:10

回答

4

您不能在SurfaceView上放置ProgressBar。您可以使ProgressBarSurfaceView的布局相同,如果使用RelativeLayout,则可以使ProgressBar浮动在SurfaceView之上。

+0

感谢您的帮助,我想要我的进度条应该浮在表面视图上的同样的东西,但仍然困惑如何实现它?如何使用表面视图的相对布局? – Pramod 2010-10-12 05:20:31

+0

@Pramod:将'SurfaceView'放在'RelativeLayout'中。把'ProgressBar'放在相同的'RelativeLayout'中。如果您通过布局XML文件进行此操作,请确保'ProgressBar'在文件中显示*后面*而不是'SurfaceView'。此时,“ProgressBar”将显示为浮动在“SurfaceView”上,无论您将它放置在何处。 – CommonsWare 2010-10-12 10:45:10

+0

感谢这为我工作。最初我得到空白屏幕,因为我没有写myclass的默认构造函数,现在正在扩展表面视图,它工作正常 – Pramod 2010-10-27 03:39:15