Im a noob to android development and I am having problems invalidating a view. I am using this tutorial and have no issues implementing it. However, when I change the background of the view, it still responds as if the previous background is still set. In other words, I change the mask but my "touchview" class doesn't see the new mask. I have had no luck using invalidate to update the view and I a have verified that the mask is actually being reset as the background. Any help would be greatly appreciated.
MY CODE
@Override
public boolean onMenuItemClick(com.actionbarsherlock.view.MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId())
{
case 1: // id from the xml file
if(isMale){
isMale=false;
item.setIcon(R.drawable.male_icon);
imageViewOriginal.setImageResource(R.drawable.woman_front);
imageViewFlip.setImageResource(R.drawable.woman_back);
if(isFrontView){
myMask.setBackgroundResource(R.drawable.woman_front_mask); //Mask changed here
}else{
myMask.setBackgroundResource(R.drawable.woman_back_mask); //Mask changed here
}
}else{
isMale=true;
item.setIcon(R.drawable.female_icon);
imageViewOriginal.setImageResource(R.drawable.man_front);
imageViewFlip.setImageResource(R.drawable.man_back);
if(isFrontView){
myMask.setBackgroundResource(R.drawable.man_front_mask); //Mask changed here
}else{
myMask.setBackgroundResource(R.drawable.man_back_mask); //Mask changed here
}
}
touchView.invalidate();
infoView.invalidate();
myMask.invalidate(); //Mask View Invalidated here
return true; // we handled the click, dont pass it up the chain
case 2: // id from the xml file
if(isFrontView){
isFrontView=false;
if(isMale){
myMask.setBackgroundResource(R.drawable.man_back_mask); //Mask changed here
}else{
myMask.setBackgroundResource(R.drawable.woman_back_mask); //Mask changed here
}
}else{
isFrontView=true;
if(isMale){
myMask.setBackgroundResource(R.drawable.man_front_mask); //Mask changed here
}else{
myMask.setBackgroundResource(R.drawable.woman_front_mask); //Mask changed here
}
}
FlipAnimator animator = new FlipAnimator(imageViewOriginal, imageViewFlip,
imageViewFlip.getWidth() / 2, imageViewFlip.getHeight() / 2);
if (imageViewOriginal.getVisibility() == View.GONE) {
animator.reverse();
}
flipLayout.startAnimation(animator);
touchView.invalidate();
infoView.invalidate();
myMask.invalidate(); //Mask View Invalidated here
return true;
}
return false;
}
I can think in two possibilities:
Option 1: You are running your code from a non-UI thread. In this case, use postInvalidate() instead of invalidate()
postInvalidate(): Cause an invalidate to happen on a subsequent cycle through the event loop. Use this to invalidate the View from a non-UI thread.
Option 2: You are running your code from the UI thread. In this case, I would need you to post more code. Remember that invalidate()
is asynchronous, as it only schedules a redraw in the main thread event queue. This means that the redraw will be performed only when the current code has all been executed.
In this case, if something is blocking your UI-Thread, you could use AsyncTask or a Runnable to perform your task.