예제 #1
0
  /**
   * @param x 图像的宽度
   * @param y 图像的高度
   * @param image 源图片
   * @param outerRadiusRat 圆角的大小
   * @return 圆角图片
   */
  public static Bitmap createFramedPhoto(int x, int y, Bitmap image, float outerRadiusRat) {
    // 根据源文件新建一个darwable对象
    Drawable imageDrawable = new BitmapDrawable(image);

    // 新建一个新的输出图片
    Bitmap output = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    // 新建一个矩形
    RectF outerRect = new RectF(0, 0, x, y);

    // 产生一个红色的圆角矩形
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);
    canvas.drawRoundRect(outerRect, outerRadiusRat, outerRadiusRat, paint);

    // 将源图片绘制到这个圆角矩形上
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    imageDrawable.setBounds(0, 0, x, y);
    canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
    imageDrawable.draw(canvas);
    canvas.restore();

    return output;
  }
예제 #2
0
 protected void onDraw(Canvas paramCanvas) {
   Drawable localDrawable = getDrawable();
   if (localDrawable == null) return;
   try {
     if (this.paint == null) {
       Paint localPaint1 = new Paint();
       this.paint = localPaint1;
       this.paint.setFilterBitmap(false);
       Paint localPaint2 = this.paint;
       Xfermode localXfermode1 = MASK_XFERMODE;
       @SuppressWarnings("unused")
       Xfermode localXfermode2 = localPaint2.setXfermode(localXfermode1);
     }
     float f1 = getWidth();
     float f2 = getHeight();
     int i = paramCanvas.saveLayer(0.0F, 0.0F, f1, f2, null, 31);
     int j = getWidth();
     int k = getHeight();
     localDrawable.setBounds(0, 0, j, k);
     localDrawable.draw(paramCanvas);
     if ((this.mask == null) || (this.mask.isRecycled())) {
       Bitmap localBitmap1 = createMask();
       this.mask = localBitmap1;
     }
     Bitmap localBitmap2 = this.mask;
     Paint localPaint3 = this.paint;
     paramCanvas.drawBitmap(localBitmap2, 0.0F, 0.0F, localPaint3);
     paramCanvas.restoreToCount(i);
     return;
   } catch (Exception localException) {
     StringBuilder localStringBuilder =
         new StringBuilder().append("Attempting to draw with recycled bitmap. View ID = ");
     System.out.println("localStringBuilder==" + localStringBuilder);
   }
 }
  @Override
  protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    if (mDeltX > 0 || mDeltX == 0 && mSwitchOn) {
      if (mSrc != null) {
        mSrc.set(
            mMoveLength - mDeltX, 0, mSwitchBottom.getWidth() - mDeltX, mSwitchFrame.getHeight());
      }
    } else if (mDeltX < 0 || mDeltX == 0 && !mSwitchOn) {
      if (mSrc != null) {
        mSrc.set(-mDeltX, 0, mSwitchFrame.getWidth() - mDeltX, mSwitchFrame.getHeight());
      }
    }

    int count =
        canvas.saveLayer(
            new RectF(mDest),
            null,
            Canvas.MATRIX_SAVE_FLAG
                | Canvas.CLIP_SAVE_FLAG
                | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
                | Canvas.FULL_COLOR_LAYER_SAVE_FLAG
                | Canvas.CLIP_TO_LAYER_SAVE_FLAG);

    canvas.drawBitmap(mSwitchBottom, mSrc, mDest, null);
    canvas.drawBitmap(mSwitchThumb, mSrc, mDest, null);
    canvas.drawBitmap(mSwitchFrame, 0, 0, null);
    canvas.drawBitmap(mSwitchMask, 0, 0, mPaint);
    canvas.restoreToCount(count);
  }
예제 #4
0
 @Override
 protected void onDraw(Canvas canvas) {
   super.onDraw(canvas);
   int w = mSrcImage.getWidth();
   int sc = canvas.saveLayer(0, 0, w, w, null, Canvas.ALL_SAVE_FLAG);
   canvas.drawBitmap(mDstImage, 0, 0, null);
   mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
   canvas.drawBitmap(mSrcImage, 0, 0, mPaint);
   canvas.restoreToCount(sc);
 }
예제 #5
0
  @SuppressLint("DrawAllocation")
  @Override
  protected void onDraw(Canvas canvas) {
    if (!isInEditMode()) {
      int i = canvas.saveLayer(0.0f, 0.0f, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
      try {
        Bitmap bitmap = mWeakBitmap != null ? mWeakBitmap.get() : null;
        // Bitmap not loaded.
        if (bitmap == null || bitmap.isRecycled()) {
          Drawable drawable = getDrawable();
          if (drawable != null) {
            // Allocation onDraw but it's ok because it will not always be called.
            bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
            Canvas bitmapCanvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, getWidth(), getHeight());
            drawable.draw(bitmapCanvas);

            // If mask is already set, skip and use cached mask.
            if (mMaskBitmap == null || mMaskBitmap.isRecycled()) {
              mMaskBitmap = getBitmap();
            }

            // Draw Bitmap.
            mPaint.reset();
            mPaint.setFilterBitmap(false);
            mPaint.setXfermode(sXfermode);
            //                        mBitmapShader = new BitmapShader(mMaskBitmap,
            //                                Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            //                        mPaint.setShader(mBitmapShader);
            bitmapCanvas.drawBitmap(mMaskBitmap, 0.0f, 0.0f, mPaint);

            mWeakBitmap = new WeakReference<Bitmap>(bitmap);
          }
        }

        // Bitmap already loaded.
        if (bitmap != null) {
          mPaint.setXfermode(null);
          //                    mPaint.setShader(null);
          canvas.drawBitmap(bitmap, 0.0f, 0.0f, mPaint);
          return;
        }
      } catch (Exception e) {
        System.gc();

        LogUtil.e(
            TAG,
            String.format("Failed to draw, Id :: %s. Error occurred :: %s", getId(), e.toString()));
      } finally {
        canvas.restoreToCount(i);
      }
    } else {
      super.onDraw(canvas);
    }
  }
