【问题标题】:Check if the imageView has a drawable [duplicate]检查imageView是否有可绘制的[重复]
【发布时间】:2017-03-01 11:29:48
【问题描述】:
if (image.getDrawable().getConstantState().equals(getResources().getDrawable(0).getConstantState())){
            image.setVisibility(View.GONE);
        }else{
            image.setVisibility(View.VISIBLE);
        }

也就是说,如果图像没有任何可绘制的,图像将消失,否则可见。但是这段代码不起作用

【问题讨论】:

    标签: android imageview android-drawable


    【解决方案1】:

    试试下面可能对你有用(对我有用)

    if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        if (image.getDrawable().getConstantState().equals(image.getContext().getDrawable(R.drawable.shadow_round_white).getConstantState())){
    
           image.setVisibility(View.GONE);
        }
        else{
           image.setVisibility(View.VISIBLE);
        }
    
     }
    else {
        if (image.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.shadow_round_white).getConstantState())){
           image.setVisibility(View.GONE);
        }
    
        else{
           image.setVisibility(View.VISIBLE);
        }
     }
    

    【讨论】:

      【解决方案2】:

      可以在image view上调用getDrawable函数,检查是否不为null - https://stackoverflow.com/a/16445086/1649353

      【讨论】:

        【解决方案3】:
        if (image.getDrawable().getConstantState().equals
                    (getResources().getDrawable(R.drawable.your_drawable).getConstantState()){//set here your drawable name(your_drawable)
                    image.setVisibility(View.VISIBLE);
                }else{
                    image.setVisibility(View.GONE);
                }
        

        【讨论】:

          【解决方案4】:

          其实还有一种比较方式:

          if(imageView.getDrawable().getConstantState().equals
                      (getResources().getDrawable(/*Your drawable*/).getConstantState()))
          imageView.setVisibility(View.VISIBLE);
                  else
                      imageView.setVisibility(View.GONE);
          

          【讨论】:

            【解决方案5】:
                if(imageview.getDrawable()==null)
                {
                  //if Image View is Null 
                }
            

            检查source

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-02-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-03-23
              相关资源
              最近更新 更多