public static BitmapDrawable createCheckerBoard(Resources res, int size) {
    size *= res.getDisplayMetrics().density;

    BitmapShader shader =
        new BitmapShader(
            Bitmap.createBitmap(
                new int[] {0xFFFFFFFF, 0xFFCCCCCC, 0xFFCCCCCC, 0xFFFFFFFF},
                2,
                2,
                Bitmap.Config.RGB_565),
            BitmapShader.TileMode.REPEAT,
            BitmapShader.TileMode.REPEAT);
    Matrix matrix = new Matrix();
    matrix.setScale(size, size);
    shader.setLocalMatrix(matrix);

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setShader(shader);

    Bitmap bm2 = Bitmap.createBitmap(size * 2, size * 2, Bitmap.Config.RGB_565);
    new Canvas(bm2).drawPaint(paint);

    BitmapDrawable drawable = new BitmapDrawable(res, bm2);
    drawable.setTileModeXY(BitmapShader.TileMode.REPEAT, BitmapShader.TileMode.REPEAT);

    return drawable;
  }
  @Override
  public Bitmap transform(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());

    int width = (source.getWidth() - size) / 2;
    int height = (source.getHeight() - size) / 2;

    Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    BitmapShader shader =
        new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
    if (width != 0 || height != 0) {
      // source isn't square, move viewport to center
      Matrix matrix = new Matrix();
      matrix.setTranslate(-width, -height);
      shader.setLocalMatrix(matrix);
    }
    paint.setShader(shader);
    paint.setAntiAlias(true);

    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);

    source.recycle();

    return bitmap;
  }
  void updateDstRect() {
    if (mApplyGravity) {
      if (mIsCircular) {
        final int minDimen = Math.min(mBitmapWidth, mBitmapHeight);
        gravityCompatApply(mGravity, minDimen, minDimen, getBounds(), mDstRect);

        // inset the drawing rectangle to the largest contained square,
        // so that a circle will be drawn
        final int minDrawDimen = Math.min(mDstRect.width(), mDstRect.height());
        final int insetX = Math.max(0, (mDstRect.width() - minDrawDimen) / 2);
        final int insetY = Math.max(0, (mDstRect.height() - minDrawDimen) / 2);
        mDstRect.inset(insetX, insetY);
        mCornerRadius = 0.5f * minDrawDimen;
      } else {
        gravityCompatApply(mGravity, mBitmapWidth, mBitmapHeight, getBounds(), mDstRect);
      }
      mDstRectF.set(mDstRect);

      if (mBitmapShader != null) {
        // setup shader matrix
        mShaderMatrix.setTranslate(mDstRectF.left, mDstRectF.top);
        mShaderMatrix.preScale(
            mDstRectF.width() / mBitmap.getWidth(), mDstRectF.height() / mBitmap.getHeight());
        mBitmapShader.setLocalMatrix(mShaderMatrix);
        mPaint.setShader(mBitmapShader);
      }

      mApplyGravity = false;
    }
  }
  @Override
  public void draw(Canvas canvas) {
    if (mRebuildShader) {
      mBitmapShader = new BitmapShader(mBitmap, mTileModeX, mTileModeY);
      if (mTileModeX == Shader.TileMode.CLAMP && mTileModeY == Shader.TileMode.CLAMP) {
        mBitmapShader.setLocalMatrix(mShaderMatrix);
      }
      mBitmapPaint.setShader(mBitmapShader);
      mRebuildShader = false;
    }

    if (mOval) {
      if (mBorderWidth > 0) {
        canvas.drawOval(mDrawableRect, mBitmapPaint);
        canvas.drawOval(mBorderRect, mBorderPaint);
      } else {
        canvas.drawOval(mDrawableRect, mBitmapPaint);
      }
    } else {
      if (mBorderWidth > 0) {
        canvas.drawRoundRect(
            mDrawableRect, Math.max(mCornerRadius, 0), Math.max(mCornerRadius, 0), mBitmapPaint);
        canvas.drawRoundRect(mBorderRect, mCornerRadius, mCornerRadius, mBorderPaint);
      } else {
        canvas.drawRoundRect(mDrawableRect, mCornerRadius, mCornerRadius, mBitmapPaint);
      }
    }
  }
Example #5
0
  @Override
  protected void onBoundsChange(Rect bounds) {
    super.onBoundsChange(bounds);
    mRect.set(0, 0, bounds.width(), bounds.height());
    radius = Math.min(bounds.width(), bounds.height()) / 2;
    mStrokeRadius = radius - mStrokeWidth / 2;

    Matrix shaderMatrix = new Matrix();
    shaderMatrix.setRectToRect(mBitmapRect, mRect, Matrix.ScaleToFit.FILL);
    mShader.setLocalMatrix(shaderMatrix);
  }
