当前位置:操作系统 > 安卓/Android >>

Android图片操作(Bitmap)

[java]  /**
     * 将多个Bitmap合并成一个图片。
     * 
     * @param int 将多个图合成多少列
     * @param Bitmap... 要合成的图片
     * @return
     */ 
public static Bitmap combineBitmaps(int columns, Bitmap... bitmaps) { 
        if (columns <= 0 || bitmaps == null || bitmaps.length == 0) { 
            throw new IllegalArgumentException("Wrong parameters: columns must > 0 and bitmaps.length must > 0."); 
        } 
        int maxWidthPerImage = 0; 
        int maxHeightPerImage = 0; 
        for (Bitmap b : bitmaps) { 
            maxWidthPerImage = maxWidthPerImage > b.getWidth() ? maxWidthPerImage : b.getWidth(); 
            maxHeightPerImage = maxHeightPerImage > b.getHeight() ? maxHeightPerImage : b.getHeight(); 
        } 
        int rows = 0; 
        if (columns >= bitmaps.length) { 
            rows = 1; 
            columns = bitmaps.length; 
        } else { 
            rows = bitmaps.length % columns == 0 ? bitmaps.length / columns : bitmaps.length / columns + 1; 
        } 
        Bitmap newBitmap = Bitmap.createBitmap(columns * maxWidthPerImage, rows * maxHeightPerImage, Config.RGB_565); 
 
        for (int x = 0; x < rows; x++) { 
            for (int y = 0; y < columns; y++) { 
                int index = x * columns + y; 
                if (index >= bitmaps.length) 
                    break; 
                newBitmap = mixtureBitmap(newBitmap, bitmaps[index], new PointF(y * maxWidthPerImage, x * maxHeightPerImage)); 
            } 
        } 
        return newBitmap; 
    } 
/**
  * 将多个Bitmap合并成一个图片。
  *
  * @param int 将多个图合成多少列
  * @param Bitmap... 要合成的图片
  * @return
  */
public static Bitmap combineBitmaps(int columns, Bitmap... bitmaps) {
  if (columns <= 0 || bitmaps == null || bitmaps.length == 0) {
   throw new IllegalArgumentException("Wrong parameters: columns must > 0 and bitmaps.length must > 0.");
  }
  int maxWidthPerImage = 0;
  int maxHeightPerImage = 0;
  for (Bitmap b : bitmaps) {
   maxWidthPerImage = maxWidthPerImage > b.getWidth() ? maxWidthPerImage : b.getWidth();
   maxHeightPerImage = maxHeightPerImage > b.getHeight() ? maxHeightPerImage : b.getHeight();
  }
  int rows = 0;
  if (columns >= bitmaps.length) {
   rows = 1;
   columns = bitmaps.length;
  } else {
   rows = bitmaps.length % columns == 0 ? bitmaps.length / columns : bitmaps.length / columns + 1;
  }
  Bitmap newBitmap = Bitmap.createBitmap(columns * maxWidthPerImage, rows * maxHeightPerImage, Config.RGB_565);

  for (int x = 0; x < rows; x++) {
   for (int y = 0; y < columns; y++) {
    int index = x * columns + y;
    if (index >= bitmaps.length)
     break;
    newBitmap = mixtureBitmap(newBitmap, bitmaps[index], new PointF(y * maxWidthPerImage, x * maxHeightPerImage));
   }
  }
  return newBitmap;
 }


[java] /**
     * Mix two Bitmap as one.
     * 
     * @param bitmapOne
     * @param bitmapTwo
     * @param point
     *            where the second bitmap is painted.
     * @return
     */ 
    public static Bitmap mixtureBitmap(Bitmap first, Bitmap second, PointF fromPoint) { 
        if (first == null || second == null || fromPoint == null) { 
            return null; 
        } 
        Bitmap newBitmap = Bitmap.createBitmap(first.getWidth(), first.getHeight(), Config.ARGB_4444); 
        Canvas cv = new Canvas(newBitmap); 
        cv.drawBitmap(first, 0, 0, null); 
        cv.drawBitmap(second, fromPoint.x, fromPoint.y, null); 
        cv.save(Canvas.ALL_SAVE_FLAG); 
        cv.restore(); 
        return newBitmap; 
    } 
/**
  * Mix two Bitmap as one.
  *
  * @param bitmapOne
  * @param bitmapTwo
  * @param point
  *            where the second bitmap is painted.
  * @return
  */
 public static Bitmap mixtureBitmap(Bitmap first, Bitmap second, PointF fromPoint) {
  if (first == null || second == null || fromPoint == null) {
   return null;
  }
  Bitmap newBitmap = Bitmap.createBitmap(first.getWidth(), first.getHeight(), Config.ARGB_4444);
  Canvas cv = new Canvas(newBitmap);
  cv.drawBitmap(first, 0, 0, null);
  cv.drawBitmap(second, fromPoint.x, fromPoint.y, null);
  cv.save(Canvas.ALL_SAVE_FLAG);
  cv.restore();
  return newBitmap;
 }


[java] //截屏  
public static Bitmap getScreenshotsForCurrentWindow(Activity activity) { 
        View cv = activity.getWindow().getDecorView(); 
    &n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,