2014-10-18 23 views
1

我將Android應用程序從2.3(10)的目標版本升級到4.4(19),但Canvas Path繪製不能以相同的方式工作 - 而且我找不到任何文件說明發生了什麼變化。Android升級2.3 - > 4.4:Canvas路徑繪製不工作/已更改

我它關於灰色半透明覆蓋 - 它應該是像第一圖像(目標的Android 2.3)

目標:機器人2.3(10)
Target: Android 2.3 (10)

目標: Android 4.4(19)
Target: Android 4.4 (19)

這是代碼:

public class ProgramItemBackgroundDrawable extends Drawable{ 

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
private int height; 
private int width; 

private int topViewHeight; 
private boolean showBorder; 

public ProgramItemBackgroundDrawable(){ 
    paint.setStyle(Paint.Style.FILL_AND_STROKE); 
    paint.setStrokeWidth(1); 

    ... 
} 

public void draw(Canvas canvas) { 
    int cornerCutSize = convertDpToPixels(20, context); 

    int smallCornerTopMargin = convertDpToPixels(4, context); 
    if (cornerCutSize > height) 
     cornerCutSize = height - smallCornerTopMargin; 

    drawBaseBackgroundColors(canvas, cornerCutSize); 

    int margin = convertDpToPixels(3, context); 
    paint.setColor(Color.BLACK); 
    paint.setAlpha((int) (255 * 0.12)); 
    Path path = new Path(); 

    if(showBorder){ 
     //Marks bottom part of item with space around for the border 
     path.moveTo(margin, margin + topViewHeight); 
     path.lineTo(margin, height - margin); 
     path.lineTo(width - cornerCutSize, height - margin); 
     path.lineTo(width - margin, height - cornerCutSize); 
     path.lineTo(width - margin, margin + topViewHeight); 
     path.close(); 

     //Marks bottom corner 
     path.moveTo(width - cornerCutSize, height); 
     path.lineTo(width, height - cornerCutSize); 
     path.lineTo(width, height); 
     path.close(); 
    } else { 
     //Marks bottom part of item 
     path.moveTo(0, margin + topViewHeight); 
     path.lineTo(0, height); 
     path.lineTo(width, height); 
     path.lineTo(width, margin + topViewHeight); 
     path.close(); 

     //Marks bottom corner if it overlaps with the top view area 
     if(height < (topViewHeight + margin + cornerCutSize)) { 
      int smallCornerCutSize = Math.abs((height - cornerCutSize) - topViewHeight - margin); 

      path.moveTo(width - smallCornerCutSize, margin + topViewHeight); 
      path.lineTo(width+1, margin + topViewHeight - smallCornerCutSize); 
      path.lineTo(width+1, margin + topViewHeight); 
      path.close(); 
     } 
    } 

    path.setFillType(Path.FillType.INVERSE_WINDING); 
    canvas.drawPath(path, paint); 
} 

private void drawBaseBackgroundColors(Canvas canvas, int cornerCutSize) { 
    ... 
} 

public static int convertDpToPixels(float dp, Context context){ 
    Resources resources = context.getResources(); 
    DisplayMetrics metrics = resources.getDisplayMetrics(); 
    float px = dp * (metrics.densityDpi/160f); 
    return (int) px; 
} 
} 

任何想法什麼改變了android處理繪圖的方式?

更新1:
我認爲它可能有一些做的Hardware Acceleration changes in Api level 14

回答

3

This fixes it:

containingView.setLayerType(View.LAYER_TYPE_SOFTWARE, null) 

我禁用硬件加速。
但是這種解決方案沒有一些缺點嗎?
我如何才能使用硬件加速工作?