我无法比较view.getBackground()==R.drawable.image?[android]

我无法比较view.getBackground()==R.drawable.image?[android],view,android-imageview,android-drawable,View,Android Imageview,Android Drawable,我需要将作为背景加载的图像与imageView进行比较。我是这样做的: @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN) { switch (v.getId()) { case R.id.hero1: i

我需要将作为背景加载的图像与imageView进行比较。我是这样做的:

 @Override
    public boolean onTouch(View v, MotionEvent event) {

        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            switch (v.getId()) {
                case R.id.hero1:
                    if(v.getBackground() == R.drawable.hero1){
                    //do something
              }
                    break;
                case R.id.hero2:
                 //other stuff
          } 
但v.getBackground()与我在drawable文件夹中的内容不可比较。 为什么?

请注意,v.getBackgroundResource()不可用。

您可以这样做-

  @Override
        public boolean onTouch(View v, MotionEvent event) {

            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                switch (v.getId()) {
                    case R.id.hero1:

                      if (v.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.hero1).getConstantState())) {
                         // Do something here
                      }

                        break;
                    case R.id.hero2:
                     //other stuff
              } 

什么是恒定状态?你可以在这里读到