Example #6
0
  /** 计算bItmap的大小 */
  private void computeBitmapShaderSize() {
    Rect bounds = getBounds();
    if (bounds == null) return;
    // 选择缩放比较多的缩放,这样图片就不会有图片拉伸失衡
    Matrix matrix = new Matrix();

    float scaleX = bounds.width() / (float) mBitmap.getWidth();
    float scaleY = bounds.height() / (float) mBitmap.getHeight();
    float scale = scaleX < scaleY ? scaleX : scaleY;
    //        float scale = Math.min(scaleX , scaleY);
    matrix.setScale(scale, scale);
    bitmapShader.setLocalMatrix(matrix);
  }
Example #7
0
 protected void onDraw(Canvas canvas)
 {
     super.onDraw(canvas);
     if (getBackground() != null)
     {
         if (mShader != null && mShaderMatrix != null)
         {
             int i = mShaderWidth / 2;
             int l = getPositionFromValue(mCurrentValue);
             mShaderMatrix.setTranslate(l - i, 0.0F);
             mShader.setLocalMatrix(mShaderMatrix);
             canvas.drawRect(mEdgeOffset, 0.0F, (float)getWidth() - mEdgeOffset, getHeight(), mLinesPaint);
         }
         canvas.translate(0.0F, mHeightDiff);
         mShadowBottom.draw(canvas);
         canvas.translate(0.0F, -mHeightDiff);
         if (debugPaint != null)
         {
             debugPaint.setColor(-1);
             debugPaint.setAlpha(127);
             canvas.drawLine(mMinX, 0.0F, mMinX, getHeight(), debugPaint);
             canvas.drawLine(mMaxX, 0.0F, mMaxX, getHeight(), debugPaint);
             canvas.drawLine(getWidth() / 2, 0.0F, getWidth() / 2, getHeight(), debugPaint);
         }
         if (mEdgeLeft != null)
         {
             if (!mEdgeLeft.isFinished())
             {
                 int j = canvas.save();
                 canvas.concat(mEdgeMatrixLeft);
                 if (mEdgeLeft.draw(canvas))
                 {
                     postInvalidate();
                 }
                 canvas.restoreToCount(j);
             }
             if (!mEdgeRight.isFinished())
             {
                 int k = canvas.save();
                 canvas.concat(mEdgeMatrixRight);
                 if (mEdgeRight.draw(canvas))
                 {
                     postInvalidate();
                 }
                 canvas.restoreToCount(k);
                 return;
             }
         }
     }
 }
Example #8
0
 private void createShader(int i, int j)
 {
     Bitmap bitmap = Bitmap.createBitmap((int)((float)i * 2.1F), mLinesSingle.getIntrinsicHeight(), android.graphics.Bitmap.Config.ARGB_8888);
     BitmapShader bitmapshader = new BitmapShader(((BitmapDrawable)mLinesSingle).getBitmap(), android.graphics.Shader.TileMode.REPEAT, android.graphics.Shader.TileMode.CLAMP);
     Canvas canvas = new Canvas(bitmap);
     Paint paint = new Paint(7);
     paint.setShader(bitmapshader);
     int k = bitmap.getWidth() / 2;
     int l = mLinesIndicator.getIntrinsicWidth();
     j = k - l / 2;
     k += l / 2;
     double d = j % mLineWidth;
     Matrix matrix = new Matrix();
     matrix.setTranslate((float)d, 0.0F);
     bitmapshader.setLocalMatrix(matrix);
     canvas.drawRect(0.0F, 0.0F, j, bitmap.getHeight(), paint);
     mLinesIndicator.setBounds(j, 0, k, bitmap.getHeight());
     mLinesIndicator.draw(canvas);
     bitmapshader.setLocalMatrix(null);
     canvas.drawRect(k, 0.0F, bitmap.getWidth(), bitmap.getHeight(), paint);
     mShader = new BitmapShader(bitmap, android.graphics.Shader.TileMode.CLAMP, android.graphics.Shader.TileMode.CLAMP);
     mLinesPaint.setShader(mShader);
     mShaderMatrix = new Matrix();
     mShaderWidth = bitmap.getWidth();
     j = (int)(0.55172413793103448D * (double)bitmap.getHeight());
     k = (int)(9F * mScaledDensity);
     mEdgeMatrixLeft.reset();
     mEdgeMatrixLeft.postRotate(-90F);
     mEdgeMatrixLeft.postTranslate(k, j);
     mEdgeMatrixLeft.postTranslate(0.0F, mHeightDiff - 3);
     mEdgeLeft.setSize(j, k * 2);
     mEdgeMatrixRight.reset();
     mEdgeMatrixRight.postRotate(90F);
     mEdgeMatrixRight.postTranslate(i - k, 0.0F);
     mEdgeMatrixRight.postTranslate(0.0F, mHeightDiff - 3);
     mEdgeRight.setSize(j, k * 2);
 }
