private void fillCanvas(Canvas canvas, DrawSettings drawSettings) {
   if (drawSettings.isNightMode()) {
     canvas.drawARGB(255, 100, 100, 100);
   } else {
     canvas.drawARGB(255, 225, 225, 225);
   }
 }
 @Override
 public Bitmap getDetailTexture(int n) {
   Bitmap bitmap =
       Bitmap.createBitmap(DETAIL_TEXTURE_WIDTH, DETAIL_TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
   Canvas canvas = new Canvas(bitmap);
   canvas.drawARGB(32, 10, 10, 10);
   mPaint.setTextSize(15.0f);
   mPaint.setAntiAlias(true);
   OpenPath mPath = mPathItems[n];
   if (mPath == null)
     canvas.drawText("Detail text for card " + n, 0, DETAIL_TEXTURE_HEIGHT / 2, mPaint);
   else {
     Path p = new Path();
     RectF bounds = new RectF();
     String s = mPath.getName();
     int y = (int) mPaint.getTextSize() + 2;
     while (s != "") {
       mPaint.getTextPath(s, 0, s.length(), 0, 0, p);
       p.computeBounds(bounds, true);
       float lines = Math.max(1, bounds.right / DETAIL_TEXTURE_WIDTH);
       int chars = (int) (s.length() / lines);
       canvas.drawText(s, 0, chars, 0, y, mPaint);
       if (chars >= s.length()) break;
       y += bounds.height() + 2;
       s = s.substring(chars).trim();
     }
   }
   return bitmap;
 }
  /**
   * Gets the cropped circle image based on the current crop selection.
   *
   * @return a new Circular Bitmap representing the cropped image
   */
  public Bitmap getCroppedOvalImage() {
    if (mBitmap != null) {
      Bitmap cropped = getCroppedImage();

      int width = cropped.getWidth();
      int height = cropped.getHeight();
      Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

      Canvas canvas = new Canvas(output);

      final int color = 0xff424242;
      final Paint paint = new Paint();

      paint.setAntiAlias(true);
      canvas.drawARGB(0, 0, 0, 0);
      paint.setColor(color);

      RectF rect = new RectF(0, 0, width, height);
      canvas.drawOval(rect, paint);
      paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
      canvas.drawBitmap(cropped, 0, 0, paint);

      return output;
    } else {
      return null;
    }
  }
    @Override
    protected void onDraw(Canvas canvas) {
      super.onDraw(canvas);

      canvas.drawARGB(255, 255, 255, 255);

      canvas.save();
      canvas.translate(120.0f, 50.0f);
      canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mColorMatrixPaint);

      canvas.translate(0.0f, 50.0f + mBitmap1.getHeight());
      canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mLightingPaint);

      canvas.translate(0.0f, 50.0f + mBitmap1.getHeight());
      canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mBlendPaint);
      canvas.restore();

      canvas.save();
      canvas.translate(120.0f + mBitmap1.getWidth() + 120.0f, 50.0f);
      canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, mColorMatrixPaint);

      canvas.translate(0.0f, 50.0f + mBitmap2.getHeight());
      canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, mLightingPaint);

      canvas.translate(0.0f, 50.0f + mBitmap2.getHeight());
      canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, mBlendPaint);
      canvas.restore();
    }
  private Bitmap addShadow(Bitmap bitmap, float cornerRadius) {
    Bitmap output =
        Bitmap.createBitmap(bitmap.getWidth() + 4, bitmap.getHeight() + 4, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    int shadowRadius = 2;
    final Rect imageRect =
        new Rect(shadowRadius, shadowRadius, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(imageRect);

    // This does not achieve the desired effect
    Paint shadowPaint = new Paint();
    shadowPaint.setAntiAlias(true);
    shadowPaint.setColor(Color.BLACK);
    shadowPaint.setShadowLayer((float) shadowRadius, 1.0f, 1.0f, Color.BLACK);
    canvas.drawRect(rectF, shadowPaint);

    canvas.drawARGB(0, 0, 0, 0);
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(color);

    canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, imageRect, imageRect, paint);

    return output;
  }
  public static Bitmap getCircleBitmap(Bitmap bitmap) {
    Bitmap output;

    if (bitmap.getWidth() > bitmap.getHeight()) {
      output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Config.ARGB_8888);
    } else {
      output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    float r = 0;

    if (bitmap.getWidth() > bitmap.getHeight()) {
      r = bitmap.getHeight() / 2;
    } else {
      r = bitmap.getWidth() / 2;
    }

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(r, r, r, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
  }
  public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, boolean square) {
    int width = 0;
    int height = 0;
    if (square) {
      if (bitmap.getWidth() < bitmap.getHeight()) {
        width = bitmap.getWidth();
        height = bitmap.getWidth();
      } else {
        width = bitmap.getHeight();
        height = bitmap.getHeight();
      }
    } else {
      height = bitmap.getHeight();
      width = bitmap.getWidth();
    }

    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, width, height);
    final RectF rectF = new RectF(rect);
    final float roundPx = 90;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
  }
  @SuppressLint("DrawAllocation")
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    final float radius =
        TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            CORNER_RADIUS,
            mContext.getResources().getDisplayMetrics());

    RectF rectF =
        new RectF(
            Edge.LEFT.getCoordinate(),
            Edge.TOP.getCoordinate(),
            Edge.RIGHT.getCoordinate(),
            Edge.BOTTOM.getCoordinate());
    float cx = (Edge.LEFT.getCoordinate() + Edge.RIGHT.getCoordinate()) / 2;
    float cy = (Edge.TOP.getCoordinate() + Edge.BOTTOM.getCoordinate()) / 2;
    float radius2 = (Edge.RIGHT.getCoordinate() - Edge.LEFT.getCoordinate()) / 2;

    Path clipPath = new Path();
    clipPath.addCircle(cx, cy, radius2, Path.Direction.CW);
    canvas.clipPath(clipPath, Region.Op.DIFFERENCE);
    canvas.drawARGB(204, 41, 48, 63);
    canvas.restore();
    canvas.drawCircle(cx, cy, radius2, mBorderPaint);
    // drawRuleOfThirdsGuidelines(canvas);
  }
  /**
   * 将图片转化为圆形头像 @Title: toRoundBitmap
   *
   * @throws
   */
  public static Bitmap toRoundBitmap(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float roundPx;
    float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
    if (width <= height) {
      roundPx = width / 2;

      left = 0;
      top = 0;
      right = width;
      bottom = width;

      height = width;

      dst_left = 0;
      dst_top = 0;
      dst_right = width;
      dst_bottom = width;
    } else {
      roundPx = height / 2;

      float clip = (width - height) / 2;

      left = clip;
      right = width - clip;
      top = 0;
      bottom = height;
      width = height;

      dst_left = 0;
      dst_top = 0;
      dst_right = height;
      dst_bottom = height;
    }

    Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
    final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
    final RectF rectF = new RectF(dst);

    paint.setAntiAlias(true); // 设置画笔无锯齿

    canvas.drawARGB(0, 0, 0, 0); // 填充整个Canvas

    // 以下有两种方法画圆,drawRounRect和drawCircle
    canvas.drawRoundRect(
        rectF, roundPx, roundPx, paint); // 画圆角矩形,第一个参数为图形显示区域,第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。
    // canvas.drawCircle(roundPx, roundPx, roundPx, paint);

    paint.setXfermode(
        new PorterDuffXfermode(
            Mode.SRC_IN)); // 设置两张图片相交时的模式,参考http://trylovecatch.iteye.com/blog/1189452
    canvas.drawBitmap(bitmap, src, dst, paint); // 以Mode.SRC_IN模式合并bitmap和已经draw了的Circle

    return output;
  }
