setBackgroundDrawable

R

RED-BARON

Fortgeschrittenes Mitglied
19
Hallo, ich scheiter mal wieder an einem scheinbar einfachen Thema.

es wird ein Klasse abgeleitet von Drawable zum Zeichnen verwendet.

onCreate()
{
bgDraw
= new BackgroundRaster(context, view);
view.setBackgroundDrawable(bgDraw);
}

Das Raster wird angezeigt. Hintergrund weiss mit grauen Linien.

die draw-methode von BackgroundRaster sieht wie folgt aus


Code:
[LEFT][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] draw(Canvas canvas) [/SIZE][/LEFT]
 
[SIZE=2]{[/SIZE]
[INDENT][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] view_w = [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]view[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getWidth();[/SIZE]

[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] view_h = [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]view[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getHeight();[/SIZE][/LEFT]

 

[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] padded_horizontal_room = view_w - ([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]padding_left[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]padding_right[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] padded_vertical_room = view_h - ([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]padding_top[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]padding_bottom[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE][/LEFT]
[/LEFT]

 

[LEFT][SIZE=2]Rect bounds = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Rect(0, 0, view_w, view_h);[/SIZE][/LEFT]

 

[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]paint[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setColor(Color.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]WHITE[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]paint[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setAlpha(255);[/SIZE]
[SIZE=2]canvas.drawRect(bounds, [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]paint[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE][/LEFT]
[/LEFT]

 

[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]paint[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setColor(Color.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]GRAY[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]paint[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setStyle(Style.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]STROKE[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]paint[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setAlpha(62);[/SIZE][/LEFT]
[/LEFT]

 

[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]float[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] dist_h = ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]float[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]) (view_h / ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]float[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2])[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]lines[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]float[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] dist_w = ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]float[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]) (view_w / ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]float[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2])[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]lines[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE][/LEFT]
[/LEFT]

 

[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] array_size = [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]lines[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] * 4;[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]float[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][] pts = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]float[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][array_size]; [/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]for[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] i = 0; i < array_size; i=i+4) [/SIZE]
[SIZE=2]{[/SIZE][/LEFT]
[/LEFT]

 
[INDENT][LEFT][SIZE=2]pts[i+0] = 0;[/SIZE][/LEFT]
 
 

[SIZE=2]pts[i+1] = dist_h * i / 4 ; [/SIZE][LEFT][SIZE=2]pts[i+2] = view_w; [/SIZE][/LEFT]

 
 
 

[LEFT][SIZE=2]pts[i+3] = dist_h * i / 4; [/SIZE][/LEFT]

 
 
 
[LEFT][/LEFT]
[/INDENT][SIZE=2]}[/SIZE]
[LEFT][SIZE=2]canvas.drawLines(pts, [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]paint[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]for[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] i = 0; i < array_size; i=i+4) [/SIZE][/LEFT]
 
[SIZE=2]{[/SIZE]
[INDENT][LEFT][SIZE=2]pts[i+0] = dist_w * i / 4 ;[/SIZE]

[SIZE=2]pts[i+1] = 0 ; [/SIZE][/LEFT]

 
[LEFT][/LEFT]
[/INDENT][INDENT][SIZE=2]pts[i+2] = dist_w * i / 4; [/SIZE]
 
 
[LEFT][SIZE=2]pts[i+3] = view_h; [/SIZE][/LEFT]
 
 
 
 
[/INDENT][SIZE=2]} [/SIZE]
[LEFT][SIZE=2]canvas.drawLines(pts, [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]paint[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]paint[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setColor(Color.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]WHITE[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]paint[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setAlpha(255);[/SIZE][/LEFT]
 
[/INDENT][SIZE=2]}[/SIZE]

Problem: beim Wechsel der Activity zu einer anderen, wird kurz der weisse Hintergrund schwarz - das graue Raster bleibt aber bestehen. Kommt man
zurück zur Activity, die solange auf dem Stack lag - ist der Hintergrund noch
oder wieder schwarz, das Raster ist weiterhin zu erkennen.

Wie kommt es zu diesem Effekt und wie kann dieser Umgangen werden ?

Vielen Dank, RB


Edit:

gelöst ;) da ich nur ein paint Objekt habe und den Style STROKE eingeschaltet habe, aber nicht wieder ausgeschaltet ...
entstand am Rand nur ein weisser Streifen...

paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);


paint.setAlpha(255);
canvas.drawRect(bounds, paint);
 
Zuletzt bearbeitet:

Ähnliche Themen

F
Antworten
3
Aufrufe
1.039
amfa
A
Zurück
Oben Unten