1 year ago
#352883

Rahul
TimerTask inside of onDraw() Android
I have an Android application that I am working on with Android Studio. I have a custom view that gets rendered from another activity. Inside of the custom view's onDraw() function, I schedule a new timer task to redraw a part of the UI. Initially, when I did this I was getting a SIGSEGV error, but I was able to fix that by adding this: setLayerType(LAYER_TYPE_SOFTWARE, null)
However, after adding this, the Bitmap I drew inside of the Timer doesn't appear on the Canvas.
Here is some of the code for better understanding:
List<Person> people = new ArrayList<>();
public CustomView() {
super(context);
patch = BitmapFactory.decodeResource(this.getResources(), R.drawable.person);
patch = Bitmap.createScaledBitmap(patch, 60, 60, true);
}
protected void onDraw() {
if (drawPatch) { // draw patch is a static boolean var that gets set to true
// from the activity using this view
timer.schedule(new TimerTask() {
@Override
public void run() {
if (currentTime == timeBetweenSpawns) {
people.add(new Person(x, y)); // x, y represents the location of the
// person on the canvas
currentTime = 0;
} else {
currentTime++;
}
}
for (Person person : people) {
person.movePerson(); // moves the person on the canvas
}
for (Person person : people) {
canvas.drawBitmap(person.getBitmap(), person.getX(), person.getY(), null);
}
}, 0, 1000);
}
}
What I am trying to do:
CustomView represents a map for a game. I want to draw people moving on the map in the game and repetitively redraw the people to make it seem like they are moving smoothly. The issue is, I don't want to redraw the entire CustomView when I do this since the only thing changing in the CustomView is the location of the people (also for context there is a lot of other stuff being drawn in the onDraw() function in CustomView, I just omitted it for brevity). The timer logic I attempted above was an attempt to do this, but it didn't work.
Is there a better way to only redraw the Person objects in the people list without having to invalidate and redraw the entire view?
Extra Context
protected void onDraw(Canvas canvas) {
for (Grass grassLocation : grassLocations) {
canvas.drawBitmap(grassLocation.getBitMap(),
grassLocation.getX(),
grassLocation.getY(),
null);
}
for (Path pathLocation : pathLocations) {
canvas.drawBitmap(pathLocation.getBitMap(),
pathLocation.getX(),
pathLocation.getY(),
null);
}
canvas.drawBitmap(monumentLocation.getBitMap(),
monumentLocation.getX(),
monumentLocation.getY(),
null);
for (Indicator indicatorLocation : indicatorLocations) {
canvas.drawBitmap(indicatorLocation.getBitMap(),
indicatorLocation.getX(),
indicatorLocation.getY(),
null);
}
for (Turret turretLocation : turretLocations) {
canvas.drawBitmap(turretLocation.getBitMap(),
turretLocation.getX(),
turretLocation.getY(),
null);
}
if (combatStarted) {
if (currentTime == timeBetweenSpawns) {
addPerson(); // add a new person
currentTime = 0;
} else {
currentTime++;
}
// Move all enemies, check if person has reached monument
for (Person person : people) {
person.movePerson(); // moves the person on the canvas
}
for (Person person : people) {
canvas.drawBitmap(person.getBitmap(), person.getX(),
person.getY(), null);
}
invalidate();
}
}
android
android-studio
android-view
android-bitmap
0 Answers
Your Answer