检查 imageView 是否有可绘制对象
Check if the imageView has a drawable
if (image.getDrawable().getConstantState().equals(getResources().getDrawable(0).getConstantState())){
image.setVisibility(View.GONE);
}else{
image.setVisibility(View.VISIBLE);
}
也就是说,如果image没有任何drawable,image将是GONE,否则Visible。但是这段代码不起作用
您可以在图像视图上调用 getDrawable 函数并检查它是否不为空 -
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);
}
其实还有一种比较方式:
if(imageView.getDrawable().getConstantState().equals
(getResources().getDrawable(/*Your drawable*/).getConstantState()))
imageView.setVisibility(View.VISIBLE);
else
imageView.setVisibility(View.GONE);
if(imageview.getDrawable()==null)
{
//if Image View is Null
}
检查source
Try below may be it works for you(It's work for me)
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);
}
}
if (image.getDrawable().getConstantState().equals(getResources().getDrawable(0).getConstantState())){
image.setVisibility(View.GONE);
}else{
image.setVisibility(View.VISIBLE);
}
也就是说,如果image没有任何drawable,image将是GONE,否则Visible。但是这段代码不起作用
您可以在图像视图上调用 getDrawable 函数并检查它是否不为空 -
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);
}
其实还有一种比较方式:
if(imageView.getDrawable().getConstantState().equals
(getResources().getDrawable(/*Your drawable*/).getConstantState()))
imageView.setVisibility(View.VISIBLE);
else
imageView.setVisibility(View.GONE);
if(imageview.getDrawable()==null)
{
//if Image View is Null
}
检查source
Try below may be it works for you(It's work for me)
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);
}
}