2014-11-14 57 views
0

我有一個問題想實現特定的混合情況:使用多個porterduffmodes與位圖

  • 創建帆布
  • 繪製原始位
  • 繪製矩形的顏色和繁殖模式
  • 繪製另一個矩形與顏色和減光模式

以下代碼正在與iOS合作:

+ (UIImage *)compositeBackgroundImageFromImage:(UIImage *)image 
{ 
if (image.size.width < 1 || image.size.height < 1) 
    return nil; 

UIColor *multiplyColor = [UIColor blueColor]; 
UIColor *lightenColor = [UIColor cyanColor]; 

UIGraphicsBeginImageContextWithOptions(image.size, YES, [UIScreen mainScreen].scale); 
CGContextRef c = UIGraphicsGetCurrentContext(); 
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height); 

[image drawAtPoint:CGPointZero]; 

CGContextSetFillColorWithColor(c, multiplyColor.CGColor); 
CGContextSetBlendMode(c, kCGBlendModeMultiply); 
CGContextFillRect(c, rect); 

CGContextSetFillColorWithColor(c, lightenColor.CGColor); 
CGContextSetBlendMode(c, kCGBlendModeLighten); 
CGContextFillRect(c, rect); 

UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
return result; 

}

,這是對應的Java與Android通用圖像裝載機定製BitmapDisplayer

.displayer(new BitmapDisplayer() { 

    @Override 
    public void display(Bitmap bitmap, ImageAware imageAware, 
      LoadedFrom loadedFrom) { 
     int w = bitmap.getWidth(); 
     int h = bitmap.getHeight(); 

     if (w > 0 && h > 0) { 
      int multiplyColor = // color here 
      int lightenColor = // color here 
      Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); 
      p.setFilterBitmap(true); 

      Bitmap bm = Bitmap.createBitmap(w, h, 
        Config.ARGB_8888); 
      Canvas c = new Canvas(bm); 
      c.drawBitmap(bitmap, 0, 0, p); 

      p.setColorFilter(new PorterDuffColorFilter(
        multiplyColor, Mode.MULTIPLY)); 
      c.drawRect(0, 0, w, h, p); 

      p.setColorFilter(new PorterDuffColorFilter(
        lightenColor, Mode.LIGHTEN)); 
      c.drawRect(0, 0, w, h, p); 

      ((ImageView) imageAware.getWrappedView()) 
        .setImageBitmap(bm); 
     } 
    } 

結果僅僅是一個爛攤子遠離的iOS。

謝謝。

回答

0

經過幾次不同的嘗試後,最終使用着色器繪製3個矩形。如果有人知道這是否與iOS代碼完全相同,那麼視覺效果看起來非常接近。

int w = bitmap.getWidth(); 
int h = bitmap.getHeight(); 

if (w > 0 && h > 0) { 
    int multiplyColor = // get color 
    int lightenColor = // get color 

    Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); 
    BitmapShader shader = new BitmapShader(bitmap, 
      TileMode.CLAMP, TileMode.CLAMP); 
    p.setShader(shader); 

    Bitmap bm = Bitmap.createBitmap(w, h, 
      Config.ARGB_8888); 
    Canvas c = new Canvas(bm); 
    c.drawRect(0, 0, w, h, p); 

    p.setColorFilter(new PorterDuffColorFilter(
      multiplyColor, Mode.MULTIPLY)); 
    c.drawRect(0, 0, w, h, p); 

    p.setColorFilter(new PorterDuffColorFilter(
      lightenColor, Mode.LIGHTEN)); 
    c.drawRect(0, 0, w, h, p); 

    ((ImageView) imageAware.getWrappedView()) 
      .setImageBitmap(bm); 
}