Exemple #10
0
  /**
   * @param bmp
   * @param radius
   * @return
   */
  public static Bitmap getCroppedBitmap(Bitmap bmp, float radius) {
    Bitmap sbmp;
    int width = (int) radius;
    if (bmp.getWidth() != width || bmp.getHeight() != width) {
      sbmp = Bitmap.createScaledBitmap(bmp, width, width, false);
    } else {
      sbmp = bmp;
    }
    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(
        sbmp.getWidth() / 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);
    return output;
  }
Exemple #11
0
 public void onDraw(Canvas canvas)
 {
     canvas.drawARGB(255, 0, 0, 0);
     getDrawingRect(a);
     b.as.a(canvas, (a.width() - b.as.f) / 2, (a.height() - b.as.g) / 2);
     invalidate();
 }
  private void generateBitmap(float fraction) {
    mBitmap = Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(mBitmap);

    canvas.clipRect(0, (int) (64 * fraction), 64, 0);
    canvas.drawARGB(255, 255, 255, 255);
  }
  public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
    Bitmap sbmp;
    if (bmp.getWidth() != radius || bmp.getHeight() != radius)
      sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    else sbmp = bmp;
    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 255, 255, 255);
    paint.setColor(Color.parseColor("#7f97d2"));
    canvas.drawCircle(
        sbmp.getWidth() / 2 + 0.7f,
        sbmp.getHeight() / 2 + 0.7f,
        sbmp.getWidth() / 2 - strokeWidth,
        paint); // 2-9f
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);

    //        Paint pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);
    //	    pTouch.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    //	    pTouch.setAlpha(255);
    //	    pTouch.setColor(Color.TRANSPARENT);
    //	    pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));

    //	    canvas.drawCircle(sbmp.getWidth() / 2-.6f, sbmp.getHeight() / 2, ((float)
    // Math.ceil((sbmp.getHeight()+15)/11))+.2f, pTouch);
    //	    canvas.drawCircle(sbmp.getWidth() / 2-.6f, sbmp.getHeight() / 2, 57, pTouch);
    return output;
  }
  public static Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius) {
    Bitmap finalBitmap;
    if (bitmap.getWidth() != radius || bitmap.getHeight() != radius)
      finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, false);
    else finalBitmap = bitmap;
    Bitmap output =
        Bitmap.createBitmap(
            finalBitmap.getWidth(), finalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, finalBitmap.getWidth(), finalBitmap.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(
        finalBitmap.getWidth() / 2 + 0.7f,
        finalBitmap.getHeight() / 2 + 0.7f,
        finalBitmap.getWidth() / 2 + 0.1f,
        paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(finalBitmap, rect, rect, paint);

    return output;
  }
  public static Bitmap getRoundedBitmap(Bitmap src, int radius) {
    Bitmap roundBitmap;
    roundBitmap =
        (src.getWidth() == radius || src.getHeight() == radius)
            ? src
            : Bitmap.createScaledBitmap(src, radius, radius, false);
    Bitmap des =
        Bitmap.createBitmap(
            roundBitmap.getWidth(), roundBitmap.getHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(des);
    final Rect rect = new Rect(0, 0, des.getWidth(), des.getHeight());

    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);

    canvas.drawARGB(0, 0, 0, 0);

    paint.setColor(Color.parseColor("#BAB399"));

    canvas.drawCircle(
        des.getWidth() / 2 + 0.7f, des.getHeight() / 2 + 0.7f, des.getWidth() / 2 - 10f, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(roundBitmap, rect, rect, paint);

    return des;
  }
Exemple #16
0
  public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    // We have to make sure our rounded corners have an alpha channel in most cases
    // color is arbitrary. This could be any color that was fully opaque (alpha = 255)
    final int color = 0xff222222;

    // We're just reusing xferPaint to paint a normal looking rounded box,
    // the roundPx is the amount we're rounding by.
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, w, h);
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    // We're going to apply this paint using a porter-duff xfer mode.
    // This will allow us to only overwrite certain pixels.
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
  }
  public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
    Bitmap sbmp;

    if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
      float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
      float factor = smallest / radius;
      sbmp =
          Bitmap.createScaledBitmap(
              bmp, (int) (bmp.getWidth() / factor), (int) (bmp.getHeight() / factor), false);
    } else {
      sbmp = bmp;
    }

    Bitmap output = Bitmap.createBitmap(radius, radius, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffa19774;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, radius, radius);

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#FFFFFF"));
    canvas.drawCircle(radius / 2, radius / 2, radius / 2 + 0.1f, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);

    return output;
  }
