M
MightyBird
Neues Mitglied
- 0
Hallo,
meine erste "größere" App nach Hello World
Einfach ein paar Dinge ausprobieren...
Aber: nach einiger Zeit hängt die App, Home Taste auch im Sekundenbereich.
Fällt jemandem was auf? Was muss anders? Wie grenze ich das Problem ein?
Neben dem Code unten habe ich im Manifest noch Landscape festgeschrieben.
Vielen Dank für die Mühe!
Gerhard
meine erste "größere" App nach Hello World
Einfach ein paar Dinge ausprobieren...
Aber: nach einiger Zeit hängt die App, Home Taste auch im Sekundenbereich.
Fällt jemandem was auf? Was muss anders? Wie grenze ich das Problem ein?
Neben dem Code unten habe ich im Manifest noch Landscape festgeschrieben.
Vielen Dank für die Mühe!
Gerhard
Code:
package com.example.grafiktest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.MotionEvent;
import android.graphics.Paint;
import android.graphics.Color;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.SoundPool;
import java.lang.Math;
// import android.graphics;
public class GrafikTest extends Activity implements OnTouchListener {
FastRenderView renderView;
Object object1 = new Object(1000,540) ;
boolean objectTouched = false ;
Paint textPaint;
Paint shapePaint;
SoundPool soundPool ;
int soundID ;
Bitmap objectBitmap;
float touchX = 0 ;
float touchY = 0 ;
boolean touched = false;
int pos = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
renderView = new FastRenderView(this);
renderView.setOnTouchListener(this);
setContentView(renderView);
// define textPaint
textPaint = new Paint();
textPaint.setColor(Color.BLACK);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setTextSize(80);
// define shapePaint
shapePaint = new Paint();
shapePaint.setColor(Color.BLACK);
shapePaint.setStyle(Paint.Style.STROKE);
shapePaint.setStrokeWidth(5);
soundPool = new SoundPool(10,AudioManager.STREAM_MUSIC,0);
soundID = soundPool.load(this,R.raw.schuss,1);
objectBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
}
class Object {
int posX = 0 ;
int posY = 0 ;
public Object (int x, int y) {
this.posX = x ;
this.posY = y ;
}
public int getX () {
return posX ;
}
public int getY () {
return posY ;
}
public void setX (int newX) {
posX = newX ;
}
public void setY (int newY) {
posY = newY ;
}
}
public boolean onTouch (View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchX = event.getX();
touchY = event.getY();
touched = true;
if ( (Math.abs (object1.getX() - touchX) < 100) && (Math.abs (object1.getY() - touchY) < 100) )
{
objectTouched = true ;
soundPool.play(soundID, 80, 80, 1, 0, 1f);
}
break ;
case MotionEvent.ACTION_UP:
touchX = event.getX();
touchY = event.getY();
touched = false;
objectTouched = false ;
break ;
case MotionEvent.ACTION_MOVE:
touchX = event.getX();
touchY = event.getY();
touched = true;
break ;
case MotionEvent.ACTION_CANCEL:
touchX = event.getX();
touchY = event.getY();
touched = false;
objectTouched = false ;
break ;
}
if (objectTouched) {
object1.setX ( (int) touchX );
object1.setY ( (int) touchY );
}
return true;
}
protected void onResume() {
super.onResume();
renderView.resume();
}
protected void onPause() {
super.onPause();
renderView.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.grafik_test, menu);
return true;
}
class FastRenderView extends SurfaceView implements Runnable {
Thread renderThread = null;
SurfaceHolder holder;
volatile boolean running = false;
long lastFrameTime = 0;
public FastRenderView(Context context) {
super(context);
holder = getHolder();
}
public void resume() {
running = true;
renderThread = new Thread(this);
renderThread.start();
}
public void run() {
while(running) {
if(!holder.getSurface().isValid())
continue;
Canvas canvas = holder.lockCanvas();
long frameTime = System.currentTimeMillis() - lastFrameTime ;
int fps = (int) (1000 / frameTime);
lastFrameTime = System.currentTimeMillis();
String fpsString = "fps: "+Long.toString(fps);
String touchXString = "posX: "+Integer.toString(object1.getX());
String touchYString = "posY: "+Integer.toString(object1.getY());
canvas.drawRGB(100, 200, 50);
canvas.drawText(fpsString, 100, 100, textPaint);
canvas.drawText(touchXString, 100, 200, textPaint);
canvas.drawText(touchYString, 100, 270, textPaint);
// canvas.drawCircle(object1.getX(), object1.getY(), 100, shapePaint);
canvas.drawBitmap(objectBitmap, object1.getX()-72, object1.getY()-72, null);
holder.unlockCanvasAndPost(canvas);
}
}
public void onPause() {
running = false;
while(true) {
try {
renderThread.join();
} catch ( InterruptedException e) {
// retry
}
}
}
public void onDestroy() {
soundPool.release();
}
}
}