Example #9
0
    private void updateMatrix() {
      if (mBitmap == null) return;

      Rect bounds = getBounds();
      if (bounds.width() == 0 || bounds.height() == 0) return;

      mMatrix.reset();
      float scale = bounds.height() / (float) Math.min(mBitmap.getWidth(), mBitmap.getHeight());
      mMatrix.setScale(scale, scale, 0, 0);
      mMatrix.postTranslate(
          bounds.exactCenterX() - mBitmap.getWidth() * scale / 2,
          bounds.exactCenterY() - mBitmap.getHeight() * scale / 2);

      mBitmapShader.setLocalMatrix(mMatrix);
    }
  public Paint getDefaultOuterRimPaint() {
    // Use a linear gradient to create the 3D effect
    final LinearGradient verticalGradient =
        new LinearGradient(
            mOuterRimRect.left,
            mOuterRimRect.top,
            mOuterRimRect.left,
            mOuterRimRect.bottom,
            Color.rgb(255, 255, 255),
            Color.rgb(84, 90, 100),
            TileMode.REPEAT);

    // Use a Bitmap shader for the metallic style
    final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.light_alu);
    final BitmapShader aluminiumTile = new BitmapShader(bitmap, TileMode.REPEAT, TileMode.REPEAT);
    final Matrix matrix = new Matrix();
    matrix.setScale(1.0f / bitmap.getWidth(), 1.0f / bitmap.getHeight());
    aluminiumTile.setLocalMatrix(matrix);

    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setShader(new ComposeShader(verticalGradient, aluminiumTile, PorterDuff.Mode.MULTIPLY));
    paint.setFilterBitmap(true);
    return paint;
  }
Example #11
0
  public void setImage(Bitmap bm) {
    if (mBitmap != bm) {
      mBitmap = bm;
      if (mBitmap != null) {
        mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mMatrix.reset();
        float scale = mHeight / (float) Math.min(mBitmap.getWidth(), mBitmap.getHeight());
        mMatrix.setScale(scale, scale, 0, 0);
        mMatrix.postTranslate(
            (mHeight - mBitmap.getWidth() * scale) / 2,
            (mHeight - mBitmap.getHeight() * scale) / 2);

        mBitmapShader.setLocalMatrix(mMatrix);
      }
    }
  }
  private float calculateMatrix(Bitmap source) {

    float scale = 1.0f;
    // 拿到bitmap宽或高的小值
    int size = Math.min(source.getWidth(), source.getHeight());
    scale = (mWidth - mBorderWidth / 2) * 1.0f / size;

    mMatrix.setScale(scale, scale);
    /*移动距离等于mBorderOffset =mBorderPaint.getStrokeWidth() / 2 */
    mMatrix.postTranslate(mBorderWidth / 4, mBorderWidth / 4);
    mBitmapShader.setLocalMatrix(mMatrix);

    Log.e(TAG, "scale:" + scale);

    return scale;
  }
  public void onDraw(Canvas canvas) {
    if (getVisibility() != VISIBLE) {
      return;
    }
    if (bmp != null) {
      float scaleY = ViewProxy.getScaleY(this);
      canvas.save();

      if (needRadius) {
        roundRect.set(0, 0, getWidth(), getHeight());
        shaderMatrix.reset();
        shaderMatrix.setRectToRect(bitmapRect, roundRect, Matrix.ScaleToFit.FILL);
        bitmapShader.setLocalMatrix(shaderMatrix);
        canvas.drawRoundRect(roundRect, radius, radius, roundPaint);
      } else {
        if (orientation == 90 || orientation == 270) {
          drawRect.set(-getHeight() / 2, -getWidth() / 2, getHeight() / 2, getWidth() / 2);
          matrix.setRectToRect(bitmapRect, drawRect, Matrix.ScaleToFit.FILL);
          matrix.postRotate(orientation, 0, 0);
          matrix.postTranslate(getWidth() / 2, getHeight() / 2);
        } else if (orientation == 180) {
          drawRect.set(-getWidth() / 2, -getHeight() / 2, getWidth() / 2, getHeight() / 2);
          matrix.setRectToRect(bitmapRect, drawRect, Matrix.ScaleToFit.FILL);
          matrix.postRotate(orientation, 0, 0);
          matrix.postTranslate(getWidth() / 2, getHeight() / 2);
        } else {
          drawRect.set(0, 0, getWidth(), getHeight());
          matrix.setRectToRect(bitmapRect, drawRect, Matrix.ScaleToFit.FILL);
        }

        canvas.clipRect(
            clipLeft / scaleY,
            clipTop / scaleY,
            getWidth() - clipRight / scaleY,
            getHeight() - clipBottom / scaleY);
        try {
          canvas.drawBitmap(bmp, matrix, paint);
        } catch (Exception e) {
          FileLog.e("tmessages", e);
        }
      }
      canvas.restore();
    }
  }