Exemple #18
0
  public static Bitmap roundCorner(Bitmap src, float round) {

    // image size
    int width = src.getWidth();
    int height = src.getHeight();

    // create bitmap output
    Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);

    // set canvas for painting
    Canvas canvas = new Canvas(result);
    // draw white
    canvas.drawARGB(0, 0, 0, 0);

    // config paint
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    //		paint.setColor(Color.BLACK);

    // config rectangle for embedding
    final Rect srcRect = new Rect(0, 0, width, height);
    final Rect desRect = new Rect(0, 0, width, height);

    // draw rect to canvas
    paint.setColor(Color.RED);
    canvas.drawCircle(width / 2, height / 2, round, paint);

    // create Xfer mode
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    // draw source image to canvas
    canvas.drawBitmap(src, srcRect, desRect, paint);

    // return final image
    return result;
  }
  @Override
  public void onDraw(Canvas canvas) {
    canvas.drawARGB(255, 0, 0, 0);

    /// Probably not used.
    // invalidate();
  }
  @Override
  protected Bitmap transform(BitmapPool pool, Bitmap source, int outWidth, int outHeight) {
    int sourceWidth = source.getWidth();
    int sourceHeight = source.getHeight();
    double outRatio = (double) outWidth / (double) outHeight;
    int x = 0;
    int width = sourceWidth;
    int height = (int) (width / outRatio);
    int y = (sourceHeight - height) / 2;
    if (height >= sourceHeight) {
      height = sourceHeight;
      width = (int) (outRatio * height);
      y = 0;
      x = (sourceWidth - width) / 2;
    }
    Bitmap bitmap = Bitmap.createBitmap(source, x, y, width, height);

    // 圆角
    Bitmap output = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF dst = new RectF(0, 0, outWidth, outHeight);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(dst, mRadius, mRadius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, src, dst, paint);

    return output;
  }
