So this bit of code seems to be the problem:
public void surfaceCreated(final SurfaceHolder sh) {
loop = new Loop();
loop.setMethod(new LoopMethod() {
public void callMethod() {
Canvas canvas = sh.lockCanvas();
synchronized(sh) {
draw(canvas);
}
sh.unlockCanvasAndPost(canvas);
}
});
loop.setRunning(true);
loop.start();
}
This works fine up until the user backs out of the application, or opens up the Recent Items tray. Upon which I get the following error:
java.lang.IllegalStateException: Surface has already been released.
which points to this line:
sh.unlockCanvasAndPost(canvas);
Why is this occurring and how do I fix it?
EDIT: ENTIRE PROBLEMATIC CLASS
public class SplashScreen extends SurfaceView implements SurfaceHolder.Callback {
private SplashActivity splashActivity;
private Loop loop;
//Loading gif info
private Movie loadingGif;
private long gifStart;
private int gifW, gifH;
private boolean gifDone;
public SplashScreen(Context context, SplashActivity splashActivity) {
super(context);
getHolder().addCallback(this);
this.splashActivity = splashActivity;
//Load gif in
InputStream gifStream = context.getResources().openRawResource(+ R.drawable.load);
loadingGif = Movie.decodeStream(gifStream);
gifW = loadingGif.width();
gifH = loadingGif.height();
gifDone = false;
}
private void beginLoading() {
}
public void surfaceCreated(final SurfaceHolder sh) {
loop = new Loop();
loop.setMethod(new LoopMethod() {
public void callMethod() {
Canvas canvas = sh.lockCanvas();
synchronized(sh) {
draw(canvas);
}
sh.unlockCanvasAndPost(canvas);
}
});
loop.setRunning(true);
loop.start();
}
public void draw(Canvas canvas) {
if(canvas != null) {
super.draw(canvas);
canvas.save();
canvas.drawColor(Color.rgb(69, 69, 69));
Paint p = new Paint();
p.setAntiAlias(true);
long now = android.os.SystemClock.uptimeMillis();
if(gifStart == 0)
gifStart = now;
if(loadingGif != null) {
int dur = loadingGif.duration();
if(!gifDone) {
int relTime = (int) ((now - gifStart) % dur);
loadingGif.setTime(relTime);
}
else
loadingGif.setTime(dur);
if(now - gifStart > dur && !gifDone) {
gifDone = true;
loadingGif.setTime(dur);
beginLoading();
}
float scaleX = getWidth()/gifW*1f;
float scaleY = getHeight()/gifH*1f;
float centerX = getWidth()/2/scaleX - gifW/2;
float centerY = getHeight()/2/scaleY - gifH/2;
canvas.scale(scaleX, scaleY);
loadingGif.draw(canvas, centerX, centerY, p);
canvas.restore();
p.setColor(Color.WHITE);
p.setTypeface(Typeface.create("Segoe UI", Typeface.BOLD));
p.setTextSize(UsefulMethods.spToPx(12));
String s = "Version " + BuildConfig.VERSION_NAME;
canvas.drawText(s, 10, getHeight() - 10, p);
}
}
else
System.out.println("not drawing :(");
}
public void surfaceDestroyed(SurfaceHolder sh) {
loop.setRunning(false);
}
public void surfaceChanged(SurfaceHolder sh, int format, int width, int height) {
System.out.println("call");
}
}
Check if visibility has changed, if it has pause/resume loop:
public void onVisibilityChanged(View view, int visibility) {
super.onVisibilityChanged(view, visibility);
if(loop != null)
if(visibility == VISIBLE)
if(!loop.isRunning())
loop.setRunning(true);
else;
else if(visibility == INVISIBLE)
if(loop.isRunning())
loop.setRunning(false);
}
In my case I covered sh.unlockCanvasAndPost with
if(sh.getSurface().isValid()){
sh.unlockCanvasAndPost(canvas);
}
If we will check isValid() source will see:
Returns true if this object holds a valid surface.
#return True if it holds a physical surface, so lockCanvas() will succeed.Otherwise returns false.
Related
I am running into an issue in my app. When my game ends (when life == 0) I am attempting to switch to a game over screen by using a different activity. When the game ends, the app simply crashes. I have included the XML for the activity I am trying to switch from as well as indicating where the app crashes. If anyone could help out, that would be great! Thanks.
activity_game.XML:
SurfaceView I am trying to switch from once game ends:
public class SVGameView extends SurfaceView implements Runnable {
private SurfaceHolder holder;
Thread thread = null;
volatile boolean running = false;
static final long FPS = 30;
private Sprite sprite;
private long lastClick;
private Bitmap ball, gameOver;
//private int x = 200, y = 200;
private int scorePosX = 100;
private int scorePosY = 100;
private int countScore = 0;
private int life = 1;
public SVGameView(Context context) {
super(context);
thread = new Thread(this);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
running = false;
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
running = true;
thread.start();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball2);
gameOver = BitmapFactory.decodeResource(getResources(),R.drawable.endscreen);
sprite = new Sprite(this, ball);
}
#Override
public void run() {
long ticksPS = 1000 / FPS;
long startTime;
long sleepTime;
while (running) {
Canvas c = null;
startTime = System.currentTimeMillis();
try {
c = getHolder().lockCanvas();
synchronized (getHolder()) {
update();
draw(c);
}
} finally {
if (c != null) {
getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = ticksPS-(System.currentTimeMillis() - startTime);
try {
if (sleepTime > 0)
thread.sleep(sleepTime);
else
thread.sleep(10);
} catch (Exception e) {}
}
}
private void update(){
sprite.update();
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
canvas.drawPaint(paint);
paint.setColor(Color.WHITE);
paint.setTextSize(48);
canvas.drawText("Score: " + countScore, scorePosX, scorePosY, paint);
canvas.drawText("Lives: " + life, 500, 100, paint);
sprite.onDraw(canvas);
//Crashes here
if(life == 0) {
getContext().startActivity(new Intent(getContext(), SVGameOver.class));
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if(System.currentTimeMillis()-lastClick > 300){
lastClick = System.currentTimeMillis();
}
synchronized (getHolder()){
if(sprite.isHit(event.getX(), event.getY())){
countScore += 1;
sprite.increase();
}else{
life --;
}
}
return super.onTouchEvent(event);
}
}
Activity I am trying to reach once the game ends:
public class SVGameOver extends Activity {
private Bitmap gameOverScreen;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
gameOverScreen = BitmapFactory.decodeResource(getResources(), R.drawable.endscreen);
}
protected void onDraw(Canvas canvas){
canvas.drawBitmap(gameOverScreen, 0,0,null);
}
}
I think logcat is asking you the right question: "have you declared this activity in your AndroidManifest.xml" ?
If you think you did it, It's highly probable you did it in a wrong way, most of the times that you think you added an Activity to the manifest but you are receiving this kind of crash, 99,9% of the time you declared it with a wrong namespace
Declare SVGameOver activity in your AndroidManifest.xml:
<activity
android:name="com.example.welcome.assignment2.SVGameOver">
...
</activity>
I am new to android programming. I am trying to write some really simple apps around basic graphics and input. The app I am working with now uses SurfaceView and draws onto that with a Canvas. It just draws a circle in the middle of the screen, and changes its color every second. Here's my SurfaceView:
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
public class DrawerThread extends Thread {
private SurfaceHolder mSurfaceHolder;
private long mTimeStep;
private long mTime;
private long mPhysicsTime;
private boolean mRun;
private int mCanvasWidth;
private int mCanvasHeight;
private int colorIter;
private int colors[][] = {
{255, 255, 0, 0},
{255, 0, 255, 0}
};
Paint mPaint;
public DrawerThread(SurfaceHolder surfaceHolder) {
mSurfaceHolder = surfaceHolder;
mTimeStep = 1000;
mRun = false;
mCanvasWidth = 1;
mCanvasHeight = 1;
colorIter = 0;
mPaint = new Paint();
mPaint.setAntiAlias(true);
}
public void setRunning(boolean value) {
if(value == true && mRun != true) {
mTime = System.currentTimeMillis();
mPhysicsTime = 0;
}
mRun = value;
}
public void setSurfaceSize(int width, int height) {
synchronized (mSurfaceHolder) {
mCanvasWidth = width;
mCanvasHeight = height;
}
}
private void doPhysics(long time) {
long deltaTime = time - mTime;
mPhysicsTime += deltaTime;
mTime = time;
int steps = (int) (mPhysicsTime / mTimeStep);
mPhysicsTime -= steps * mTimeStep;
colorIter = (colorIter + steps) % colors.length;
}
private void doDraw(Canvas canvas) {
canvas.drawColor(Color.argb(255, 0, 0, 0));
mPaint.setARGB(colors[colorIter][0], colors[colorIter][1], colors[colorIter][2], colors[colorIter][3]);
canvas.drawCircle(mCanvasWidth/2, mCanvasHeight/2, 100, mPaint);
}
#Override
public void run() {
while(mRun) {
Canvas canvas = null;
try {
synchronized (mSurfaceHolder) {
canvas = mSurfaceHolder.lockCanvas();
doPhysics(System.currentTimeMillis());
doDraw(canvas);
}
} finally {
if(canvas != null) {
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
private DrawerThread mDrawerThread;
public MySurfaceView(Context context) {
super(context);
SurfaceHolder surfaceHolder = getHolder();
surfaceHolder.addCallback(this);
mDrawerThread = new DrawerThread(surfaceHolder);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
mDrawerThread.setRunning(true);
mDrawerThread.start();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
mDrawerThread.setSurfaceSize(width, height);
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
mDrawerThread.setRunning(false);
while(retry) {
try{
mDrawerThread.join();
retry = false;
} catch(InterruptedException e) {}
}
}
}
I wrote this code based on the Lunar Landing example provided here:
https://gitorious.org/replicant/development/source/3eda8fc3859c243df4a1f892a11e2da84b49cb94:samples/LunarLander/src/com/example/android/lunarlander/LunarView.java#L8
I tried to make it simpler than that so I omitted what I could. So the app is working until I change from profile view to landscape OR use the Recents button. Both of these actions crash my app on the canvas.drawColor(...) line in function doDraw(), nullpointer exception. How can I this problem?
Second question: I tried to limit my drawing thread by putting a Thread.sleep(<>) in my run() in the drawing thread. However, I noticed that while this thread is sleeping, the screen doesn't flip when I turn my phone sideways(go from profile to landscape). Why does the sleeping drawing thread block surface change?
Third question: why do I need the while loop in surfaceDestroyed()? Isn't mDrawerThread.join() a blocking function?
I know basic Java but I have almost 0 knowledge of android programming, so I would appreciate any constructive remarks.
(IDE: Android Studio)
I'm new in Android and I want to do some animations. I'm trying to make my sprite sheet move automatically. But there is a problem with screen rendering. It leaves a trail while it is moving.Click here to see the screen shot
This is my code:
public class SampleAnimationActivity extends Activity {
/** Called when the activity is first created. */
Screen screen;
MapAnimation mapAnimation;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
screen = new Screen(this);
setContentView(screen);
}
#Override
protected void onDestroy() {
super.onDestroy();
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
}
public class Screen extends SurfaceView implements Callback{
private SurfaceHolder holder;
private MySurfaceViewThread mySurfaceViewThread;
private boolean isSurfaceCreated;
private Bitmap character, tiles;
public Screen(Context context) {
super(context);
initialize();
}
public void initialize(){
//Create a new SurfaceHolder and assign this class as its callback...
holder = getHolder();
holder.addCallback(this);
isSurfaceCreated = false;
character = BitmapFactory.decodeResource(getResources(),R.drawable.penguin_sprite);
tiles = BitmapFactory.decodeResource(getResources(), R.drawable.tile_sprites);
resume();
}
public void resume(){
//Create and start the graphics update thread.
if(mySurfaceViewThread == null){
mySurfaceViewThread = new MySurfaceViewThread();
if(isSurfaceCreated == true){
mySurfaceViewThread.start();
}
}
}
public void pause(){
//Kill the graphics update thread
if(mySurfaceViewThread != null){
mySurfaceViewThread.pause();
mySurfaceViewThread = null;
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
isSurfaceCreated = true;
if(mySurfaceViewThread != null){
mySurfaceViewThread.start();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
isSurfaceCreated = false;
pause();
}
public class MySurfaceViewThread extends Thread{
private boolean isPaused;
private boolean characterLoaded, characterDrawn;
private SurfaceHolder surfaceHolder;
public MySurfaceViewThread(){
super();
isPaused = false;
characterLoaded = false;
surfaceHolder = holder;
characterDrawn = false;
}
public void run(){
//Repeat the drawing loop until the thread is stopped
while(!isPaused){
if(!surfaceHolder.getSurface().isValid()){
continue;
}
if(characterLoaded == false){
mapAnimation = new MapAnimation(screen, character);
characterLoaded = true;
}
Canvas canvas = surfaceHolder.lockCanvas();
mapAnimation.onDraw(canvas);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
public void pause(){
}
public void onDraw(){
}
}
}
}
public class MapAnimation {
private Screen screen;
private Bitmap character;
private int width, height, xPosition, yPosition, xSpeed, ySpeed;
public MapAnimation(Screen screen, Bitmap character) {
this.screen = screen;
this.character = character;
this.width = character.getWidth();
this.height = character.getHeight();
xPosition = 0;
yPosition = 0;
xSpeed = 5;
ySpeed = 5;
}
public void updateCharacter(){
if(xPosition > screen.getWidth() - width - xSpeed){
xSpeed = 0;
ySpeed = 5;
}
if(yPosition > screen.getHeight() - height - ySpeed){
xSpeed = -5;
ySpeed = 0;
}
if(xPosition + xSpeed < 0){
xPosition=0;
xSpeed = 0;
ySpeed = -5;
}
if(yPosition+ySpeed < 0){
yPosition = 0;
xSpeed = 5;
ySpeed = 0;
}
xPosition += xSpeed;
yPosition += ySpeed;
}
public void onDraw(Canvas canvas){
updateCharacter();
Rect src = new Rect(0, 0,135,225);
Rect dst = new Rect(xPosition, yPosition, xPosition+width, yPosition+height);
canvas.drawBitmap(character, src, dst, null);
}
}
Your help will be deeply appreciated :)
I already solved my problem, I just need to add "drawColor(color.BLACk);" before calling mapAnimation.onDraw() method.
I'm new to Android platform. I wish to develop a live wallpaper application. When i was searched regarding this in search Engine's, many of them created a live wallpaper as their code (using SurfaceView and Canvas), I'm not much aware in this. Here my doubt is, any possible to set a .gif images as a live wallpaper.
This is the basic wallpaper service (as supplied in the Live Wallpaper Tutorial) hacked to display an animated gif.
First - create a project & set up your manifest as a Live wallpaper.
Then - download a gif, like this one
Save that gif in res/raw/nyan.gif in your project.
Create a live wallpaper service, like shown in this example.
public class NyanNyanService extends WallpaperService {
static final String TAG = "NYAN";
static final Handler mNyanHandler = new Handler();
/**
* #see android.service.wallpaper.WallpaperService#onCreate()
*/
#Override
public void onCreate() {
super.onCreate();
}
/**
* #see android.service.wallpaper.WallpaperService#onCreateEngine()
*/
#Override
public Engine onCreateEngine() {
try {
return new NyanEngine();
} catch (IOException e) {
Log.w(TAG, "Error creating NyanEngine", e);
stopSelf();
return null;
}
}
class NyanEngine extends Engine {
private final Movie mNyan;
private final int mNyanDuration;
private final Runnable mNyanNyan;
float mScaleX;
float mScaleY;
int mWhen;
long mStart;
NyanEngine() throws IOException {
InputStream is = getResources().openRawResource(R.raw.nyan);
if (is != null) {
try {
mNyan = Movie.decodeStream(is);
mNyanDuration = mNyan.duration();
} finally {
is.close();
}
} else {
throw new IOException("Unable to open R.raw.nyan");
}
mWhen = -1;
mNyanNyan = new Runnable() {
public void run() {
nyan();
}
};
}
#Override
public void onDestroy() {
super.onDestroy();
mNyanHandler.removeCallbacks(mNyanNyan);
}
#Override
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);
if (visible) {
nyan();
} else {
mNyanHandler.removeCallbacks(mNyanNyan);
}
}
#Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
mScaleX = width / (1f * mNyan.width());
mScaleY = height / (1f * mNyan.height());
nyan();
}
#Override
public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep,
float yOffsetStep, int xPixelOffset, int yPixelOffset) {
super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep, xPixelOffset, yPixelOffset);
nyan();
}
void nyan() {
tick();
SurfaceHolder surfaceHolder = getSurfaceHolder();
Canvas canvas = null;
try {
canvas = surfaceHolder.lockCanvas();
if (canvas != null) {
nyanNyan(canvas);
}
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
mNyanHandler.removeCallbacks(mNyanNyan);
if (isVisible()) {
mNyanHandler.postDelayed(mNyanNyan, 1000L/25L);
}
}
void tick() {
if (mWhen == -1L) {
mWhen = 0;
mStart = SystemClock.uptimeMillis();
} else {
long mDiff = SystemClock.uptimeMillis() - mStart;
mWhen = (int) (mDiff % mNyanDuration);
}
}
void nyanNyan(Canvas canvas) {
canvas.save();
canvas.scale(mScaleX, mScaleY);
mNyan.setTime(mWhen);
mNyan.draw(canvas, 0, 0);
canvas.restore();
}
}
}
This will basically scale the nyan-nyan cat to fit the screen and animate it perpetually.
A Live wallpaper manifest looks sort-of-like this (this example does not contain a configuration activity):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.nyan.nyan.package"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/application_nyan" >
<service
android:label="#string/wallpaper_nyan"
android:name=".NyanNyanService"
android:permission="android.permission.BIND_WALLPAPER">
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
</intent-filter>
<meta-data android:name="android.service.wallpaper" android:resource="#xml/nyan" />
</service>
</application>
</manifest>
The AndroidManifest.xml has a reference to a file in res/xml, in this case named "nyan.xml":
<?xml version="1.0" encoding="utf-8"?>
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android" />
See this article: How to set gif as android live wallpaper - techprpr - Medium
Jens answer use "Movie" class, but Movie class is deprecated in android api 28
so when api >= 28, I use AnimatedImageDrawable instead
Setup live wallpaper like Jens answer, and I change wallpaper service code:
wallpaper service: AnimWallpaper
public class AnimWallpaper extends WallpaperService {
#Override
public Engine onCreateEngine() {
return new CustomEngine();
}
class CustomEngine extends Engine {
UseAnim useAnim;
public CustomEngine() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
useAnim = new UseAnim(getApplicationContext(), getSurfaceHolder(), R.raw.gif2);
}
}
#Override
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);
if (visible) {
useAnim.restart();
} else {
useAnim.stop();
}
}
#Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
useAnim.updateScaleAndPadding2(width, height);
useAnim.restart();
}
#Override
public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) {
super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep, xPixelOffset, yPixelOffset);
useAnim.restart();
}
#Override
public void onDestroy() {
super.onDestroy();
useAnim.stop();
}
}
}
UseAnim
class UseAnim {
SurfaceHolder holder;
Runnable startRunnable;
AnimatedImageDrawable gif;
float fps = 60;
Handler handler = new Handler();
#RequiresApi(api = Build.VERSION_CODES.P)
public UseAnim(Context ctx, SurfaceHolder holder, int gifResId) {
this.holder = holder;
final ImageDecoder.Source src = ImageDecoder.createSource(ctx.getResources(), gifResId);
startRunnable = new Runnable() {
#Override
public void run() {
start();
}
};
new Handler().post(new Runnable() {
#Override
public void run() {
try {
UseAnim.this.gif = (AnimatedImageDrawable) ImageDecoder.decodeDrawable(src);
} catch (IOException e) {
throw new RuntimeException(e);
}
UseAnim.this.gif.start();
}
});
}
public void restart() {
stop();
start();
}
float scale = -1;
public void start() {
// since get gif with AnimatedImageDrawable must be in handler.post, so gif maybe null
if (gif != null) {
Canvas canvas = null;
try {
if (scale == -1) {
updateScaleAndPadding();
}
canvas = holder.lockCanvas();
if (canvas != null) {
canvas.translate(horiPadding, vertiPadding);
canvas.scale(scale, scale);
gif.draw(canvas);
}
} finally {
if (canvas != null) {
holder.unlockCanvasAndPost(canvas);
}
}
}
handler.removeCallbacks(startRunnable);
handler.postDelayed(startRunnable, (long) (1000L / fps));
}
public void stop() {
handler.removeCallbacks(startRunnable);
}
int horiPadding;
int vertiPadding;
private void updateScaleAndPadding() {
Canvas canvas = null;
try {
canvas = holder.lockCanvas();
int cw = canvas.getWidth();
int ch = canvas.getHeight();
updateScaleAndPadding2(cw, ch);
} finally {
if (canvas != null) {
holder.unlockCanvasAndPost(canvas);
}
}
}
public void updateScaleAndPadding2(int cw, int ch) {
if (gif != null) {
int gifW = gif.getIntrinsicWidth();
int gifH = gif.getIntrinsicHeight();
if (gifW * 1f / gifH > cw * 1f / ch) {
scale = ch * 1f / gifH;
} else {
scale = cw * 1f / gifW;
}
horiPadding = (int) ((cw - gifW * scale) / 2);
vertiPadding = (int) ((ch - gifH * scale) / 2);
}
}
}
I have a surface view, and whenever I touch the screen, an image comes up in the spot I touch. It's great, but I cannot figure out how to put a background on the SurfaceView. I have tried using the OnDraw to draw a background right away (Without having to touch it), and that only works some of the time. It force closes most of the time.
Would anyone want to look at my code and see if it's possible to get a background image on the Surface view? Thanks in advance.
class MyView extends SurfaceView implements SurfaceHolder.Callback {
private Thready _thread;
private ArrayList _graphicsz = new ArrayList();
private GraphicObject _currentGraphic = null;
public MyView(Context context) {
super(context);
getHolder().addCallback(this);
_thread = new Thready(getHolder(), this);
setFocusable(true);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
synchronized (_thread.getSurfaceHolder()) {
GraphicObject graphic = null;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
graphic = new GraphicObject(BitmapFactory.decodeResource(getResources(), R.drawable.cat1small));
graphic.getCoordinates().setX((int) event.getX() - graphic.getGraphic().getWidth() / 2);
graphic.getCoordinates().setY((int) event.getY() - graphic.getGraphic().getHeight() / 2);
_currentGraphic = graphic;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
_currentGraphic.getCoordinates().setX((int) event.getX() - _currentGraphic.getGraphic().getWidth() / 2);
_currentGraphic.getCoordinates().setY((int) event.getY() - _currentGraphic.getGraphic().getHeight() / 2);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
_graphicsz.add(_currentGraphic);
_currentGraphic = null;
}
return true;
}
}
#Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
Bitmap bitmap1;
GraphicObject.Coordinates coords1;
for (GraphicObject graphic : _graphicsz) {
bitmap1 = graphic.getGraphic();
coords1 = graphic.getCoordinates();
canvas.drawBitmap(bitmap1, coords1.getX(), coords1.getY(), null);
}
// draw current graphic at last...
if (_currentGraphic != null) {
bitmap1 = _currentGraphic.getGraphic();
coords1 = _currentGraphic.getCoordinates();
canvas.drawBitmap(bitmap1, coords1.getX(), coords1.getY(), null);
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
_thread.setRunning(true);
_thread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
// simply copied from sample application LunarLander:
// we have to tell thread to shut down & wait for it to finish, or else
// it might touch the Surface after we return and explode
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
}
class Thready extends Thread {
private SurfaceHolder _surfaceHolder;
private MyView _panel;
private boolean _run = false;
public Thready(SurfaceHolder surfaceHolder, MyView panel) {
_surfaceHolder = surfaceHolder;
_panel = panel;
}
public void setRunning(boolean run) {
_run = run;
}
public SurfaceHolder getSurfaceHolder() {
return _surfaceHolder;
}
#Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
_panel.onDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
class GraphicObject {
/**
* Contains the coordinates of the graphic.
*/
public class Coordinates {
private int _x = 100;
private int _y = 0;
public int getX() {
return _x + _bitmap.getWidth() / 2;
}
public void setX(int value) {
_x = value - _bitmap.getWidth() / 2;
}
public int getY() {
return _y + _bitmap.getHeight() / 2;
}
public void setY(int value) {
_y = value - _bitmap.getHeight() / 2;
}
public String toString() {
return "Coordinates: (" + _x + "/" + _y + ")";
}
}
private Bitmap _bitmap;
private Coordinates _coordinates;
public GraphicObject(Bitmap bitmap) {
_bitmap = bitmap;
_coordinates = new Coordinates();
}
public Bitmap getGraphic() {
return _bitmap;
}
public Coordinates getCoordinates() {
return _coordinates;
}
}
Well, what strikes me first off all (and what could be causing a lot of problem) is this:
you're creating new bitmap way too often
Every time you get a touch event, you're loading in your bitmaps ... and you can get 80 to 100 touchevents every second!
So to start with, define global bitmaps (private Bitmap bmp1; etc), load them somewhere else and THEN use them in your touch event.
Also, remove the bmp loading/creation from onDraw and move it to somewhere else.
After you've done that, see what happens; you might have more problems (a REALY quick scan of your code seemed fine), but not creating and loading bitmaps 80 times a second will definitely help out :)