I am new to Android and I'm after some help.
Here is my Maingamepannel class which is extending from surfaceview. I want to add a Progressbar which can show progress on two custom buttons on click.
Here I have allready added three images nnow i want to add a 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);
}
}
}