如何检查视图设置特定的背景 [英] How to check if the view is set the specific background

查看:151
本文介绍了如何检查视图设置特定的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图比较视图背景的绘制,但它不是为我工作。

 查看V1 = options.findViewById(I)
v1.findViewById(R.drawable.back);
可绘制D = v1.getBackground();如果(d.getConstantState()== getResources()。getDrawable(R.drawable.correct_ans_back)){
    v1.setBackgroundResource(R.drawable.a);
}其他{
    view.setBackgroundResource(R.drawable.b);
}

如何检查我在这里得到一个错误。

 不兼容的运算数类型Drawable.Constant国家和绘制对象


解决方案

1)更换

 如果(d.getConstantState()== getResources()。getDrawable(R.drawable.correct_ans_back))

 如果(d.getConstantState()== getResources()。getDrawable(R.drawable.correct_ans_back).getConstantState())

这将解决不兼容的运算数类型Drawable.Constant国家和绘制对象错误。

2)如果你不能够比较两个位图,那么你可以使用下面的方法。

 公共布尔compareDrawable(D1可绘制,绘制对象D2){
    尝试{
        位图bitmap1 =((BitmapDrawable)d1)的.getBitmap();
        ByteArrayOutputStream流1 =新ByteArrayOutputStream();
        bitmap1.com preSS(Bitmap.Com pressFormat.JPEG,100,流1);
        stream1.flush();
        字节[] = bitmapdata1 stream1.toByteArray();
        stream1.close();        位图bitmap2 =((BitmapDrawable)D2).getBitmap();
        ByteArrayOutputStream流2 =新ByteArrayOutputStream();
        bitmap2.com preSS(Bitmap.Com pressFormat.JPEG,100,流2);
        stream2.flush();
        字节[] = bitmapdata2 stream2.toByteArray();
        stream2.close();        返回bitmapdata1.equals(bitmapdata2);
    }
    赶上(例外五){
        // TODO:处理异常
    }
    返回false;
}

3)或者,也可以将两种不同的标记背景图像和比较标记改为只直接比较绘制。
您还可以设置背景的标记作为绘制的ID和如下所述进行比较,

 对象标记= bgView.getTag();
INT BACKGROUNDID = R.drawable.bg_image;
如果(标记= NULL&放大器;!及((整数)标签).intValue()== BACKGROUNDID){
   //做你的工作。
}

i am trying to compare the view background to the drawable but its not working for me.

View v1 = options.findViewById(i);
v1.findViewById(R.drawable.back);
Drawable d = v1.getBackground();       

if(d.getConstantState() == getResources().getDrawable(R.drawable.correct_ans_back)){
    v1.setBackgroundResource(R.drawable.a);             
}else{
    view.setBackgroundResource(R.drawable.b);               
}

how to check i am getting an error here.

incompatible operand types Drawable.Constant State and Drawable

解决方案

1) Replace

if(d.getConstantState() == getResources().getDrawable(R.drawable.correct_ans_back))

with

if(d.getConstantState() == getResources().getDrawable(R.drawable.correct_ans_back).getConstantState())

This will solve the incompatible operand types Drawable.Constant State and Drawable error.

2) If you are not able to compare two bitmaps, then you can use the below method.

public boolean compareDrawable(Drawable d1, Drawable d2){
    try{
        Bitmap bitmap1 = ((BitmapDrawable)d1).getBitmap();
        ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
        bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, stream1);
        stream1.flush();
        byte[] bitmapdata1 = stream1.toByteArray();
        stream1.close();

        Bitmap bitmap2 = ((BitmapDrawable)d2).getBitmap();
        ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
        bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, stream2);
        stream2.flush();
        byte[] bitmapdata2 = stream2.toByteArray();
        stream2.close();

        return bitmapdata1.equals(bitmapdata2);
    }
    catch (Exception e) {
        // TODO: handle exception
    }
    return false;
}

3) OR , you can assign two different TAG to the background images and compare the TAG only instead of comparing drawable directly. You can also set background's TAG as drawable's id and compare it as described below,

Object tag = bgView.getTag(); 
int backgroundId = R.drawable.bg_image;
if( tag != null && ((Integer)tag).intValue() == backgroundId) {
   //do your work.
}

这篇关于如何检查视图设置特定的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