setImageDrawable未设置当前drawable

时间:2013-12-18 09:52:30

标签: android drag-and-drop android-drawable

在阅读了几十篇关于这个主题的问答后,我找不到这种情况的答案。

在我的应用中,用户将不同的图标从一个表格布局拖动到另一个表格布局(底部表格布局是一种“图标键盘”)。

将图标拖到上面的表格后(它会替换占位符图像),用户应该可以继续拖动它到不同的位置。

现在,第一部分很有效。我可以将图标从“键盘”拖动到上面的表格布局中。但出于某种原因,当我尝试在此布局中的不同位置之间拖动它时,应该更改为图标的imageView将保留相同的drawable(占位符)。

public boolean onDrag(View v, DragEvent event) {
      ImageView imageOnBoard = (ImageView) v;
      switch (event.getAction()) {
      case DragEvent.ACTION_DRAG_STARTED:
               break;
      case DragEvent.ACTION_DRAG_ENTERED:   
          imageOnBoard.setBackgroundColor(0xFF00FF00);

        break;
      case DragEvent.ACTION_DRAG_EXITED:   
          imageOnBoard.setBackgroundColor(color.transparent);  
          imageOnBoard.setOnTouchListener(null);
        break;
      case DragEvent.ACTION_DROP:        


         imageOnBoard.setBackgroundColor(color.transparent);
        TableRow tr1 =  (TableRow) imageOnBoard.getParent();
        int r=tr1.indexOfChild(imageOnBoard);
        TableLayout tl1  = (TableLayout) tr1.getParent();
        int c=tl1.indexOfChild(tr1);


        ImageView strategyIcon = (ImageView) (event.getLocalState()); 
        TableLayout source = (TableLayout) strategyIcon.getParent().getParent();
        if (source.equals((TableLayout) findViewById(R.id.tableLayout)))  
        {

            Drawable draw = strategyIcon.getDrawable();
            imageOnBoard.setImageDrawable(draw);

        }
        else
        {

             Drawable draw = strategyIcon.getDrawable();
            imageOnBoard.setImageDrawable(draw);

        }


        imageOnBoard.setVisibility(View.VISIBLE);
        imageOnBoard.setOnTouchListener(new MyTouchListener());

        break;
      case DragEvent.ACTION_DRAG_ENDED:  

            break;

      default:
        break;
      }
      return true;
    }

正如你所看到的,我将DRAG_DROP动作分为两种情况(从“键盘”拖动并拖动上部布局。我测试了它,它确实进入了不同的情况,但是可绘制的对象似乎“没有更新”。

请帮忙!

我现在明白问题是第二次提取Drawable(getDrawable)时,从上层布局,我没有收到当前的drawable,而是原来的drawable。我怎么处理这个?

5 个答案:

答案 0 :(得分:1)

我遇到同样的问题,当我使用image1.setImageDrawable(add);将Image设置为ImageView时有时它不起作用,后来我使用滑动来设置图像: 例如Glide.with(Activity.this).load(R.drawable.add).into(image1); 然后它工作正常

答案 1 :(得分:0)

这不是一个简单的错误吗?在这里你得到了“draw”Drawable,但是你没有做任何事情。也许替换:

if (source.equals((TableLayout) findViewById(R.id.tableLayout)))  
        {
            //imageOnBoard.setImageResource(strategyIcon.getId());
            Drawable draw = strategyIcon.getDrawable();
            imageOnBoard.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));

        }

通过:

if (source.equals((TableLayout) findViewById(R.id.tableLayout)))  
            {
                Drawable draw = strategyIcon.getDrawable();
                imageOnBoard.setImageDrawable(draw);

            }

答案 2 :(得分:0)

这个问题与getDrawable有关,它提供了旧的Drawable。 这是使用Tags:

修复的
if (source.equals((TableLayout) findViewById(R.id.tableLayout)))   //in case this icon is dragged from the board
        {


            Drawable draw = (Drawable) strategyIcon.getTag();
            imageOnBoard.setImageDrawable(draw);
            imageOnBoard.setTag(draw);

        }




        else                                                           //in case this icon is dragged from the menu
        {
            Drawable draw = strategyIcon.getDrawable();
            imageOnBoard.setImageDrawable(draw);

            imageOnBoard.invalidateDrawable(draw);
            imageOnBoard.setTag(draw);

        }

答案 3 :(得分:0)

我遇到了同样的问题,我的解决方案是在“setImageDrawable”之前设置图像的LayoutParams。显然,因为drawable没有尺寸(特别是,如果它是一个自定义可绘制或一些图标)在屏幕中显示为零长度的宽度和高度。 因此,解决方案是:

SemiCircleDrawable d = new SemiCircleDrawable(...); // just a custom drawable

myImage.setLayoutParams(newLinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
myImage.setImageDrawable(d);

答案 4 :(得分:-1)

因为在api级别16中不推荐使用setImageDrawable()方法,所以此方法不起作用。相反,您可以使用以下方法

@SuppressWarnings("deprecation")
    private void setRes(ImageView iv,Drawable drawable){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
            iv.setBackground(drawable);
        else
            iv.setBackgroundDrawable(drawable);
    }

参考#http://developer.android.com/reference/android/view/View.html#setBackground(android.graphics.drawable.Drawable)

相关问题