Example #14
0
  private void updateShaderMatrix() {
    float scale;
    float dx = 0;
    float dy = 0;

    mShaderMatrix.set(null);

    if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) {
      scale = mDrawableRect.height() / (float) mBitmapHeight;
      dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;
    } else {
      scale = mDrawableRect.width() / (float) mBitmapWidth;
      dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;
    }

    mShaderMatrix.setScale(scale, scale);
    mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth, (int) (dy + 0.5f) + mBorderWidth);

    mBitmapShader.setLocalMatrix(mShaderMatrix);
  }
  public RoundedDrawable(Bitmap bitmap) {

    mBitmapWidth = bitmap.getWidth();
    mBitmapHeight = bitmap.getHeight();
    mBitmapRect.set(0, 0, mBitmapWidth, mBitmapHeight);

    mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    mBitmapShader.setLocalMatrix(mShaderMatrix);

    mBitmapPaint = new Paint();
    mBitmapPaint.setStyle(Paint.Style.FILL);
    mBitmapPaint.setAntiAlias(true);
    mBitmapPaint.setShader(mBitmapShader);

    mBorderPaint = new Paint();
    mBorderPaint.setStyle(Paint.Style.STROKE);
    mBorderPaint.setAntiAlias(true);
    mBorderPaint.setColor(mBorderColor.getColorForState(getState(), DEFAULT_BORDER_COLOR));
    mBorderPaint.setStrokeWidth(mBorderWidth);
  }
Example #16
0
  private void updateShaderMatrix() {
    float scale;
    float dx = 0;
    float dy = 0;

    shaderMatrix.set(null);

    if (bitmapWidth * drawableRect.height() > drawableRect.width() * bitmapHeight) {
      scale = drawableRect.height() / (float) bitmapHeight;
      dx = (drawableRect.width() - bitmapWidth * scale) * 0.5f;
    } else {
      scale = drawableRect.width() / (float) bitmapWidth;
      dy = (drawableRect.height() - bitmapHeight * scale) * 0.5f;
    }

    shaderMatrix.setScale(scale, scale);
    shaderMatrix.postTranslate(
        (int) (dx + 0.5f) + drawableRect.left, (int) (dy + 0.5f) + drawableRect.top);

    bitmapShader.setLocalMatrix(shaderMatrix);
  }
Example #17
0
    public SampleView(Context context) {
      super(context);
      dstB = createDst(W, H);
      srcB = createSrc(W, H);
      paint = new Paint();

      Bitmap bm =
          Bitmap.createBitmap(
              new int[] {0xFFFFFFFF, 0xFFCCCCCC, 0xFFCCCCCC, 0xFFFFFFFF},
              2,
              2,
              Bitmap.Config.RGB_565);

      bg = new BitmapShader(bm, TileMode.REPEAT, TileMode.REPEAT);
      Matrix matrix = new Matrix();
      matrix.setScale(6, 6);
      bg.setLocalMatrix(matrix);

      labelP = new Paint(Paint.ANTI_ALIAS_FLAG);
      labelP.setTextAlign(Align.CENTER);
    }
  RoundedDrawable(Bitmap bitmap, float cornerRadius, int border, int borderColor) {

    mBorderWidth = border;
    mBorderColor = borderColor;

    mBitmapWidth = bitmap.getWidth();
    mBitmapHeight = bitmap.getHeight();
    mBitmapRect.set(0, 0, mBitmapWidth, mBitmapHeight);

    mCornerRadius = cornerRadius;
    mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    mBitmapShader.setLocalMatrix(mShaderMatrix);

    mBitmapPaint = new Paint();
    mBitmapPaint.setAntiAlias(true);
    mBitmapPaint.setShader(mBitmapShader);

    mBorderPaint = new Paint();
    mBorderPaint.setAntiAlias(true);
    mBorderPaint.setColor(mBorderColor);
    mBorderPaint.setStrokeWidth(border);
  }
