2017-04-17 67 views
0

我有一個這樣的代碼:應用崩潰調用BitmapDrawable.draw(帆布帆布)時

@Override 
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) { 
    drawGradient(canvas, parent); 
    drawStages(canvas, parent); 
} 

private void drawStages(final Canvas canvas, RecyclerView parent) { 
    final int parentLeft = parent.getPaddingLeft(); 
    final int parentRight = parentLeft + mLeftPadding + mIconSize + mRightPadding; 

    int childCount = parent.getChildCount(); 
    for (int i = 0; i < childCount - 1; i++) { 
     View child = parent.getChildAt(i); 

     LineupLayoutManager.LayoutParams params = 
       (LineupLayoutManager.LayoutParams) child.getLayoutParams(); 

     final int parentTop = child.getTop() + params.topMargin; 
     final int parentBottom = child.getBottom() + params.bottomMargin; 
     int height = parentBottom - parentTop; 

     final int paddingVertical = Math.round((height - mIconSize)/2); 

     try { 
      String link = findPlaceById(params.getPlaceId(),mStageList).getImg(); 
      Picasso.with(context).load(link).resize(150,150).into(new Target() { 
       @Override 
       public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
        final Drawable drawable = new BitmapDrawable(); 
        drawable.setBounds(parentLeft + mLeftPadding, parentTop + paddingVertical, 
          parentRight - mRightPadding, parentBottom - paddingVertical); 
        Canvas canvas1 = new Canvas(bitmap); 
        drawable.draw(canvas1); 
       } 

       @Override 
       public void onBitmapFailed(Drawable errorDrawable) { 

       } 

       @Override 
       public void onPrepareLoad(Drawable placeHolderDrawable) { 

       } 
      }); 
     } catch (IndexOutOfBoundsException e) { 
      Log.e(LOG_TAG, Log.getStackTraceString(e)); 
     } 
    } 
} 

整個代碼是從RecyclerView.ItemDecoration 擴展類的問題是內部,當我執行它,應用崩潰與此錯誤:

Fatal signal 11 (SIGSEGV), code 1, fault addr 0x58 in tid 17542

我也試圖把bitmap.draw到runOnUIThread()但沒有成功。

了一些研究之後,我發現,畢加索以某種方式與畫布干擾。這意味着如果我把這些東西放在畢加索方法之外,一切正常。

+0

發佈您的完整logcat輸出。 – csmckelvey

+0

事情是沒有logcat輸出,我會在這裏發佈它。只有你看到的錯誤 – Traabefi

+0

[Android致命信號11(SIGSEGV)在0x636f7d89(代碼= 1)可能有重複。它如何能夠被跟蹤了嗎?](http://stackoverflow.com/questions/17840521/android-fatal-signal-11-sigsegv-at-0x636f7d89-code-1-how-can-it-be-tracked) – csmckelvey

回答

0

我只是假設的權利,但好像你存儲一個參考canvas,有時更早。當您收到Bitmap時,此Canvas可能已經以某種方式失效。

代替您的實施,請存儲對Drawable的引用,並請求重繪您的View。然後在onDraw()內畫出DrawableCanvas作爲參數提供。

嘗試根據您的喜好調整以下實現。

BitmapDrawable drawable = null; 

private void drawStages(Canvas canvas, RecyclerView parent) { 
    if (drawable != null) { 
     int parentLeft = parent.getPaddingLeft(); 
     int parentRight = parentLeft + mLeftPadding + mIconSize + mRightPadding; 

     int childCount = parent.getChildCount(); 
     for (int i = 0; i < childCount - 1; i++) { 
      View child = parent.getChildAt(i); 

      LineupLayoutManager.LayoutParams params = 
        (LineupLayoutManager.LayoutParams) child.getLayoutParams(); 

      int parentTop = child.getTop() + params.topMargin; 
      int parentBottom = child.getBottom() + params.bottomMargin; 
      int height = parentBottom - parentTop; 

      int paddingVertical = Math.round((height - mIconSize)/2); 

      drawable.setBounds(parentLeft + mLeftPadding, parentTop + paddingVertical, 
        parentRight - mRightPadding, parentBottom - paddingVertical); 
      drawable.draw(canvas); 
    } else try { 
     String link = findPlaceById(params.getPlaceId(),mStageList).getImg(); 
     Picasso.with(context).load(link).resize(150,150).into(new Target() { 
      @Override 
      public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
       drawable = new BitmapDrawable(); 
       parent.invalidate() 
      } 

      @Override 
      public void onBitmapFailed(Drawable errorDrawable) { 
      } 

      @Override 
      public void onPrepareLoad(Drawable placeHolderDrawable) { 
      } 
     }); 
    } catch (IndexOutOfBoundsException e) { 
     Log.e(LOG_TAG, Log.getStackTraceString(e)); 
    } 
} 

編輯:新增工作代碼。

private void drawStages(Canvas canvas, final RecyclerView parent) { 
    final int parentLeft = parent.getPaddingLeft(); 
    final int parentRight = parentLeft + mLeftPadding + mIconSize + mRightPadding; 

    int childCount = parent.getChildCount(); 
    for (int i = 0; i < childCount - 1; i++) { 
     View child = parent.getChildAt(i); 

     LineupLayoutManager.LayoutParams params = 
       (LineupLayoutManager.LayoutParams) child.getLayoutParams(); 

     final int parentTop = child.getTop() + params.topMargin; 
     final int parentBottom = child.getBottom() + params.bottomMargin; 
     int height = parentBottom - parentTop; 

     final int paddingVertical = Math.round((height - mIconSize)/2); 
     BitmapDrawable drawable; 
     if(drawables.size()!=0)drawable = drawables.get(i); 
     else drawable = null; 
     if(drawable!=null){ 
      drawable.setBounds(parentLeft + mLeftPadding, parentTop + paddingVertical, 
        parentRight - mRightPadding, parentBottom - paddingVertical); 
      drawable.draw(canvas); 
     } else { 
      String link = findPlaceById(params.getPlaceId(),mStageList).getImg(); 
      Picasso.with(context).load(link).resize(150,150).into(new Target() { 
       @Override 
       public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
        BitmapDrawable d = new BitmapDrawable(context.getResources(),bitmap); 
        drawables.add(d); 
        parent.invalidateItemDecorations(); 
       } 

       @Override 
       public void onBitmapFailed(Drawable errorDrawable) { 
       } 

       @Override 
       public void onPrepareLoad(Drawable placeHolderDrawable) { 
       } 
      }); 
     } 
    } 
} 
+0

你能說明一下嗎?我將用一些較大的片段編輯代碼 – Traabefi

+0

這不會畫任何東西,它只會使視圖無效? – Traabefi

+0

但是'drawStages()'會再次被調用,if子句將被激活。 – tynn