Exemple #21
0
 @Override
 public void available(RenderingRequest request) {
   clearBitmap();
   mImage = request.getBitmap();
   /// M: [BUG.ADD] @{
   if (mIsAddVersionOperation && mImage != null) {
     mHasFinishAppliedFilterOperation = true;
   }
   /// @}
   if (mImage == null) {
     mImageFrame = null;
     return;
   }
   if (mRepresentation.getOverlayId() != 0 && mOverlayBitmap == null) {
     mOverlayBitmap =
         BitmapFactory.decodeResource(mContext.getResources(), mRepresentation.getOverlayId());
   }
   if (mOverlayBitmap != null) {
     if (getRepresentation().getFilterType() == FilterRepresentation.TYPE_BORDER) {
       Canvas canvas = new Canvas(mImage);
       canvas.drawBitmap(
           mOverlayBitmap,
           new Rect(0, 0, mOverlayBitmap.getWidth(), mOverlayBitmap.getHeight()),
           new Rect(0, 0, mImage.getWidth(), mImage.getHeight()),
           new Paint());
     } else {
       Canvas canvas = new Canvas(mImage);
       canvas.drawARGB(128, 0, 0, 0);
       drawCenteredImage(mOverlayBitmap, mImage, false);
     }
   }
   if (mAdapter != null) {
     mAdapter.notifyDataSetChanged();
   }
 }
  public static int createTextureWithTextContent(final String text) {
    // Create an empty, mutable bitmap
    final Bitmap bitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888);
    // get a canvas to paint over the bitmap
    final Canvas canvas = new Canvas(bitmap);
    canvas.drawARGB(0, 0, 255, 0);

    // get a background image from resources
    // note the image format must match the bitmap format
    //        Drawable background = context.getResources().getDrawable(R.drawable.background);
    //        background.setBounds(0, 0, 256, 256);
    //        background.draw(canvas); // draw the background to our bitmap

    // Draw the text
    final Paint textPaint = new Paint();
    textPaint.setTextSize(32);
    textPaint.setAntiAlias(true);
    textPaint.setARGB(0xff, 0xff, 0xff, 0xff);
    // draw the text centered
    canvas.drawText(text, 16, 112, textPaint);

    final int texture = createTextureId(GLES20.GL_TEXTURE_2D);

    // Alpha blending
    // GLES20.glEnable(GLES20.GL_BLEND);
    // GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);

    // Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    // Clean up
    bitmap.recycle();

    return texture;
  }
  // -------------------------------------------------------------------------------------------------------------------
  public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = 12;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    if (null != bitmap) {
      bitmap.recycle();
      bitmap = null;
    }

    return output;
  }
  /**
   * 获取裁剪后的圆形图片
   *
   * @param radius 半径
   */
  public Bitmap getCroppedRoundBitmap(Bitmap bmp, int radius) {
    Bitmap scaledSrcBmp;
    int diameter = radius * 2;

    // 为了防止宽高不相等,造成圆形图片变形,因此截取长方形中处于中间位置最大的正方形图片
    int bmpWidth = bmp.getWidth();
    int bmpHeight = bmp.getHeight();
    int squareWidth = 0, squareHeight = 0;
    int x = 0, y = 0;
    Bitmap squareBitmap;
    if (bmpHeight > bmpWidth) { // 高大于宽
      squareWidth = squareHeight = bmpWidth;
      x = 0;
      y = (bmpHeight - bmpWidth) / 2;
      // 截取正方形图片
      squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, squareHeight);
    } else if (bmpHeight < bmpWidth) { // 宽大于高
      squareWidth = squareHeight = bmpHeight;
      x = (bmpWidth - bmpHeight) / 2;
      y = 0;
      squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, squareHeight);
    } else {
      squareBitmap = bmp;
    }

    if (squareBitmap.getWidth() != diameter || squareBitmap.getHeight() != diameter) {
      scaledSrcBmp = Bitmap.createScaledBitmap(squareBitmap, diameter, diameter, true);

    } else {
      scaledSrcBmp = squareBitmap;
    }
    Bitmap output =
        Bitmap.createBitmap(scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawCircle(
        scaledSrcBmp.getWidth() / 2,
        scaledSrcBmp.getHeight() / 2,
        scaledSrcBmp.getWidth() / 2,
        paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(scaledSrcBmp, rect, rect, paint);
    // bitmap回收(recycle导致在布局文件XML看不到效果)
    // bmp.recycle();
    // squareBitmap.recycle();
    // scaledSrcBmp.recycle();
    bmp = null;
    squareBitmap = null;
    scaledSrcBmp = null;
    return output;
  }
  private void initialize() {
    rescale(getWidth(), getHeight());
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);

    // draws first mandelbrot
    mCanvas.drawARGB(0xff, 10, 10, 10);

    setOnTouchListener(this);
  }
