2013-12-17 9 views
5

Posso mascherare le immagini in griglia, ma il problema è come posso tagliare o ritagliare il contenuto della maschera dell'immagine nel mascheramento. aiutarmi per qualche idea e qualche demo ..come posso tagliare o ritagliare l'immagine del contenuto?

public void makeMaskImage(ImageView mImageView, Bitmap mBitmap, 
     int MASK_IMAGEVIEW, int IMAGEVIEW_THUMB_BACKGROUND) { 
    try { 
     Bitmap mask = BitmapFactory.decodeResource(mContext.getResources(),MASK_IMAGEVIEW); 
     Bitmap result = Bitmap.createBitmap(mask.getWidth(), 
       mask.getHeight(), Config.ARGB_8888); 
     Canvas mCanvas = new Canvas(result); 
     Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); 

     if (mBitmap == null) { 
      mBitmap = BitmapFactory.decodeResource(mContext.getResources(), 
        R.drawable.no_image_friend); 
      mImageView.setImageBitmap(mBitmap); 
      return; 
     } 

     mCanvas.drawBitmap(mBitmap, 0, 0, null); 
     mCanvas.drawBitmap(mask, 0, 0, paint); 
     paint.setXfermode(null); 
     try { 
      mImageView .setImageBitmap(Bitmap_process(getImageBuffer(result))); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     mImageView.setScaleType(ScaleType.CENTER); 
     mImageView.setBackgroundResource(IMAGEVIEW_THUMB_BACKGROUND); 
    } catch (OutOfMemoryError e) { 
     e.printStackTrace(); 
    } 
} 

risposta

0

Hi seguito è può essere di qualche aiuto per voi

  1. bisogno di un'immagine in forma

    finale lungo startTime = SystemClock.uptimeMillis() ;

    // Part 1: Decode image 
        Bitmap unscaledBitmap = ScalingUtilities.decodeResource(getResources(), mSourceId, 
          mDstWidth, mDstHeight, ScalingLogic.FIT); 
    
        // Part 2: Scale image 
        Bitmap scaledBitmap = ScalingUtilities.createScaledBitmap(unscaledBitmap, mDstWidth, 
          mDstHeight, ScalingLogic.FIT); 
        unscaledBitmap.recycle(); 
    
        // Calculate memory usage and performance statistics 
        final int memUsageKb = (unscaledBitmap.getRowBytes() * unscaledBitmap.getHeight())/1024; 
        final long stopTime = SystemClock.uptimeMillis(); 
    
        // Publish results 
        mResultView.setText("Time taken: " + (stopTime - startTime) 
          + " ms. Memory used for scaling: " + memUsageKb + " kb."); 
        mImageView.setImageBitmap(scaledBitmap); 
    

successivo come ritagliare l'immagine

final long startTime = SystemClock.uptimeMillis(); 

      // Part 1: Decode image 
      Bitmap unscaledBitmap = ScalingUtilities.decodeResource(getResources(), mSourceId, 
        mDstWidth, mDstHeight, ScalingLogic.CROP); 

      // Part 2: Scale image 
      Bitmap scaledBitmap = ScalingUtilities.createScaledBitmap(unscaledBitmap, mDstWidth, 
        mDstHeight, ScalingLogic.CROP); 
      unscaledBitmap.recycle(); 

      // Calculate memory usage and performance statistics 
      final int memUsageKb = (unscaledBitmap.getRowBytes() * unscaledBitmap.getHeight())/1024; 
      final long stopTime = SystemClock.uptimeMillis(); 

      // Publish results 
      mResultView.setText("Time taken: " + (stopTime - startTime) 
        + " ms. Memory used for scaling: " + memUsageKb + " kb."); 
      mImageView.setImageBitmap(scaledBitmap); 

funzione delle risorse di decodifica

public static Bitmap decodeResource(Resources res, int resId, int dstWidth, int dstHeight, 
      ScalingLogic scalingLogic) { 
     Options options = new Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeResource(res, resId, options); 
     options.inJustDecodeBounds = false; 
     options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth, 
       dstHeight, scalingLogic); 
     Bitmap unscaledBitmap = BitmapFactory.decodeResource(res, resId, options); 

     return unscaledBitmap; 
    } 

funzione calculateSampleSize

public static int calculateSampleSize(int srcWidth, int srcHeight, int dstWidth, int dstHeight, 
      ScalingLogic scalingLogic) { 
     if (scalingLogic == ScalingLogic.FIT) { 
      final float srcAspect = (float)srcWidth/(float)srcHeight; 
      final float dstAspect = (float)dstWidth/(float)dstHeight; 

      if (srcAspect > dstAspect) { 
       return srcWidth/dstWidth; 
      } else { 
       return srcHeight/dstHeight; 
      } 
     } else { 
      final float srcAspect = (float)srcWidth/(float)srcHeight; 
      final float dstAspect = (float)dstWidth/(float)dstHeight; 

      if (srcAspect > dstAspect) { 
       return srcHeight/dstHeight; 
      } else { 
       return srcWidth/dstWidth; 
      } 
     } 
    } 

funzione createScaledBitmap

public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight, 
      ScalingLogic scalingLogic) { 
     Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), 
       dstWidth, dstHeight, scalingLogic); 
     Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), 
       dstWidth, dstHeight, scalingLogic); 
     Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(), 
       Config.ARGB_8888); 
     Canvas canvas = new Canvas(scaledBitmap); 
     canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG)); 

     return scaledBitmap; 
    } 

funzione ScalingLogic

public static enum ScalingLogic { 
     CROP, FIT 
    } 

funzione calculateDstRect

public static Rect calculateDstRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight, 
      ScalingLogic scalingLogic) { 
     if (scalingLogic == ScalingLogic.FIT) { 
      final float srcAspect = (float)srcWidth/(float)srcHeight; 
      final float dstAspect = (float)dstWidth/(float)dstHeight; 

      if (srcAspect > dstAspect) { 
       return new Rect(0, 0, dstWidth, (int)(dstWidth/srcAspect)); 
      } else { 
       return new Rect(0, 0, (int)(dstHeight * srcAspect), dstHeight); 
      } 
     } else { 
      return new Rect(0, 0, dstWidth, dstHeight); 
     } 
    } 
Problemi correlati