WallpaperService oder Engine in Activity anzeigen

  • Antworten:4
  • Bentwortet
Aaron B.
  • Forum-Beiträge: 206

11.06.2013, 18:05:51 via App

Hallo zusammen!

Ich habe ein Live-Wallpaper (Tick tack toe Spiel) programmiert und möchte dieses jetzt in einer Activity anzeigen.

Gibt es eine Möglichkeit, die Engine irgendwie in einer Activity anzuzeigen?

Wenn nicht, was wäre der beste Weg dafür? Ich habe bereits von SurfaceView gehört, weiß aber nicht, ob das der beste Weg ist...

Danke im Voraus :)


EDIT:

Dies ist der WallpaperService, den ich für mein Live-Wallpaper nutze:


package de.ticktacktoewallpaper;

import android.content.*;
import android.graphics.*;
import android.os.*;
import android.service.wallpaper.*;
import android.view.*;
import java.util.*;

public class Service extends WallpaperService
{

private final Handler handler = new Handler();

private final String NONE = " ";
private final String X = "X";
private final String O = "O";
private final String GS = "Gleichstand";

@Override
public Engine onCreateEngine() {
return new WallpaperEngine();
}

private class WallpaperEngine extends Engine {

private final Paint paint;
private final Runnable runnable;
private final Calendar cal;
private float x, y;
private boolean visible;
private boolean clicked = false;

String i7 = NONE;
String i8 = NONE;
String i9 = NONE;

String i4 = NONE;
String i5 = NONE;
String i6 = NONE;

String i1 = NONE;
String i2 = NONE;
String i3 = NONE;

String player = X;
String winner = NONE;

WallpaperEngine() {
paint = new Paint();
runnable = new Runnable() {
public void run() {
draw();
}
};
cal = Calendar.getInstance();
}

@Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
setTouchEventsEnabled(true);
}

@Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runnable);
}

@Override
public void onVisibilityChanged(boolean v) {
visible = v;
if(v) {
draw();
} else {
handler.removeCallbacks(runnable);
}
}

@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
x = width / 2.0f;
y = height / 2.0f;

draw();
}

@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
visible = false;
handler.removeCallbacks(runnable);
}

@Override
public void onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if(event.getAction() == MotionEvent.ACTION_DOWN) {
clicked = true;

x = event.getX();
y = event.getY();

draw();
} else if(event.getAction() == MotionEvent.ACTION_UP) {
draw();
}
}

