android - How to update canvas draw text instead of redraw canvas -
i able draw text on canvas on motion view problem when draw text & go next draw on same canvas draw text getting disappear mean screen getting redraw because of invalidate want keep previous draw , make new draw on same canvas how going ?
@override protected void ondraw(canvas canvas) { paint hint = new paint(); path = new path(); mtextpaths = new arraylist<path>(); log.v("getting mtextpaths", mtextpaths.tostring()); int m; if (strgettile != null) { (m = 0; m < strgettile.length(); m++) { system.out.println(strgettile.charat(m)); char convertst = strgettile.charat(m); charactertostring = character.tostring(convertst); // canvas.drawtext(charactertostring, x, y, hint); // canvas.drawtext(charactertostring, m // * width + x, m * height + y, foreground); //its working in // cross // canvas.drawtext(charactertostring, x, m * height + y, // foreground); //its working vertical // canvas.drawtext(charactertostring, m // * width + x, y, foreground); //its working in horizontal // setselectedtile(tile); if (getorientation.equalsignorecase("horizontal")) { canvas.drawtext(charactertostring, m * width + positionx, positiony, foreground); // motion event hint.setcolor(color.black); hint.settextsize(45); foreground.gettextpath(charactertostring, 0, charactertostring.length(), positionx * 2 / 3, positiony - 4, path); } else { canvas.drawtext(charactertostring, positionx, m * height + positiony, foreground); hint.setcolor(color.black); hint.settextsize(45); foreground.gettextpath(charactertostring, 0, charactertostring.length(), positionx * 2 / 3, positiony - 4, path); } } } public void setselectedtile(string tile, string strorientations) { // todo auto-generated method stub log.v("getting string in puzzle view ", tile); strgettile = tile; getorientation = strorientations; mtextpaths.add(path); invalidate(); }
- canvas creating every time whenever calling invalidate() method. maintaining text , positions in arraylist , draw canvas whenever clicking on canvas.
it 1 of way solve problem. canvas bitmap. whatever draw on permanent. there ways handle again implementations redraw entire canvas on every pass.
below code snip:
public class puzzleview extends view { private static final string tag = "sudoku"; private float width; // width of 1 tile private float height; // height of 1 tile private int selx; // x index of selection private int sely; // y index of selection private final rect selrect = new rect(); private final game game; float positionx = 5; float positiony = 15; arraylist<kirti> data = new arraylist<kirti>(); string strgettile = null; paint foreground = new paint(paint.anti_alias_flag); string getorientation; string charactertostring; path path; list<path> mtextpaths = new arraylist<path>(); public puzzleview(context context) { super(context); this.game = (game) context; setfocusable(true); setfocusableintouchmode(true); } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { width = w / 9f; height = h / 9f; // getrect(selx, sely, selrect); log.d(tag, "onsizechanged: width " + width + ", height " + height); super.onsizechanged(w, h, oldw, oldh); } @override protected void ondraw(canvas canvas) { // super.ondraw(canvas); // canvas.save(); // canvas.restore(); // draw background... paint background = new paint(); background.setcolor(getresources().getcolor(r.color.puzzle_background)); canvas.drawrect(0, 0, getwidth(), getheight(), background); // draw board... // define colors grid lines paint dark = new paint(); dark.setcolor(getresources().getcolor(r.color.puzzle_dark)); paint hilite = new paint(); hilite.setcolor(getresources().getcolor(r.color.puzzle_hilite)); paint light = new paint(); light.setcolor(getresources().getcolor(r.color.puzzle_light)); // draw minor grid lines (int = 0; < 9; i++) { canvas.drawline(0, * height, getwidth(), * height, light); canvas.drawline(0, * height + 1, getwidth(), * height + 1, hilite); canvas.drawline(i * width, 0, * width, getheight(), light); canvas.drawline(i * width + 1, 0, * width + 1, getheight(), hilite); } // draw major grid lines (int = 0; < 9; i++) { if (i % 3 != 0) continue; canvas.drawline(0, * height, getwidth(), * height, dark); canvas.drawline(0, * height + 1, getwidth(), * height + 1, hilite); canvas.drawline(i * width, 0, * width, getheight(), dark); canvas.drawline(i * width + 1, 0, * width + 1, getheight(), hilite); } paint hint = new paint(); path = new path(); int m; (int = 0; < data.size(); i++) { string strgettile = data.get(i).getst(); if (strgettile != null) { (m = 0; m < strgettile.length(); m++) { system.out.println(strgettile.charat(m)); char convertst = strgettile.charat(m); charactertostring = character.tostring(convertst); if (data.get(i).getorientation .equalsignorecase("horizontal")) { canvas.drawtext(charactertostring, m * data.get(i).getwidth() + data.get(i).getxposition(), data.get(i) .getyposition(), foreground); // motion // event hint.setcolor(color.black); hint.settextsize(45); } else { // mtextpaths.add(strgettile); // (int = 0; < mtextpaths.size(); i++) { canvas.drawtext(charactertostring, data.get(i) .getxposition(), m * data.get(i).getheight() + data.get(i).getyposition(), foreground); // } hint.setcolor(color.black); hint.settextsize(45); } } try { if (foreground != null) { foreground.gettextpath(charactertostring, 0, charactertostring.length(), data.get(i) .getxposition() * 2 / 3, data.get(i) .getyposition() - 4, path); } } catch (exception e) { } } } } @override public boolean ontouchevent(motionevent event) { if (event.getaction() != motionevent.action_down) game.showkeypadorerror(selx, sely); foreground.setcolor(getresources().getcolor(r.color.puzzle_foreground)); foreground.setstyle(style.fill); foreground.settextsize(height * 0.75f); foreground.settextscalex(width / height); foreground.settextalign(paint.align.center); fontmetrics fm = foreground.getfontmetrics(); positionx = (int) event.getx(); positiony = (int) event.gety() - (fm.ascent + fm.descent) / 2; return true; } public void setselectedtile(string tile, string strorientations) { // todo auto-generated method stub log.v("getting string in puzzle view ", tile); if (game.settileifvalid(selx, sely, tile)) { strgettile = tile; getorientation = strorientations; mtextpaths.add(path); data.add(new kirti(positionx, positiony, strgettile, getorientation, width, height)); invalidate(); log.v("getting mtextpaths", mtextpaths.tostring()); } }}
the model class is:
public class kirti { float positionx; float positiony; string strgettile; string getorientation; float width; float height; public kirti(float positionx, float positiony, string strgettile, string getorientation, float width, float height) { super(); this.positionx = positionx; this.positiony = positiony; this.strgettile = strgettile; this.getorientation = getorientation; this.width = width; this.height = height; } public float getpositionx() { return positionx; } public void setpositionx(float positionx) { this.positionx = positionx; } public float getpositiony() { return positiony; } public void setpositiony(float positiony) { this.positiony = positiony; } public string getstrgettile() { return strgettile; } public void setstrgettile(string strgettile) { this.strgettile = strgettile; } public string getgetorientation() { return getorientation; } public void setgetorientation(string getorientation) { this.getorientation = getorientation; } public float getwidth() { return width; } public void setwidth(float width) { this.width = width; } public float getheight() { return height; } public void setheight(float height) { this.height = height; }}
Comments
Post a Comment