Example #19
0
  private void drawDrawable(Canvas canvas, Drawable drawable, int alpha) {
    if (drawable instanceof BitmapDrawable) {
      BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;

      Paint paint = bitmapDrawable.getPaint();
      boolean hasFilter = paint != null && paint.getColorFilter() != null;
      if (hasFilter && !isPressed) {
        bitmapDrawable.setColorFilter(null);
      } else if (!hasFilter && isPressed) {
        bitmapDrawable.setColorFilter(
            new PorterDuffColorFilter(0xffdddddd, PorterDuff.Mode.MULTIPLY));
      }
      if (colorFilter != null) {
        bitmapDrawable.setColorFilter(colorFilter);
      }
      if (bitmapShader != null) {
        drawRegion.set(imageX, imageY, imageX + imageW, imageY + imageH);
        if (isVisible) {
          roundRect.set(drawRegion);
          shaderMatrix.reset();
          shaderMatrix.setRectToRect(bitmapRect, roundRect, Matrix.ScaleToFit.FILL);
          bitmapShader.setLocalMatrix(shaderMatrix);
          roundPaint.setAlpha(alpha);
          canvas.drawRoundRect(roundRect, roundRadius, roundRadius, roundPaint);
        }
      } else {
        int bitmapW;
        int bitmapH;
        if (orientation == 90 || orientation == 270) {
          bitmapW = bitmapDrawable.getIntrinsicHeight();
          bitmapH = bitmapDrawable.getIntrinsicWidth();
        } else {
          bitmapW = bitmapDrawable.getIntrinsicWidth();
          bitmapH = bitmapDrawable.getIntrinsicHeight();
        }
        float scaleW = bitmapW / (float) imageW;
        float scaleH = bitmapH / (float) imageH;

        if (isAspectFit) {
          float scale = Math.max(scaleW, scaleH);
          canvas.save();
          bitmapW /= scale;
          bitmapH /= scale;
          drawRegion.set(
              imageX + (imageW - bitmapW) / 2,
              imageY + (imageH - bitmapH) / 2,
              imageX + (imageW + bitmapW) / 2,
              imageY + (imageH + bitmapH) / 2);
          bitmapDrawable.setBounds(drawRegion);
          try {
            bitmapDrawable.setAlpha(alpha);
            bitmapDrawable.draw(canvas);
          } catch (Exception e) {
            if (bitmapDrawable == currentImage && currentKey != null) {
              ImageLoader.getInstance().removeImage(currentKey);
              currentKey = null;
            } else if (bitmapDrawable == currentThumb && currentThumbKey != null) {
              ImageLoader.getInstance().removeImage(currentThumbKey);
              currentThumbKey = null;
            }
            setImage(
                currentImageLocation,
                currentHttpUrl,
                currentFilter,
                currentThumb,
                currentThumbLocation,
                currentThumbFilter,
                currentSize,
                currentExt,
                currentCacheOnly);
            FileLog.e("tmessages", e);
          }
          canvas.restore();
        } else {
          if (Math.abs(scaleW - scaleH) > 0.00001f) {
            canvas.save();
            canvas.clipRect(imageX, imageY, imageX + imageW, imageY + imageH);

            if (orientation != 0) {
              if (centerRotation) {
                canvas.rotate(orientation, imageW / 2, imageH / 2);
              } else {
                canvas.rotate(orientation, 0, 0);
              }
            }

            if (bitmapW / scaleH > imageW) {
              bitmapW /= scaleH;
              drawRegion.set(
                  imageX - (bitmapW - imageW) / 2,
                  imageY,
                  imageX + (bitmapW + imageW) / 2,
                  imageY + imageH);
            } else {
              bitmapH /= scaleW;
              drawRegion.set(
                  imageX,
                  imageY - (bitmapH - imageH) / 2,
                  imageX + imageW,
                  imageY + (bitmapH + imageH) / 2);
            }
            if (orientation == 90 || orientation == 270) {
              int width = (drawRegion.right - drawRegion.left) / 2;
              int height = (drawRegion.bottom - drawRegion.top) / 2;
              int centerX = (drawRegion.right + drawRegion.left) / 2;
              int centerY = (drawRegion.top + drawRegion.bottom) / 2;
              bitmapDrawable.setBounds(
                  centerX - height, centerY - width, centerX + height, centerY + width);
            } else {
              bitmapDrawable.setBounds(drawRegion);
            }
            if (isVisible) {
              try {
                bitmapDrawable.setAlpha(alpha);
                bitmapDrawable.draw(canvas);
              } catch (Exception e) {
                if (bitmapDrawable == currentImage && currentKey != null) {
                  ImageLoader.getInstance().removeImage(currentKey);
                  currentKey = null;
                } else if (bitmapDrawable == currentThumb && currentThumbKey != null) {
                  ImageLoader.getInstance().removeImage(currentThumbKey);
                  currentThumbKey = null;
                }
                setImage(
                    currentImageLocation,
                    currentHttpUrl,
                    currentFilter,
                    currentThumb,
                    currentThumbLocation,
                    currentThumbFilter,
                    currentSize,
                    currentExt,
                    currentCacheOnly);
                FileLog.e("tmessages", e);
              }
            }

            canvas.restore();
          } else {
            canvas.save();
            if (orientation != 0) {
              if (centerRotation) {
                canvas.rotate(orientation, imageW / 2, imageH / 2);
              } else {
                canvas.rotate(orientation, 0, 0);
              }
            }
            drawRegion.set(imageX, imageY, imageX + imageW, imageY + imageH);
            if (orientation == 90 || orientation == 270) {
              int width = (drawRegion.right - drawRegion.left) / 2;
              int height = (drawRegion.bottom - drawRegion.top) / 2;
              int centerX = (drawRegion.right + drawRegion.left) / 2;
              int centerY = (drawRegion.top + drawRegion.bottom) / 2;
              bitmapDrawable.setBounds(
                  centerX - height, centerY - width, centerX + height, centerY + width);
            } else {
              bitmapDrawable.setBounds(drawRegion);
            }
            if (isVisible) {
              try {
                bitmapDrawable.setAlpha(alpha);
                bitmapDrawable.draw(canvas);
              } catch (Exception e) {
                if (bitmapDrawable == currentImage && currentKey != null) {
                  ImageLoader.getInstance().removeImage(currentKey);
                  currentKey = null;
                } else if (bitmapDrawable == currentThumb && currentThumbKey != null) {
                  ImageLoader.getInstance().removeImage(currentThumbKey);
                  currentThumbKey = null;
                }
                setImage(
                    currentImageLocation,
                    currentHttpUrl,
                    currentFilter,
                    currentThumb,
                    currentThumbLocation,
                    currentThumbFilter,
                    currentSize,
                    currentExt,
                    currentCacheOnly);
                FileLog.e("tmessages", e);
              }
            }
            canvas.restore();
          }
        }
      }
    } else {
      drawRegion.set(imageX, imageY, imageX + imageW, imageY + imageH);
      drawable.setBounds(drawRegion);
      if (isVisible) {
        try {
          drawable.setAlpha(alpha);
          drawable.draw(canvas);
        } catch (Exception e) {
          FileLog.e("tmessages", e);
        }
      }
    }
  }