void draw() {
final SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try {
c = holder.lockCanvas();
if(c != null) {
int w = c.getWidth();
int h = c.getHeight();

float fa = w / 2 / 2;
float fb = 200;
float fc = fa + (w / 2);
float fd = (w / 2) + fb;

float part = w / 2 / 5;

float x1 = part + fa;
float x2 = (2.5f * part) + fa;
float x3 = (4 * part) + fa;

float y1 = part + 200;
float y2 = (2.5f * part) + 200;
float y3 = (4 * part) + 200;

float i = 30;

paint.setColor(Service.this.getSharedPreferences("app", Context.MODE_PRIVATE).getInt("bgcolor", Color.WHITE));
c.drawRect(0, 0, w, h, paint);

paint.setColor(Color.parseColor("#55000000"));
//c.drawRect(fa, fb, fc, fd, paint);
c.drawRect(x1 - i, y1 - i, x1 + i, y1 + i, paint);
c.drawRect(x1 - i, y2 - i, x1 + i, y2 + i, paint);
c.drawRect(x1 - i, y3 - i, x1 + i, y3 + i, paint);

c.drawRect(x2 - i, y1 - i, x2 + i, y1 + i, paint);
c.drawRect(x2 - i, y2 - i, x2 + i, y2 + i, paint);
c.drawRect(x2 - i, y3 - i, x2 + i, y3 + i, paint);

c.drawRect(x3 - i, y1 - i, x3 + i, y1 + i, paint);
c.drawRect(x3 - i, y2 - i, x3 + i, y2 + i, paint);
c.drawRect(x3 - i, y3 - i, x3 + i, y3 + i, paint);

paint.setColor(Color.WHITE);
paint.setTextAlign(Paint.Align.CENTER);
paint.setTextSize(15);

c.drawText(i7, x1, y1, paint);
c.drawText(i8, x2, y1, paint);
c.drawText(i9, x3, y1, paint);

c.drawText(i4, x1, y2, paint);
c.drawText(i5, x2, y2, paint);
c.drawText(i6, x3, y2, paint);

c.drawText(i1, x1, y3, paint);
c.drawText(i2, x2, y3, paint);
c.drawText(i3, x3, y3, paint);

paint.setColor(Color.parseColor("#55000000"));
c.drawRect((w / 2) - 100, ((h / 5) + (h / 2)) - 50, (w / 2) + 100, ((h / 5) + (h / 2)) + 50, paint);

paint.setColor(Color.WHITE);
paint.setTextSize(20);
c.drawText("Reset", w / 2, (h / 5) + (h / 2), paint);

paint.setColor(Color.parseColor("#55000000"));
paint.setTextSize(50);
c.drawText("Tick Tack Toe", w / 2, 100, paint);

paint.setTextSize(20);
if(winner == NONE) {
c.drawText(player + " ist dran", w / 2, 150, paint);
} else if(winner == GS) {
paint.setColor(Color.RED);
c.drawText("Es ist " + GS, w / 2, 150, paint);
} else {
paint.setColor(Color.RED);
c.drawText(nextPlayer() + " hat gewonnen!", w / 2, 150, paint);
}

paint.setColor(Color.parseColor("#55000000"));
paint.setTextAlign(Paint.Align.RIGHT);
c.drawText("by RB", w - 25, 100, paint);

//Linien

paint.setColor(Color.RED);
paint.setStrokeWidth(5);

if(winner != NONE) {
if((i7 == X && i8 == X && i9 == X) || (i7 == O && i8 == O && i9 == O)) {
c.drawLine(x1, y1, x3, y1, paint);
} else if((i4 == X && i5 == X && i6 == X) || (i4 == O && i5 == O && i6 == O)) {
c.drawLine(x1, y2, x3, y2, paint);
} else if((i1 == X && i2 == X && i3 == X) || (i1 == O && i2 == O && i3 == O)) {
c.drawLine(x1, y3, x3, y3, paint);
} else if((i7 == X && i4 == X && i1 == X) || (i7 == O && i4 == O && i1 == O)) {
c.drawLine(x1, y1, x1, y3, paint);
} else if((i8 == X && i5 == X && i2 == X) || (i8 == O && i5 == O && i2 == O)) {
c.drawLine(x2, y1, x2, y3, paint);
} else if((i9 == X && i6 == X && i3 == X) || (i9 == O && i6 == O && i3 == O)) {
c.drawLine(x3, y1, x3, y3, paint);
} else if((i7 == X && i5 == X && i3 == X) || (i7 == O && i5 == O && i3 == O)) {
c.drawLine(x1, y1, x3, y3, paint);
} else if((i9 == X && i5 == X && i1 == X) || (i9 == O && i5 == O && i1 == O)) {
c.drawLine(x3, y1, x1, y3, paint);
}
}

//click

if(clicked) {
clicked = false;

if(x >= (w / 2) - 100 && x <= (w / 2) + 100 && y >= ((h / 5) + (h / 2)) - 50 && y <= ((h / 5) + (h / 2)) + 50) {
//Toast.makeText(Service.this, "Reset", Toast.LENGTH_SHORT).show();

//alle booleans zurücksetzen
i7 = NONE;
i8 = NONE;
i9 = NONE;

i4 = NONE;
i5 = NONE;
i6 = NONE;

i1 = NONE;
i2 = NONE;
i3 = NONE;

player = X;
winner = NONE;
} else if(winner == NONE) {
if(x >= x1 - i && x <= x1 + i && y >= y1 - i && y <= y1 + i) {
//7
if(i7 == NONE) {
i7 = player;
player = nextPlayer();
}
} else if(x >= x2 - i && x <= x2 + i && y >= y1 - i && y <= y1 + i) {
//8
if(i8 == NONE) {
i8 = player;
player = nextPlayer();
}
} else if(x >= x3 - i && x <= x3 + i && y >= y1 - i && y <= y1 + i) {
//9
if(i9 == NONE) {
i9 = player;
player = nextPlayer();
}
} else if(x >= x1 - i && x <= x1 + i && y >= y2 - i && y <= y2 + i) {
//4
if(i4 == NONE) {
i4 = player;
player = nextPlayer();
}
} else if(x >= x2 - i && x <= x2 + i && y >= y2 - i && y <= y2 + i) {
//5
if(i5 == NONE) {
i5 = player;
player = nextPlayer();
}
} else if(x >= x3 - i && x <= x3 + i && y >= y2 - i && y <= y2 + i) {
//6
if(i6 == NONE) {
i6 = player;
player = nextPlayer();
}
} else if(x >= x1 - i && x <= x1 + i && y >= y3 - i && y <= y3 + i) {
//1
if(i1 == NONE) {
i1 = player;
player = nextPlayer();
}
} else if(x >= x2 - i && x <= x2 + i && y >= y3 - i && y <= y3 + i) {
//2
if(i2 == NONE) {
i2 = player;
player = nextPlayer();
}
} else if(x >= x3 - i && x <= x3 + i && y >= y3 - i && y <= y3 + i) {
//3
if(i3 == NONE) {
i3 = player;
player = nextPlayer();
}
}

if(i7 != NONE && i8 != NONE && i9 != NONE && i4 != NONE && i5 != NONE && i6 != NONE && i1 != NONE && i2 != NONE && i3 != NONE) {
winner = GS;
}
}
}

x = 0;
y = 0;

if(i7 == X && i8 == X && i9 == X) {
winner = X;
} else if(i4 == X && i5 == X && i6 == X) {
winner = X;
} else if(i1 == X && i2 == X && i3 == X) {
winner = X;
} else if(i7 == X && i4 == X && i1 == X) {
winner = X;
} else if(i8 == X && i5 == X && i2 == X) {
winner = X;
} else if(i9 == X && i6 == X && i3 == X) {
winner = X;
} else if(i7 == X && i5 == X && i3 == X) {
winner = X;
} else if(i9 == X && i5 == X && i1 == X) {
winner = X;
} else if(i7 == O && i8 == O && i9 == O) {
winner = O;
} else if(i4 == O && i5 == O && i6 == O) {
winner = O;
} else if(i1 == O && i2 == O && i3 == O) {
winner = O;
} else if(i7 == O && i4 == O && i1 == O) {
winner = O;
} else if(i8 == O && i5 == O && i2 == O) {
winner = O;
} else if(i9 == O && i6 == O && i3 == O) {
winner = O;
} else if(i7 == O && i5 == O && i3 == O) {
winner = O;
} else if(i9 == O && i5 == O && i1 == O) {
winner = O;
}

}
} finally {
if(c != null)
holder.unlockCanvasAndPost(c);
}
handler.removeCallbacks(runnable);
if(visible) {
handler.postDelayed(runnable, 10000);
}
}

String nextPlayer() {
if(player == X) {
return O;
} else {
return X;
}
}

}

}

— geändert am 16.06.2013, 18:23:33

Antworten
Michele
  • Forum-Beiträge: 1.525

11.06.2013, 19:12:18 via Website

Ähh setContentView(view);

Meinst du das?

Deine Klasse heißt Service.
Also:
1Service service = Service(this);
2setContentView(service);


LG

Antworten
Aaron B.
  • Forum-Beiträge: 206

11.06.2013, 22:56:46 via App

Aber das ist kein view sondern ein WallpaperService. Wenn ich setContentView(new Service()); mache, kommt ein Fehler:

"There is no applicable method to 'de.package.Service'!"

LG

Antworten
Michele
  • Forum-Beiträge: 1.525

11.06.2013, 22:59:12 via Website

Ich habe sowas noch nie gemacht.
Deswegen ein versuch war es wert.

Was anderes würde mir nun nicht einfallen auf die schnelle.


LG

Antworten
Aaron B.
  • Forum-Beiträge: 206

12.06.2013, 13:11:54 via App

Trotzdem danke :)

Antworten