Exemple #26
0
  public void setWindowSize(int width, int height) {
    windowWidth = width;
    windowHeight = height;
    windowCaptionFrameCountdown = windowCaptionDelayFrames;

    setWindowScale();
    controls.resizeToScreen(surfaceWidth, surfaceHeight);

    canvas.drawARGB(255, 0, 0, 0);
  }
    @Override
    protected void onDraw(Canvas c) {
      View activitedDrawer = null;
      if (mLeftDrawer != null && mLeftDrawer.getRight() > 0) {
        activitedDrawer = mLeftDrawer;
      } else if (mRightDrawer != null
          && mRightDrawer.getLeft() < SlidingDrawerLayout.this.getWidth()) {
        activitedDrawer = mRightDrawer;
      }
      if (activitedDrawer == null) {
        return;
      }

      int paddingTop = 0;
      int paddingBottom = 0;
      if (activitedDrawer instanceof DrawerLayoutChild) {
        DrawerLayoutChild dlc = (DrawerLayoutChild) activitedDrawer;
        paddingTop = dlc.getLayoutPaddingTop();
        paddingBottom = dlc.getLayoutPaddingBottom();
      }

      int width = getWidth();
      int height = getHeight();

      int saved = -1;
      if (paddingTop != 0 || paddingBottom != 0) {
        saved = c.save();
        c.clipRect(0, paddingTop, width, height - paddingBottom);
      }

      // Draw drak background
      c.drawARGB(MathUtils.lerp(FORM, TO, mPercent), 0, 0, 0);

      if (activitedDrawer == mLeftDrawer) {
        if (mShadowLeft != null) {
          int right = mLeftDrawer.getRight();
          final int shadowWidth = mShadowLeft.getIntrinsicWidth();
          mShadowLeft.setBounds(right, 0, right + shadowWidth, getHeight());
          mShadowLeft.setAlpha((int) (0xff * mLeftPercent));
          mShadowLeft.draw(c);
        }
      } else if (activitedDrawer == mRightDrawer) {
        if (mShadowRight != null) {
          int left = mRightDrawer.getLeft();
          final int shadowWidth = mShadowRight.getIntrinsicWidth();
          mShadowRight.setBounds(left - shadowWidth, 0, left, getHeight());
          mShadowRight.setAlpha((int) (0xff * mRightPercent));
          mShadowRight.draw(c);
        }
      }

      if (saved != -1) {
        c.restoreToCount(saved);
      }
    }
  /**
   * ��ͼƬ��ΪԲ��ͼƬ
   *
   * @param bitmap
   * @return
   */
  public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

    output = null;
    if (bitmap != null) {

      int width = bitmap.getWidth();
      int height = bitmap.getHeight();
      float roundPx;
      float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
      if (width <= height) {
        roundPx = width / 2;
        top = 0;
        bottom = width;
        left = 0;
        right = width;
        height = width;
        dst_left = 0;
        dst_top = 0;
        dst_right = width;
        dst_bottom = width;
      } else {
        roundPx = height / 2;
        float clip = (width - height) / 2;
        left = clip;
        right = width - clip;
        top = 0;
        bottom = height;
        width = height;
        dst_left = 0;
        dst_top = 0;
        dst_right = height;
        dst_bottom = height;
      }
      output = Bitmap.createBitmap(width, height, android.graphics.Bitmap.Config.ARGB_8888);
      Canvas canvas = new Canvas(output);
      canvas.setDrawFilter(
          new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
      final int color = 0xff424242;
      final Paint paint = new Paint();
      final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
      final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
      final RectF rectF = new RectF(dst);
      paint.setAntiAlias(true);
      canvas.drawARGB(0, 0, 0, 0);
      paint.setColor(color);
      canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
      paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
      canvas.drawBitmap(bitmap, src, dst, paint);

      bitmap = output;
      return bitmap;
    }
    output = null;
    return null;
  }
  public static Bitmap padBitmap(Bitmap input, int paddingX, int paddingY) {
    Bitmap outputBitmap =
        Bitmap.createBitmap(
            input.getWidth() + 2 * paddingX,
            input.getHeight() + paddingY * 2,
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outputBitmap);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawBitmap(input, paddingX, paddingY, null);

    return outputBitmap;
  }
    @Override
    protected void onDraw(Canvas canvas) {
      canvas.drawARGB(80, 102, 204, 255);

      canvas.drawPicture(picture);

      canvas.translate(300, 0);
      canvas.drawPicture(picture, rect);

      canvas.translate(0, 300);
      canvas.drawPicture(picture, rect1);
    }