예제 #1
0
  /**
   * 功能简述: 功能详细描述: 注意:
   *
   * @param drawable
   * @param wScale
   * @param hScale
   * @param res
   * @return
   */
  public static BitmapDrawable zoomDrawable(
      Drawable drawable, float wScale, float hScale, Resources res) {
    if (drawable != null) {
      int width = drawable.getIntrinsicWidth();
      int height = drawable.getIntrinsicHeight();
      Bitmap oldbmp = null;
      // drawable 转换成 bitmap
      if (drawable instanceof BitmapDrawable) {
        // 如果传入的drawable是BitmapDrawable,就不必要生成新的bitmap
        oldbmp = ((BitmapDrawable) drawable).getBitmap();
      } else {
        oldbmp = Utilities.createBitmapFromDrawable(drawable);
      }

      Matrix matrix = new Matrix(); // 创建操作图片用的 Matrix 对象
      matrix.postScale(wScale, hScale); // 设置缩放比例

      // 建立新的bitmap,其内容是对原bitmap的缩放后的图
      Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);
      matrix = null;

      // 把 bitmap 转换成 drawable 并返回
      return new BitmapDrawable(res, newbmp);
    }
    return null;
  }
예제 #2
0
  /**
   * 功能简述:对指定drawable进行指定的高宽缩放后,创建一张新的BitmapDrawable。 功能详细描述: 注意:
   *
   * @param context
   * @param drawable:待处理的drawable
   * @param w:期望缩放后的BitmapDrawable的宽
   * @param h:期望缩放后的BitmapDrawable的高
   * @return 经缩放处理后的新的BitmapDrawable
   */
  public static BitmapDrawable zoomDrawable(Context context, Drawable drawable, int w, int h) {
    if (drawable != null) {
      int width = drawable.getIntrinsicWidth();
      int height = drawable.getIntrinsicHeight();
      Bitmap oldbmp = null;
      // drawable 转换成 bitmap
      if (drawable instanceof BitmapDrawable) {
        // 如果传入的drawable是BitmapDrawable,就不必要生成新的bitmap
        oldbmp = ((BitmapDrawable) drawable).getBitmap();
      } else {
        oldbmp = Utilities.createBitmapFromDrawable(drawable);
      }

      Matrix matrix = new Matrix(); // 创建操作图片用的 Matrix 对象
      float scaleWidth = (float) w / width; // 计算缩放比例
      float scaleHeight = (float) h / height;
      matrix.postScale(scaleWidth, scaleHeight); // 设置缩放比例

      // 建立新的bitmap,其内容是对原bitmap的缩放后的图
      Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);
      matrix = null;

      // 把bitmap转换成drawable并返回
      return new BitmapDrawable(context.getResources(), newbmp);
    }
    return null;
  }