android - 为什么自定义 View 不失效?

标签 android layout view mask invalidation

我是 android 开发的菜鸟,我在使 View 无效时遇到了问题。我正在使用 this教程,并且在实现它时没有任何问题。但是,当我更改 View 的背景时,它仍然会像以前的背景一样进行响应。换句话说,我更改了掩码,但我的“touchview”类没有看到新掩码。我没有运气使用 invalidate 来更新 View ,我已经验证了掩码实际上被重置为背景。任何帮助将不胜感激。

我的代码

@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:您正在从非 UI 线程运行代码。在这种情况下,使用 postInvalidate() 而不是 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.

选项 2:您从 UI 线程运行代码。在这种情况下,我需要您发布更多代码。请记住,invalidate() 是异步的,因为它只在主线程事件队列中安排重绘。也就是说只有当前代码全部执行完才会重绘。

在这种情况下,如果有什么东西阻塞了您的 UI 线程,您可以使用 AsyncTask 或 Runnable 来执行您的任务。

关于android - 为什么自定义 View 不失效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16418842/

相关文章:

Android 在顶部和底部 ListView 上设置淡入淡出的 alpha 渐变,如 instagram 实时聊天

android - 永远不会调用带有 Retrofit 2.0.2 的自定义 xml 转换器

android - ViewPager 使用 xml 文件作为 View

带有 IF 语句的 MySQL VIEW

MySQL 经常重新创建 View 的性能

java - 如何将数据存储到数组以及从数组中求和数据

c++ - QLabel在调整大小时切断文本

html - Bootstrap 4.0 移动布局的排序/移动列

java - Swing 文本 API : Override LabelView with different string length?

android - Flutter Firestore QuerySnapshot 没有 getter 'document 的实例