Example #20
0
  private void initDrawingTools() {
    rimRect = new RectF(0.1f, 0.1f, 0.9f, 0.9f);

    // the linear gradient is a bit skewed for realism
    rimPaint = new Paint();
    rimPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    rimPaint.setShader(
        new LinearGradient(
            0.40f,
            0.0f,
            0.60f,
            1.0f,
            Color.rgb(0xf0, 0xf5, 0xf0),
            Color.rgb(0x30, 0x31, 0x30),
            Shader.TileMode.CLAMP));

    rimCirclePaint = new Paint();
    rimCirclePaint.setAntiAlias(true);
    rimCirclePaint.setStyle(Paint.Style.STROKE);
    rimCirclePaint.setColor(Color.argb(0x4f, 0x33, 0x36, 0x33));
    rimCirclePaint.setStrokeWidth(0.005f);

    float rimSize = 0.02f;
    faceRect = new RectF();
    faceRect.set(
        rimRect.left + rimSize,
        rimRect.top + rimSize,
        rimRect.right - rimSize,
        rimRect.bottom - rimSize);

    faceTexture = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.plastic);
    BitmapShader paperShader =
        new BitmapShader(faceTexture, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
    Matrix paperMatrix = new Matrix();
    facePaint = new Paint();
    facePaint.setFilterBitmap(true);
    paperMatrix.setScale(1.0f / faceTexture.getWidth(), 1.0f / faceTexture.getHeight());
    paperShader.setLocalMatrix(paperMatrix);
    facePaint.setStyle(Paint.Style.FILL);
    facePaint.setShader(paperShader);

    rimShadowPaint = new Paint();
    rimShadowPaint.setShader(
        new RadialGradient(
            0.5f,
            0.5f,
            faceRect.width() / 2.0f,
            new int[] {0x00000000, 0x00000500, 0x50000500},
            new float[] {0.96f, 0.96f, 0.99f},
            Shader.TileMode.MIRROR));
    rimShadowPaint.setStyle(Paint.Style.FILL);

    scalePaint = new Paint();
    scalePaint.setStyle(Paint.Style.STROKE);
    scalePaint.setColor(0x9f004d0f);
    scalePaint.setStrokeWidth(0.005f);
    scalePaint.setAntiAlias(true);

    scalePaint.setTextSize(0.045f);
    scalePaint.setTypeface(Typeface.SANS_SERIF);
    scalePaint.setTextScaleX(0.8f);
    scalePaint.setTextAlign(Paint.Align.CENTER);

    float scalePosition = 0.10f;
    scaleRect = new RectF();
    scaleRect.set(
        faceRect.left + scalePosition,
        faceRect.top + scalePosition,
        faceRect.right - scalePosition,
        faceRect.bottom - scalePosition);

    titlePaint = new Paint();
    titlePaint.setColor(0xaf946109);
    titlePaint.setAntiAlias(true);
    titlePaint.setTypeface(Typeface.DEFAULT_BOLD);
    titlePaint.setTextAlign(Paint.Align.CENTER);
    titlePaint.setTextSize(0.05f);
    titlePaint.setTextScaleX(0.8f);

    titlePath = new Path();
    titlePath.addArc(new RectF(0.24f, 0.24f, 0.76f, 0.76f), -180.0f, -180.0f);

    logoPaint = new Paint();
    logoPaint.setFilterBitmap(true);
    logo = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.psyduck2);
    logoMatrix = new Matrix();
    logoScale = (1.0f / logo.getWidth()) * 0.3f;
    ;
    logoMatrix.setScale(logoScale, logoScale);

    handPaint = new Paint();
    handPaint.setAntiAlias(true);
    handPaint.setColor(0xff392f2c);
    handPaint.setShadowLayer(0.01f, -0.005f, -0.005f, 0x7f000000);
    handPaint.setStyle(Paint.Style.FILL);

    handPath = new Path();
    handPath.moveTo(0.5f, 0.5f + 0.2f);
    handPath.lineTo(0.5f - 0.010f, 0.5f + 0.2f - 0.007f);
    handPath.lineTo(0.5f - 0.002f, 0.5f - 0.32f);
    handPath.lineTo(0.5f + 0.002f, 0.5f - 0.32f);
    handPath.lineTo(0.5f + 0.010f, 0.5f + 0.2f - 0.007f);
    handPath.lineTo(0.5f, 0.5f + 0.2f);
    handPath.addCircle(0.5f, 0.5f, 0.025f, Path.Direction.CW);

    handScrewPaint = new Paint();
    handScrewPaint.setAntiAlias(true);
    handScrewPaint.setColor(0xff493f3c);
    handScrewPaint.setStyle(Paint.Style.FILL);

    backgroundPaint = new Paint();
    backgroundPaint.setFilterBitmap(true);
  }
  private void updateShaderMatrix() {
    float scale;
    float dx;
    float dy;

    switch (mScaleType) {
      case CENTER:
        mBorderRect.set(mBounds);
        mBorderRect.inset((mBorderWidth) / 2, (mBorderWidth) / 2);

        mShaderMatrix.set(null);
        mShaderMatrix.setTranslate(
            (int) ((mBorderRect.width() - mBitmapWidth) * 0.5f + 0.5f),
            (int) ((mBorderRect.height() - mBitmapHeight) * 0.5f + 0.5f));
        break;

      case CENTER_CROP:
        mBorderRect.set(mBounds);
        mBorderRect.inset((mBorderWidth) / 2, (mBorderWidth) / 2);

        mShaderMatrix.set(null);

        dx = 0;
        dy = 0;

        if (mBitmapWidth * mBorderRect.height() > mBorderRect.width() * mBitmapHeight) {
          scale = mBorderRect.height() / (float) mBitmapHeight;
          dx = (mBorderRect.width() - mBitmapWidth * scale) * 0.5f;
        } else {
          scale = mBorderRect.width() / (float) mBitmapWidth;
          dy = (mBorderRect.height() - mBitmapHeight * scale) * 0.5f;
        }

        mShaderMatrix.setScale(scale, scale);
        mShaderMatrix.postTranslate(
            (int) (dx + 0.5f) + mBorderWidth, (int) (dy + 0.5f) + mBorderWidth);
        break;

      case CENTER_INSIDE:
        mShaderMatrix.set(null);

        if (mBitmapWidth <= mBounds.width() && mBitmapHeight <= mBounds.height()) {
          scale = 1.0f;
        } else {
          scale =
              Math.min(
                  mBounds.width() / (float) mBitmapWidth, mBounds.height() / (float) mBitmapHeight);
        }

        dx = (int) ((mBounds.width() - mBitmapWidth * scale) * 0.5f + 0.5f);
        dy = (int) ((mBounds.height() - mBitmapHeight * scale) * 0.5f + 0.5f);

        mShaderMatrix.setScale(scale, scale);
        mShaderMatrix.postTranslate(dx, dy);

        mBorderRect.set(mBitmapRect);
        mShaderMatrix.mapRect(mBorderRect);
        mBorderRect.inset((mBorderWidth) / 2, (mBorderWidth) / 2);
        mShaderMatrix.setRectToRect(mBitmapRect, mBorderRect, Matrix.ScaleToFit.FILL);
        break;

      default:
      case FIT_CENTER:
        mBorderRect.set(mBitmapRect);
        mShaderMatrix.setRectToRect(mBitmapRect, mBounds, Matrix.ScaleToFit.CENTER);
        mShaderMatrix.mapRect(mBorderRect);
        mBorderRect.inset((mBorderWidth) / 2, (mBorderWidth) / 2);
        mShaderMatrix.setRectToRect(mBitmapRect, mBorderRect, Matrix.ScaleToFit.FILL);
        break;

      case FIT_END:
        mBorderRect.set(mBitmapRect);
        mShaderMatrix.setRectToRect(mBitmapRect, mBounds, Matrix.ScaleToFit.END);
        mShaderMatrix.mapRect(mBorderRect);
        mBorderRect.inset((mBorderWidth) / 2, (mBorderWidth) / 2);
        mShaderMatrix.setRectToRect(mBitmapRect, mBorderRect, Matrix.ScaleToFit.FILL);
        break;

      case FIT_START:
        mBorderRect.set(mBitmapRect);
        mShaderMatrix.setRectToRect(mBitmapRect, mBounds, Matrix.ScaleToFit.START);
        mShaderMatrix.mapRect(mBorderRect);
        mBorderRect.inset((mBorderWidth) / 2, (mBorderWidth) / 2);
        mShaderMatrix.setRectToRect(mBitmapRect, mBorderRect, Matrix.ScaleToFit.FILL);
        break;

      case FIT_XY:
        mBorderRect.set(mBounds);
        mBorderRect.inset((mBorderWidth) / 2, (mBorderWidth) / 2);
        mShaderMatrix.set(null);
        mShaderMatrix.setRectToRect(mBitmapRect, mBorderRect, Matrix.ScaleToFit.FILL);
        break;
    }

    mDrawableRect.set(mBorderRect);
    mBitmapShader.setLocalMatrix(mShaderMatrix);
  }
  private void setMatrix() {
    mBorderRect.set(mBounds);
    mDrawableRect.set(
        0 + mBorderWidth,
        0 + mBorderWidth,
        mBorderRect.width() - mBorderWidth,
        mBorderRect.height() - mBorderWidth);

    float scale;
    float dx;
    float dy;

    switch (mScaleType) {
      case CENTER:
        //			Log.d(TAG, "CENTER");
        mBorderRect.set(mBounds);
        mDrawableRect.set(
            0 + mBorderWidth,
            0 + mBorderWidth,
            mBorderRect.width() - mBorderWidth,
            mBorderRect.height() - mBorderWidth);

        mShaderMatrix.set(null);
        mShaderMatrix.setTranslate(
            (int) ((mDrawableRect.width() - mBitmapWidth) * 0.5f + 0.5f),
            (int) ((mDrawableRect.height() - mBitmapHeight) * 0.5f + 0.5f));
        break;
      case CENTER_CROP:
        //			Log.d(TAG, "CENTER_CROP");
        mBorderRect.set(mBounds);
        mDrawableRect.set(
            0 + mBorderWidth,
            0 + mBorderWidth,
            mBorderRect.width() - mBorderWidth,
            mBorderRect.height() - mBorderWidth);

        mShaderMatrix.set(null);

        dx = 0;
        dy = 0;

        if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) {
          scale = (float) mDrawableRect.height() / (float) mBitmapHeight;
          dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;
        } else {
          scale = (float) mDrawableRect.width() / (float) mBitmapWidth;
          dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;
        }

        mShaderMatrix.setScale(scale, scale);
        mShaderMatrix.postTranslate(
            (int) (dx + 0.5f) + mBorderWidth, (int) (dy + 0.5f) + mBorderWidth);
        break;
      case CENTER_INSIDE:
        //			Log.d(TAG, "CENTER_INSIDE");
        mShaderMatrix.set(null);

        if (mBitmapWidth <= mBounds.width() && mBitmapHeight <= mBounds.height()) {
          scale = 1.0f;
        } else {
          scale =
              Math.min(
                  (float) mBounds.width() / (float) mBitmapWidth,
                  (float) mBounds.height() / (float) mBitmapHeight);
        }

        dx = (int) ((mBounds.width() - mBitmapWidth * scale) * 0.5f + 0.5f);
        dy = (int) ((mBounds.height() - mBitmapHeight * scale) * 0.5f + 0.5f);

        mShaderMatrix.setScale(scale, scale);
        mShaderMatrix.postTranslate(dx, dy);

        mBorderRect.set(mBitmapRect);
        mShaderMatrix.mapRect(mBorderRect);
        mDrawableRect.set(
            mBorderRect.left + mBorderWidth,
            mBorderRect.top + mBorderWidth,
            mBorderRect.right - mBorderWidth,
            mBorderRect.bottom - mBorderWidth);
        mShaderMatrix.setRectToRect(mBitmapRect, mDrawableRect, Matrix.ScaleToFit.FILL);
        break;
      case FIT_CENTER:
        mBorderRect.set(mBitmapRect);
        mShaderMatrix.setRectToRect(mBitmapRect, mBounds, Matrix.ScaleToFit.CENTER);
        mShaderMatrix.mapRect(mBorderRect);
        mDrawableRect.set(
            mBorderRect.left + mBorderWidth,
            mBorderRect.top + mBorderWidth,
            mBorderRect.right - mBorderWidth,
            mBorderRect.bottom - mBorderWidth);
        mShaderMatrix.setRectToRect(mBitmapRect, mDrawableRect, Matrix.ScaleToFit.FILL);
        break;
      case FIT_END:
        mBorderRect.set(mBitmapRect);
        mShaderMatrix.setRectToRect(mBitmapRect, mBounds, Matrix.ScaleToFit.END);
        mShaderMatrix.mapRect(mBorderRect);
        mDrawableRect.set(
            mBorderRect.left + mBorderWidth,
            mBorderRect.top + mBorderWidth,
            mBorderRect.right - mBorderWidth,
            mBorderRect.bottom - mBorderWidth);
        mShaderMatrix.setRectToRect(mBitmapRect, mDrawableRect, Matrix.ScaleToFit.FILL);
        break;
      case FIT_START:
        mBorderRect.set(mBitmapRect);
        mShaderMatrix.setRectToRect(mBitmapRect, mBounds, Matrix.ScaleToFit.START);
        mShaderMatrix.mapRect(mBorderRect);
        mDrawableRect.set(
            mBorderRect.left + mBorderWidth,
            mBorderRect.top + mBorderWidth,
            mBorderRect.right - mBorderWidth,
            mBorderRect.bottom - mBorderWidth);
        mShaderMatrix.setRectToRect(mBitmapRect, mDrawableRect, Matrix.ScaleToFit.FILL);
        break;
      case FIT_XY:
      default:
        //			Log.d(TAG, "DEFAULT TO FILL");
        mBorderRect.set(mBounds);
        mDrawableRect.set(
            0 + mBorderWidth,
            0 + mBorderWidth,
            mBorderRect.width() - mBorderWidth,
            mBorderRect.height() - mBorderWidth);
        mShaderMatrix.set(null);
        mShaderMatrix.setRectToRect(mBitmapRect, mDrawableRect, Matrix.ScaleToFit.FILL);
        break;
    }
    mBitmapShader.setLocalMatrix(mShaderMatrix);
  }