为什么自定义视图无法刷新?

8
我是一名Android开发新手,遇到了无法使视图失效的问题。我正在使用this教程,并且在实现时没有任何问题。但是,当我更改视图的背景时,它仍然会响应先前设置的背景。换句话说,我更改了遮罩,但我的“touchview”类并没有看到新的遮罩。我尝试使用invalidate来更新视图,但没有成功,并且我已经验证了遮罩实际上被重置为背景。非常感谢您的帮助。
我的代码:
@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;
}

1
为什么你不在case语句中使用R.id.itemId,这样更直观。你确定你的代码被调用了吗?链接已经失效,请在这里发布你的代码。 - VinceStyling
Action Bar Sherlock让我使用int ids创建菜单项。上面的链接有效,代码被调用。 - B. Money
1个回答

24
我能想到两种可能性: 选项一: 您正在非UI线程中运行代码。在这种情况下,请使用postInvalidate()而不是invalidate()

postInvalidate(): 在通过事件循环的后续周期中使无效。使用此方法从非UI线程中使View失效。

选项二: 您正在UI线程中运行代码。在这种情况下,我需要您发布更多的代码。请记住,invalidate()是异步的,因为它只在主线程事件队列中安排了一个重绘。这意味着只有在当前代码都被执行完毕后才会执行重绘。
在这种情况下,如果某些事情阻塞了您的UI线程,您可以使用AsyncTask或Runnable来执行任务。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接