Marking view as dirty from another class
Right now I'm building a g开发者_如何学编程ame for Android. I have a class game.java that calls board.xml as it's view. Board.xml has the following:
... //Score info
<edu.xxx.yyy.zzz.BoardView android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1"/>
... //pause button
... //submit button
BoardView is a Java class that extends View and is used to draw the game board. Everything is being displayed correctly. I want to know if I can implement code in game.java that can mark areas of BoardView as dirty (namely after I hit that Submit button which changes some global variables).
Give the BoardView an id in xml with the attribute
android:id="@+id/myBoardView"
Then just grab the BoardView using findViewById
BoardView myBV = (BoardView) findViewById(R.id.myBoardView);
Then simply mark the areas dirty that you want to be made dirty by using the invalidate method
myBV.invalidate(); //invalidates the entire BoardView
or
Rect dirty = new Rect(...);
myBV.invalidate(dirty); //marks the area defined by dirty as dirty
or
int left = 0; int right = 10; int top = 0; int bot = 10;
myBV.invalidate(left, top, right, bot); //invalidates the area defined by these coords as dirty
精彩评论