예제 #6
0
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //        canvas.drawColor(Color.WHITE);

    int sc = canvas.saveLayer(0, 0, screenW, screenH, null, Canvas.ALL_SAVE_FLAG);

    canvas.drawColor(0xFF0080FF);

    mPaint.setXfermode(porterDuffXfermode);

    canvas.drawBitmap(bitmapSrc, x, y, mPaint);

    mPaint.setXfermode(null);

    canvas.restoreToCount(sc);
  }
  private void createFramedPhotoBorder(int size) {
    Drawable imageDrawable =
        (image != null) ? new BitmapDrawable(getResources(), image) : placeholder;

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

    RectF outerRect = new RectF(0, 0, size, size);
    float cornerRadius = size / 20f;

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);
    canvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    imageDrawable.setBounds(0, 0, size, size);

    // Save the layer to apply the paint
    canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
    imageDrawable.draw(canvas);
    canvas.restore();
    framedPhoto = output;
  }
예제 #8
0
    @Override
    protected void onDraw(Canvas canvas) {
      super.onDraw(canvas);
      Log.i(
          "tonghu",
          "Xfermoders.SampleView-onDraw :" + canvas.getWidth() + ", " + canvas.getHeight());
      canvas.drawColor(Color.WHITE);
      canvas.translate(15, 35);
      int x = 0;
      int y = 0;

      for (int i = 0; i < sModes.length; i++) {
        paint.setShader(null);
        paint.setColor(Color.BLACK);
        paint.setStyle(Style.STROKE);
        canvas.drawRect(x - 0.5f, y - 0.5f, x + W + 0.5f, y + H + 0.5f, paint);

        paint.setShader(bg);
        paint.setStyle(Style.FILL);
        canvas.drawRect(x, y, x + W, y + H, paint);

        paint.setShader(null);
        int saveLayer = canvas.saveLayer(x, y, x + W, y + H, paint, Canvas.ALL_SAVE_FLAG);
        canvas.drawBitmap(dstB, x, y, paint);
        paint.setXfermode(sModes[i]);
        canvas.drawBitmap(srcB, x, y, paint);
        canvas.drawText(labels[i], x + W / 2, y - labelP.getTextSize() / 2, labelP);
        paint.setXfermode(null);
        canvas.restoreToCount(saveLayer);

        x += W + 10;
        if ((i % ROW_MAX) == ROW_MAX - 1) {
          x = 0;
          y += H + 30;
        }
      }
    }
예제 #9
0
  @Override
  protected void onDraw(@NonNull Canvas canvas) {
    if (mBounds == null) {
      return;
    }

    int width = mBounds.width();
    int height = mBounds.height();

    if (width == 0 || height == 0) {
      return;
    }

    if (!mCacheValid || width != mCachedWidth || height != mCachedHeight) {
      // Need to redraw the cache
      if (width == mCachedWidth && height == mCachedHeight) {
        // Have a correct-sized bitmap cache already allocated. Just erase it.
        mCacheBitmap.eraseColor(0);
      } else {
        // Allocate a new bitmap with the correct dimensions.
        mCacheBitmap.recycle();
        //noinspection AndroidLintDrawAllocation
        mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        mCachedWidth = width;
        mCachedHeight = height;
      }

      Canvas cacheCanvas = new Canvas(mCacheBitmap);
      if (mMaskDrawable != null) {
        int sc = cacheCanvas.save();
        mMaskDrawable.draw(cacheCanvas);
        mMaskedPaint.setColorFilter(
            (mDesaturateOnPress && isPressed()) ? mDesaturateColorFilter : null);
        cacheCanvas.saveLayer(
            mBoundsF,
            mMaskedPaint,
            Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
        super.onDraw(cacheCanvas);
        cacheCanvas.restoreToCount(sc);
      } else if (mDesaturateOnPress && isPressed()) {
        int sc = cacheCanvas.save();
        cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint);
        mMaskedPaint.setColorFilter(mDesaturateColorFilter);
        cacheCanvas.saveLayer(
            mBoundsF,
            mMaskedPaint,
            Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
        super.onDraw(cacheCanvas);
        cacheCanvas.restoreToCount(sc);
      } else {
        super.onDraw(cacheCanvas);
      }

      if (mBorderDrawable != null) {
        mBorderDrawable.draw(cacheCanvas);
      }
    }

    // Draw from cache
    canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null);
  }