Здравствуйте,помогите разобраться в чем причина.. Пишу приложение в Android Studio.
Запускаю кнопкой Run.
На подключенном устройстве все запускается. Ошибок нет, работает как надо. :
................................................................................................................................................................
Но если я запускаю это приложение с телефона нажатием по значку.
То не запускается. Сначала белый экран. Потом темный и все... висит..
Я предполагаю что это как то связано с тем что я в setContentView() помещаю класс DrawSurface(this). Потому что с R.layout.activity_circle не возникает проблем. Вот код:
public class CircleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new DrawSurface(this));
//setContentView(R.layout.activity_circle);
}
}
Класс DrawSurface:
public class DrawSurface extends SurfaceView implements SurfaceHolder.Callback{
private DrawThread thread;
private String TAG = "LOG";
public DrawSurface(Context context){
super(context);
getHolder().addCallback(this);
thread = new DrawThread(getHolder(), this);
setFocusable(true);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
thread.start();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while(retry){
try{
thread.join();
retry = false;
}catch(InterruptedException e){
}
}
}
public boolean onTouchEvent(MotionEvent event){
if(event.getAction()== MotionEvent.ACTION_DOWN){
thread.touchX = event.getX();
thread.touchY = event.getY();
Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());
}
return true;
}
}
Класс DrawThread:
public class DrawThread extends Thread{
private double speedMoveX = 1;
private double speedMoveY = 1;
private double Y0 = 0;
private double Angle = 1;
private Random rand;
private double gravityForce = 1;
private int newAngle = 0;
private Bitmap myBitmap;
private long prevTime;
private float X=0;
private float Y=0;
protected float touchX=-40;
protected float touchY=-40;
private Paint paint;
private Paint paintScore;
private int currentScore = 0;
private int textSize = 54;
private boolean running = false;
private SurfaceHolder surfaceHolder;
private DrawSurface drawSurface;
private String LOG_TAG = "LOG";
public DrawThread(SurfaceHolder surfaceHolder, DrawSurface drawSurface){
super();
this.surfaceHolder = surfaceHolder;
prevTime = System.currentTimeMillis();
rand = new Random();
Angle = rand.nextInt(25);
paint = new Paint();
paintScore = new Paint();
paintScore.setTextSize(textSize);
}
public void setRunning(boolean running){
this.running = running;
}
public void updateCord(){
X += speedMoveX * Math.cos(Angle * Math.PI / 180);
Y += speedMoveY * Math.sin(Angle * Math.PI / 180);
if (Y > Y0+9.8){
speedMoveY += gravityForce;
Y0 = Y;
}
if (X > 420){
X = 420;
speedMoveX = -speedMoveX;
}
if (X < 60){
X = 60;
speedMoveX = -speedMoveX;
}
if (Y >= 760){
Y = 760;
speedMoveY = -speedMoveY;
}
if (Y < 60){
Y = 60;
speedMoveY = -speedMoveY;
}
}
private boolean isHit(){
boolean hit = false;
if(touchX > X-60 && touchX < X+60 && touchY > Y-60 && touchY <Y+60){
hit = true;
}
return hit;
}
public void run(){
Log.d(LOG_TAG, "Starting game loop");
long tickCount=0;
Canvas canvas = null;
paint.setColor(Color.BLACK);
paint.setStrokeWidth(8);
while (running){
tickCount++;
long now = System.currentTimeMillis();
long elapsedTime = now - prevTime;
/* if (elapsedTime > 1){
prevTime = now;
updateCord();
}*/
try {
canvas = surfaceHolder.lockCanvas(null);
if (canvas == null)
continue;
canvas.drawColor(Color.GRAY);
Log.d("LOG onDraw()", "onDraw()");
canvas.drawText("touchX = " + touchX + ", touchY = " + touchY, 10, 20, paint);
canvas.drawText("Score:",200,50,paintScore);
canvas.drawText(currentScore+"" ,360,50,paintScore);
canvas.drawCircle(X, Y,60, paint);
if (isHit()){
currentScore++;
paint.setColor(Color.BLUE);
Angle = rand.nextInt(20);
X = rand.nextInt(330);
Y = rand.nextInt(760);
}
else{
paint.setColor(Color.RED);
touchX=-40;
touchY=-40;
}
}finally {
if (canvas != null){
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
Log.d(LOG_TAG, "Game loop executes"+tickCount+" times");
}
}
Вот AndroidManifect.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="circle.denis.com.circle" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".CircleActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>