Exemplo n.º 1
0
  public void handleMessage(String event, JSONObject message) {
    try {
      if (MESSAGE_ZOOM_RECT.equals(event)) {
        float x = (float) message.getDouble("x");
        float y = (float) message.getDouble("y");
        final RectF zoomRect =
            new RectF(x, y, x + (float) message.getDouble("w"), y + (float) message.getDouble("h"));
        mTarget.post(
            new Runnable() {
              public void run() {
                animatedZoomTo(zoomRect);
              }
            });
      } else if (MESSAGE_ZOOM_PAGE.equals(event)) {
        ImmutableViewportMetrics metrics = getMetrics();
        RectF cssPageRect = metrics.getCssPageRect();

        RectF viewableRect = metrics.getCssViewport();
        float y = viewableRect.top;
        // attempt to keep zoom keep focused on the center of the viewport
        float newHeight = viewableRect.height() * cssPageRect.width() / viewableRect.width();
        float dh = viewableRect.height() - newHeight; // increase in the height
        final RectF r = new RectF(0.0f, y + dh / 2, cssPageRect.width(), y + dh / 2 + newHeight);
        mTarget.post(
            new Runnable() {
              public void run() {
                animatedZoomTo(r);
              }
            });
      }
    } catch (Exception e) {
      Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
    }
  }
Exemplo n.º 2
0
    public void fling(int viewWidth, int viewHeight, int velocityX, int velocityY) {
      final RectF rect = getDisplayRect();
      if (null == rect) {
        return;
      }

      final int startX = Math.round(-rect.left);
      final int minX, maxX, minY, maxY;

      if (viewWidth < rect.width()) {
        minX = 0;
        maxX = Math.round(rect.width() - viewWidth);
      } else {
        minX = maxX = startX;
      }

      final int startY = Math.round(-rect.top);
      if (viewHeight < rect.height()) {
        minY = 0;
        maxY = Math.round(rect.height() - viewHeight);
      } else {
        minY = maxY = startY;
      }

      mCurrentX = startX;
      mCurrentY = startY;

      if (startX != maxX || startY != maxY) {
        mScroller.fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY, 0, 0);
      }
    }
Exemplo n.º 3
0
  private Item createItem(String label, float percent, int sliceColor, int itemColor) {
    Item it = new Item();
    it.mLabel = label;
    it.mItemColor = itemColor;
    it.cSliceColor = sliceColor;
    it.cPercent = percent;
    //        Log.d(TAG, "Percent for "+it.mLabel+": "+it.cPercent);

    // Calculate the highlight color. Saturate at 0xff to make sure that high values
    // don't result in aliasing.
    it.mItemHighlight =
        Color.argb(
            0xff,
            Math.min((int) (mHighlightStrength * (float) Color.red(itemColor)), 0xff),
            Math.min((int) (mHighlightStrength * (float) Color.green(itemColor)), 0xff),
            Math.min((int) (mHighlightStrength * (float) Color.blue(itemColor)), 0xff));

    it.cSliceHighlight =
        Color.argb(
            0xff,
            Math.min((int) (mHighlightStrength * (float) Color.red(sliceColor)), 0xff),
            Math.min((int) (mHighlightStrength * (float) Color.green(sliceColor)), 0xff),
            Math.min((int) (mHighlightStrength * (float) Color.blue(sliceColor)), 0xff));

    float centerX = mPieBounds.width() / 2;
    float centerY = mPieBounds.height() / 2;
    float itemW = (mPieBounds.width() / 2) * it.cPercent;
    float itemH = (mPieBounds.height() / 2) * it.cPercent;
    it.cSliceBounds = new RectF(centerX - itemW, centerY - itemH, centerX + itemW, centerY + itemH);

    return it;
  }
Exemplo n.º 4
0
 protected float b(RectF rectF, c cVar) {
   return cVar.b >= this.d
       ? (rectF.height() - this.l) - this.n
       : ((float) this.a)
           + ((((float) cVar.b) / ((float) this.d))
               * (((rectF.height() - this.l) - this.n) - ((float) this.a)));
 }
Exemplo n.º 5
0
  private void computeGlyphPath() {
    drawableArea.set(getBounds());
    drawableArea.inset(padding, padding);
    glyphPaint.getTextPath(glyph, 0, 1, 0, 0, glyphPath);
    // Add an extra path point to fix the icon remaining blank on a Galaxy Note 2 running 4.1.2.
    glyphPath.computeBounds(glyphPathBounds, false);
    final float centerX = glyphPathBounds.centerX();
    final float centerY = glyphPathBounds.centerY();
    glyphPath.moveTo(centerX, centerY);
    glyphPath.lineTo(centerX + 0.001f, centerY + 0.001f);
    final float areaWidthF = (float) drawableArea.width();
    final float areaHeightF = (float) drawableArea.height();
    final float scaleX = areaWidthF / glyphPathBounds.width();
    final float scaleY = areaHeightF / glyphPathBounds.height();
    final float scaleFactor = Math.min(scaleX, scaleY);
    glyphPathTransform.setScale(scaleFactor, scaleFactor);
    glyphPath.transform(glyphPathTransform);

    // TODO this two pass calculation irks me.
    // It has to be possible to push this into a single Matrix transform; what makes it hard is
    // that the origin of Text is not top-left, but baseline-left so need to account for that.
    glyphPath.computeBounds(glyphPathBounds, false);
    final float areaLeftF = (float) drawableArea.left;
    final float areaTopF = (float) drawableArea.top;
    float transX = areaLeftF - glyphPathBounds.left;
    transX += 0.5f * Math.abs(areaWidthF - glyphPathBounds.width());
    float transY = areaTopF - glyphPathBounds.top;
    transY += 0.5f * Math.abs(areaHeightF - glyphPathBounds.height());
    glyphPath.offset(transX, transY);

    invalidateSelf();
  }
  @Override
  public void draw(Canvas canvas) {
    mBounds.set(getBounds());

    if (mMode == MODE_RATING) {
      float arcAngle = mCurrentValue * 360f;
      final float arcStart = -90f;
      canvas.drawArc(mBounds, arcStart + arcAngle, 360f - arcAngle, true, mBackgroundArcPaint);
      canvas.drawArc(mBounds, arcStart, arcAngle, true, mArcPaint);
    } else if (mMode == MODE_PROMPT) {
      canvas.drawCircle(
          mBounds.centerX(),
          mBounds.centerY(),
          mBounds.height() * BACKGROUND_CIRCLE_RADIUS_RATIO,
          mBackgroundArcPaint);
    }

    canvas.drawCircle(
        mBounds.centerX(),
        mBounds.centerY(),
        mBounds.height() * FOREGROUND_CIRCLE_RADIUS_RATIO,
        mForegroundCirclePaint);

    if (mText != null) {
      canvas.drawText(
          mText,
          mBounds.centerX() - (mTextWidth / 2f),
          mBounds.centerY() + (mTextHeight / 2f),
          mTextPaint);
    }
  }
  protected void drawCenterText(Canvas c) {

    SpannableString centerText = mChart.getCenterText();

    if (mChart.isDrawCenterTextEnabled() && centerText != null) {

      PointF center = mChart.getCenterCircleBox();

      float innerRadius =
          mChart.isDrawHoleEnabled() && mChart.isHoleTransparent()
              ? mChart.getRadius() * (mChart.getHoleRadius() / 100f)
              : mChart.getRadius();

      RectF holeRect = mRectBuffer[0];
      holeRect.left = center.x - innerRadius;
      holeRect.top = center.y - innerRadius;
      holeRect.right = center.x + innerRadius;
      holeRect.bottom = center.y + innerRadius;
      RectF boundingRect = mRectBuffer[1];
      boundingRect.set(holeRect);

      float radiusPercent = mChart.getCenterTextRadiusPercent();
      if (radiusPercent > 0.0) {
        boundingRect.inset(
            (boundingRect.width() - boundingRect.width() * radiusPercent) / 2.f,
            (boundingRect.height() - boundingRect.height() * radiusPercent) / 2.f);
      }

      if (!centerText.equals(mCenterTextLastValue) || !boundingRect.equals(mCenterTextLastBounds)) {

        // Next time we won't recalculate StaticLayout...
        mCenterTextLastBounds.set(boundingRect);
        mCenterTextLastValue = centerText;

        float width = mCenterTextLastBounds.width();

        // If width is 0, it will crash. Always have a minimum of 1
        mCenterTextLayout =
            new StaticLayout(
                centerText,
                0,
                centerText.length(),
                mCenterTextPaint,
                (int) Math.max(Math.ceil(width), 1.f),
                Layout.Alignment.ALIGN_CENTER,
                1.f,
                0.f,
                false);
      }

      float layoutHeight = mCenterTextLayout.getHeight();

      c.save();
      c.translate(
          boundingRect.left, boundingRect.top + (boundingRect.height() - layoutHeight) / 2.f);
      mCenterTextLayout.draw(c);
      c.restore();
    }
  }
Exemplo n.º 8
0
  /**
   * Grow by.
   *
   * @param dx the dx
   * @param dy the dy
   */
  void growBy(float dx, float dy) {
    if (mMaintainAspectRatio) {
      if (dx != 0) {
        dy = dx / mInitialAspectRatio;
      } else if (dy != 0) {
        dx = dy * mInitialAspectRatio;
      }
    }

    // Don't let the cropping rectangle grow too fast.
    // Grow at most half of the difference between the image rectangle and
    // the cropping rectangle.
    RectF r = new RectF(mCropRect);
    if (dx > 0F && r.width() + 2 * dx > mImageRect.width()) {

      float adjustment = (mImageRect.width() - r.width()) / 2F;
      dx = adjustment;
      if (mMaintainAspectRatio) {
        dy = dx / mInitialAspectRatio;
      }
    }
    if (dy > 0F && r.height() + 2 * dy > mImageRect.height()) {
      float adjustment = (mImageRect.height() - r.height()) / 2F;
      dy = adjustment;
      if (mMaintainAspectRatio) {
        dx = dy * mInitialAspectRatio;
      }
    }

    r.inset(-dx, -dy);

    // Don't let the cropping rectangle shrink too fast.
    final float widthCap = 25F;
    if (r.width() < widthCap) {
      return;
      //            r.inset(-(widthCap - r.width()) / 2F, 0F);
    }
    float heightCap = mMaintainAspectRatio ? (widthCap / mInitialAspectRatio) : widthCap;
    if (r.height() < heightCap) {
      return;
      //            r.inset(0F, -(heightCap - r.height()) / 2F);
    }

    // Put the cropping rectangle inside the image rectangle.
    if (r.left < mImageRect.left) {
      r.offset(mImageRect.left - r.left, 0F);
    } else if (r.right > mImageRect.right) {
      r.offset(-(r.right - mImageRect.right), 0);
    }
    if (r.top < mImageRect.top) {
      r.offset(0F, mImageRect.top - r.top);
    } else if (r.bottom > mImageRect.bottom) {
      r.offset(0F, -(r.bottom - mImageRect.bottom));
    }

    mCropRect.set(r);
    mDrawRect = computeLayout();
    mContext.invalidate();
  }
    private void drawCircle(Canvas canvas, BatteryTracker tracker, float textX, RectF drawRect) {
      boolean unknownStatus = tracker.status == BatteryManager.BATTERY_STATUS_UNKNOWN;
      int animOffset = tracker.shouldIndicateCharging() ? mAnimOffset : 0;
      int level = tracker.level;
      Paint paint;

      if (unknownStatus) {
        paint = mBackPaint;
        level = 100; // Draw all the circle;
      } else {
        paint = mFrontPaint;
        paint.setColor(getColorForLevel(level));
        if (tracker.status == BatteryManager.BATTERY_STATUS_FULL) {
          level = 100;
        }
      }

      // draw thin gray ring first
      canvas.drawArc(drawRect, 270, 360, false, mBackPaint);
      // draw colored arc representing charge level
      canvas.drawArc(drawRect, 270 + animOffset, 3.6f * level, false, paint);
      // if chosen by options, draw percentage text in the middle
      // always skip percentage when 100, so layout doesnt break
      if (unknownStatus) {
        mTextPaint.setColor(paint.getColor());
        canvas.drawText("?", textX, mTextY, mTextPaint);

      } else if (tracker.shouldIndicateCharging() && (!mShowPercent || level == 100)) {
        // draw the bolt
        final float bl = (int) (drawRect.left + drawRect.width() / 3.2f);
        final float bt = (int) (drawRect.top + drawRect.height() / 4f);
        final float br = (int) (drawRect.right - drawRect.width() / 5.2f);
        final float bb = (int) (drawRect.bottom - drawRect.height() / 8f);
        if (mBoltFrame.left != bl
            || mBoltFrame.top != bt
            || mBoltFrame.right != br
            || mBoltFrame.bottom != bb) {
          mBoltFrame.set(bl, bt, br, bb);
          mBoltPath.reset();
          mBoltPath.moveTo(
              mBoltFrame.left + mBoltPoints[0] * mBoltFrame.width(),
              mBoltFrame.top + mBoltPoints[1] * mBoltFrame.height());
          for (int i = 2; i < mBoltPoints.length; i += 2) {
            mBoltPath.lineTo(
                mBoltFrame.left + mBoltPoints[i] * mBoltFrame.width(),
                mBoltFrame.top + mBoltPoints[i + 1] * mBoltFrame.height());
          }
          mBoltPath.lineTo(
              mBoltFrame.left + mBoltPoints[0] * mBoltFrame.width(),
              mBoltFrame.top + mBoltPoints[1] * mBoltFrame.height());
        }
        canvas.drawPath(mBoltPath, mBoltPaint);

      } else if (level < 100 && mShowPercent) {
        mTextPaint.setColor(paint.getColor());
        canvas.drawText(Integer.toString(level), textX, mTextY, mTextPaint);
      }
    }
Exemplo n.º 10
0
  /** Do all of the recalculations needed when the data array changes. */
  private void onDataChanged() {
    int angle = 360 / mData.size();
    // This is the extra degrees that cannot be equally divided by the number of sides
    int extra = 360 % mData.size();

    // When the data changes, we have to recalculate
    // all of the angles.
    int currentAngle = 0;
    for (Item it : mData) {

      // Adds the extra degrees to the last slice to ensue no gaps
      if (mData.get(mData.size() - 1).mLabel.equals(it.mLabel)) {
        it.mEndAngle = currentAngle + angle + extra;
      } else {

        it.mEndAngle = currentAngle + angle;
      }
      it.mStartAngle = currentAngle;
      currentAngle = it.mEndAngle;

      // Recalculate the gradient shaders. There are
      // three values in this gradient, even though only
      // two are necessary, in order to work around
      // a bug in certain versions of the graphics engine
      // that expects at least three values if the
      // positions array is non-null.
      //
      it.mItemShader =
          new SweepGradient(
              mPieBounds.width() / 2.0f,
              mPieBounds.height() / 2.0f,
              new int[] {
                it.mItemHighlight, it.mItemHighlight, it.mItemColor, it.mItemColor,
              },
              new float[] {
                0,
                (float) (360 - it.mEndAngle) / 360.0f,
                (float) (360 - it.mStartAngle) / 360.0f,
                1.0f
              });

      it.cSliceShader =
          new SweepGradient(
              mPieBounds.width() / 2.0f,
              mPieBounds.height() / 2.0f,
              new int[] {
                it.cSliceHighlight, it.cSliceHighlight, it.cSliceColor, it.cSliceColor,
              },
              new float[] {
                0,
                (float) (360 - it.mEndAngle) / 360.0f,
                (float) (360 - it.mStartAngle) / 360.0f,
                1.0f
              });
    }
    calcCurrentItem();
    onScrollFinished();
  }
Exemplo n.º 11
0
 private int getSectionByPoint(float y) {
   if (mSections == null || mSections.length == 0) return 0;
   if (y < mIndexbarRect.top + mIndexbarMargin) return 0;
   if (y >= mIndexbarRect.top + mIndexbarRect.height() - mIndexbarMargin)
     return mSections.length - 1;
   return (int)
       ((y - mIndexbarRect.top - mIndexbarMargin)
           / ((mIndexbarRect.height() - 2 * mIndexbarMargin) / mSections.length));
 }
Exemplo n.º 12
0
 @Override
 public void draw(Canvas canvas, Paint paint) {
   RectF rectF = rect();
   float x = rectF.left + rectF.width() / 2;
   float y = rectF.top + rectF.height() / 2;
   float max = (rectF.width() > rectF.height()) ? rectF.width() : rectF.height();
   float r = max / 2;
   canvas.drawCircle(x, y, r, paint);
 }
Exemplo n.º 13
0
  /** Updates application UI and image on TV according to scale values. */
  private float getCurrentScale() {
    RectF imageRect = mPinchToZoomTransformation.getRect();

    float imageViewRatio = (float) mImageViewWidth / mImageViewHeight;
    float imageRatio = imageRect.width() / imageRect.height();

    return imageViewRatio < imageRatio
        ? imageRect.width() / mImageViewWidth
        : imageRect.height() / mImageViewHeight;
  }
Exemplo n.º 14
0
  /** Recalculates page rectangles. */
  private void updatePageRects() {
    if (mViewRect.width() == 0 || mViewRect.height() == 0) {
      return;
    } else if (mViewMode == SHOW_ONE_PAGE) {
      mPageRectRight.set(mViewRect);
      mPageRectRight.left += mViewRect.width() * mMargins.left;
      mPageRectRight.right -= mViewRect.width() * mMargins.right;
      mPageRectRight.top += mViewRect.height() * mMargins.top;
      mPageRectRight.bottom -= mViewRect.height() * mMargins.bottom;

      mPageRectLeft.set(mPageRectRight);
      mPageRectLeft.offset(-mPageRectRight.width(), 0);

      int bitmapW = (int) ((mPageRectRight.width() * mViewportWidth) / mViewRect.width());
      int bitmapH = (int) ((mPageRectRight.height() * mViewportHeight) / mViewRect.height());
      mObserver.onPageSizeChanged(bitmapW, bitmapH);
    } else if (mViewMode == SHOW_TWO_PAGES) {
      mPageRectRight.set(mViewRect);
      mPageRectRight.left += mViewRect.width() * mMargins.left;
      mPageRectRight.right -= mViewRect.width() * mMargins.right;
      mPageRectRight.top += mViewRect.height() * mMargins.top;
      mPageRectRight.bottom -= mViewRect.height() * mMargins.bottom;

      mPageRectLeft.set(mPageRectRight);
      mPageRectLeft.right = (mPageRectLeft.right + mPageRectLeft.left) / 2;
      mPageRectRight.left = mPageRectLeft.right;

      int bitmapW = (int) ((mPageRectRight.width() * mViewportWidth) / mViewRect.width());
      int bitmapH = (int) ((mPageRectRight.height() * mViewportHeight) / mViewRect.height());
      mObserver.onPageSizeChanged(bitmapW, bitmapH);
    }
  }
Exemplo n.º 15
0
  private void setup() {
    if (!mReady) {
      mSetupPending = true;
      return;
    }

    if (mBitmap == null) {
      return;
    }

    mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

    mBitmapPaint.setAntiAlias(true);
    mBitmapPaint.setShader(mBitmapShader);

    mBorderPaint.setStyle(Paint.Style.STROKE);
    mBorderPaint.setAntiAlias(true);
    mBorderPaint.setColor(mBorderColor);
    mBorderPaint.setStrokeWidth(mBorderWidth);

    mBitmapHeight = mBitmap.getHeight();
    mBitmapWidth = mBitmap.getWidth();

    mBorderRect.set(0, 0, getWidth(), getHeight());
    mBorderRadius =
        Math.min(
            (mBorderRect.height() - mBorderWidth) / 2, (mBorderRect.width() - mBorderWidth) / 2);

    mDrawableRect.set(
        mBorderWidth,
        mBorderWidth,
        mBorderRect.width() - mBorderWidth,
        mBorderRect.height() - mBorderWidth);
    mDrawableRadius = Math.min(mDrawableRect.height() / 2, mDrawableRect.width() / 2);

    mFlagBackgroundPaint.setColor(Color.BLACK & 0x66FFFFFF);
    mFlagBackgroundPaint.setFlags(TextPaint.ANTI_ALIAS_FLAG);

    mFlagTextPaint.setFlags(TextPaint.ANTI_ALIAS_FLAG);
    mFlagTextPaint.setTextAlign(Align.CENTER);
    mFlagTextPaint.setColor(Color.WHITE);
    mFlagTextPaint.setTextSize(getResources().getDisplayMetrics().density * 18);

    mSweepGradient =
        new SweepGradient(
            getWidth() / 2,
            getHeight() / 2,
            new int[] {Color.rgb(255, 255, 255), Color.rgb(1, 209, 255)},
            null);

    mBorderPaint.setShader(mSweepGradient);

    updateShaderMatrix();
    invalidate();
  }
Exemplo n.º 16
0
  boolean layout(PointLabel label, LabelIndex labels) {
    String text = label.getText();
    Feature f = label.getFeature();
    Geometry g = label.getGeometry();

    Rule rule = label.getRule();

    // get center in screen space
    Coordinate centroid = g.getCentroid().getCoordinate();

    PointF anchor = tx.getWorldToCanvas().map(centroid);

    // apply offsets
    anchor.x += rule.number(f, TEXT_DX, 0f);
    anchor.y += rule.number(f, TEXT_DY, 0f);

    Paint p = label.get(Paint.class, Paint.class);

    // compute bounds of this label
    Rect b = new Rect();
    p.getTextBounds(text, 0, text.length(), b);

    // padding
    float padding = rule.number(f, TEXT_MIN_PADDING, 0f);
    if (padding > 0f) {
      b = expand(b, padding);
    }

    // label.setAnchor(new Coordinate(center.x,center.y));
    // label.setBox(envelope(rectFromBottomLeft(center, b.width(), b.height())));

    RectF c = new RectF(b);
    tx.getCanvasToWorld().mapRect(c);

    centroid = tx.getCanvasToWorld().map(anchor);
    label.setAnchor(centroid);

    RectF box = null;
    switch (p.getTextAlign()) {
      case LEFT:
        box = rectFromBottomLeft(point(centroid), c.width(), c.height());
        break;
      case CENTER:
        box = rectFromBottomCenter(point(centroid), c.width(), c.height());
        break;
      case RIGHT:
        box = rectFromBottomRight(point(centroid), c.width(), c.height());
        break;
    }

    label.setBox(envelope(box));

    return labels.insert(label);
  }
  private void drawSatValPanel(Canvas canvas) {

    final RectF rect = mSatValRect;
    boolean redrawCache = (hueCache != mHue);

    hueCache = mHue;

    if (BORDER_WIDTH_PX > 0) {
      mBorderPaint.setColor(mBorderColor);
      canvas.drawRect(
          mDrawingRect.left,
          mDrawingRect.top,
          rect.right + BORDER_WIDTH_PX,
          rect.bottom + BORDER_WIDTH_PX,
          mBorderPaint);
    }

    if (redrawCache || drawCache == null) {
      if (mValShader == null) {
        mValShader =
            new LinearGradient(
                rect.left,
                rect.top,
                rect.left,
                rect.bottom,
                0xffffffff,
                0xff000000,
                TileMode.CLAMP);
      }

      int rgb = Color.HSVToColor(new float[] {mHue, 1f, 1f});

      mSatShader =
          new LinearGradient(
              rect.left, rect.top, rect.right, rect.top, 0xffffffff, rgb, TileMode.CLAMP);
      ComposeShader mShader = new ComposeShader(mValShader, mSatShader, PorterDuff.Mode.MULTIPLY);
      mSatValPaint.setShader(mShader);

      drawCache =
          Bitmap.createBitmap((int) rect.width(), (int) rect.height(), Bitmap.Config.ARGB_8888);
      Canvas cacheCanvas = new Canvas(drawCache);
      cacheCanvas.drawRect(0.f, 0.f, rect.width(), rect.height(), mSatValPaint);
    }

    canvas.drawBitmap(drawCache, rect.left, rect.top, mSatValPaint);

    Point p = satValToPoint(mSat, mVal);

    mSatValTrackerPaint.setColor(0xff000000);
    canvas.drawCircle(p.x, p.y, PALETTE_CIRCLE_TRACKER_RADIUS - 1f * mDensity, mSatValTrackerPaint);

    mSatValTrackerPaint.setColor(0xffdddddd);
    canvas.drawCircle(p.x, p.y, PALETTE_CIRCLE_TRACKER_RADIUS, mSatValTrackerPaint);
  }
Exemplo n.º 18
0
 void drawTestBox(Canvas cv, RectF r, float rot, String text) {
   cv.save();
   cv.translate(r.centerX(), r.centerY());
   cv.rotate((float) (rot * 180 / Math.PI));
   RectF rs = new RectF(-r.width() / 2, -r.height() / 2, r.width() / 2, r.height() / 2);
   cv.drawRect(rs, paintIcon);
   if (text != null) {
     paintText.setTextSize(paintText.getTextSize() - 4);
     cv.drawText(text, rs.centerX(), rs.centerY(), paintText);
     paintText.setTextSize(paintText.getTextSize() + 4);
   }
   cv.restore();
 }
 private void drawShadow(Canvas canvas) {
   float f = -mCornerRadius - mShadowSize;
   float f1 = mCornerRadius + (float) mInsetShadow + mRawShadowSize / 2.0F;
   int i;
   boolean flag;
   int j;
   if (mCardBounds.width() - 2.0F * f1 > 0.0F) {
     i = 1;
   } else {
     i = 0;
   }
   if (mCardBounds.height() - 2.0F * f1 > 0.0F) {
     flag = true;
   } else {
     flag = false;
   }
   j = canvas.save();
   canvas.translate(mCardBounds.left + f1, mCardBounds.top + f1);
   canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
   if (i != 0) {
     canvas.drawRect(0.0F, f, mCardBounds.width() - 2.0F * f1, -mCornerRadius, mEdgeShadowPaint);
   }
   canvas.restoreToCount(j);
   j = canvas.save();
   canvas.translate(mCardBounds.right - f1, mCardBounds.bottom - f1);
   canvas.rotate(180F);
   canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
   if (i != 0) {
     float f2 = mCardBounds.width();
     float f3 = -mCornerRadius;
     canvas.drawRect(0.0F, f, f2 - 2.0F * f1, mShadowSize + f3, mEdgeShadowPaint);
   }
   canvas.restoreToCount(j);
   i = canvas.save();
   canvas.translate(mCardBounds.left + f1, mCardBounds.bottom - f1);
   canvas.rotate(270F);
   canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
   if (flag) {
     canvas.drawRect(0.0F, f, mCardBounds.height() - 2.0F * f1, -mCornerRadius, mEdgeShadowPaint);
   }
   canvas.restoreToCount(i);
   i = canvas.save();
   canvas.translate(mCardBounds.right - f1, mCardBounds.top + f1);
   canvas.rotate(90F);
   canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
   if (flag) {
     canvas.drawRect(0.0F, f, mCardBounds.height() - 2.0F * f1, -mCornerRadius, mEdgeShadowPaint);
   }
   canvas.restoreToCount(i);
 }
  @Override
  public void draw(Canvas canvas) {
    String file = getEnvObject().getCurrentRepresentation().getIcon();
    Path objectPath =
        DrawingUtils.freedomPolygonToPath(
            (FreedomPolygon) getEnvObject().getCurrentRepresentation().getShape());
    RectF box = new RectF();
    objectPath.computeBounds(box, true);
    System.out.print("GPT box: box widht:" + box.width() + " box heigh" + box.height());
    drawingMatrix = new Matrix();
    float rotation = (float) getEnvObject().getCurrentRepresentation().getRotation();
    drawingMatrix.postRotate(rotation);
    drawingMatrix.postTranslate(
        getEnvObject().getCurrentRepresentation().getOffset().getX(),
        getEnvObject().getCurrentRepresentation().getOffset().getY());
    Bitmap bmp = null;
    if (file != null) { // TODO: Asign the bmp in the setEnvObject
      bmp = BitmapUtils.getImage(file, (int) box.width(), (int) box.height());
    }
    if (bmp != null) {
      ghostBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Config.ARGB_8888);
      canvas.drawBitmap(bmp, drawingMatrix, null);

    } else {
      // TODO: Cache path
      Paint paint = new Paint();
      paint.setStyle(Style.FILL);

      ghostPath = new Path();
      objectPath.transform(drawingMatrix, ghostPath);
      int fillColor = -1;
      try {
        fillColor = Color.parseColor(getEnvObject().getCurrentRepresentation().getFillColor());
        paint.setColor(fillColor);
        canvas.drawPath(ghostPath, paint);
      } catch (IllegalArgumentException ex) {
        System.out.println("ParseColor exception in fill");
      }
      int borderColor = -1;
      try {
        borderColor = Color.parseColor(getEnvObject().getCurrentRepresentation().getBorderColor());
        paint.setColor(borderColor);
        paint.setStyle(Style.STROKE);
        canvas.drawPath(ghostPath, paint);
      } catch (IllegalArgumentException ex) {
        System.out.println("ParseColor exception in border");
      }
    }
  }
Exemplo n.º 21
0
 private void drawShadow(Canvas canvas) {
   final float edgeShadowTop = -mCornerRadius - mShadowSize;
   final float inset = mCornerRadius + mInsetShadow + mRawShadowSize / 2;
   final boolean drawHorizontalEdges = mCardBounds.width() - 2 * inset > 0;
   final boolean drawVerticalEdges = mCardBounds.height() - 2 * inset > 0;
   // LT
   int saved = canvas.save();
   canvas.translate(mCardBounds.left + inset, mCardBounds.top + inset);
   canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
   if (drawHorizontalEdges) {
     canvas.drawRect(
         0, edgeShadowTop, mCardBounds.width() - 2 * inset, -mCornerRadius, mEdgeShadowPaint);
   }
   canvas.restoreToCount(saved);
   // RB
   saved = canvas.save();
   canvas.translate(mCardBounds.right - inset, mCardBounds.bottom - inset);
   canvas.rotate(180f);
   canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
   if (drawHorizontalEdges) {
     canvas.drawRect(
         0,
         edgeShadowTop,
         mCardBounds.width() - 2 * inset,
         -mCornerRadius + mShadowSize,
         mEdgeShadowPaint);
   }
   canvas.restoreToCount(saved);
   // LB
   saved = canvas.save();
   canvas.translate(mCardBounds.left + inset, mCardBounds.bottom - inset);
   canvas.rotate(270f);
   canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
   if (drawVerticalEdges) {
     canvas.drawRect(
         0, edgeShadowTop, mCardBounds.height() - 2 * inset, -mCornerRadius, mEdgeShadowPaint);
   }
   canvas.restoreToCount(saved);
   // RT
   saved = canvas.save();
   canvas.translate(mCardBounds.right - inset, mCardBounds.top + inset);
   canvas.rotate(90f);
   canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
   if (drawVerticalEdges) {
     canvas.drawRect(
         0, edgeShadowTop, mCardBounds.height() - 2 * inset, -mCornerRadius, mEdgeShadowPaint);
   }
   canvas.restoreToCount(saved);
 }
Exemplo n.º 22
0
  private void setup() {
    if (!mReady) {
      mSetupPending = true;
      return;
    }

    if (getWidth() == 0 && getHeight() == 0) {
      return;
    }

    if (mBitmap == null) {
      invalidate();
      return;
    }

    // 给图片设置bitmapShader,到时花园时,画的便是这张图片
    mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    mBitmapPaint.setAntiAlias(true);
    mBitmapPaint.setShader(mBitmapShader);

    mBorderPaint.setStyle(Paint.Style.STROKE);
    mBorderPaint.setAntiAlias(true);
    mBorderPaint.setColor(mBorderColor);
    mBorderPaint.setStrokeWidth(mBorderWidth);

    mFillPaint.setStyle(Paint.Style.FILL);
    mFillPaint.setAntiAlias(true);
    mFillPaint.setColor(mFillColor);

    mBitmapHeight = mBitmap.getHeight();
    mBitmapWidth = mBitmap.getWidth();

    mBorderRect.set(calculateBounds());
    mBorderRadius =
        Math.min(
            (mBorderRect.height() - mBorderWidth) / 2.0f,
            (mBorderRect.width() - mBorderWidth) / 2.0f);

    mDrawableRect.set(mBorderRect);
    if (!mBorderOverlay && mBorderWidth > 0) {
      mDrawableRect.inset(mBorderWidth - 1.0f, mBorderWidth - 1.0f);
    }
    mDrawableRadius = Math.min(mDrawableRect.height() / 2.0f, mDrawableRect.width() / 2.0f);

    applyColorFilter();
    updateShaderMatrix();
    invalidate();
  }
Exemplo n.º 23
0
  public void drawTexture(BasicTexture texture, RectF source, RectF target) {
    if (target.width() <= 0 || target.height() <= 0) return;

    // Copy the input to avoid changing it.
    mDrawTextureSourceRect.set(source);
    mDrawTextureTargetRect.set(target);
    source = mDrawTextureSourceRect;
    target = mDrawTextureTargetRect;

    mGLState.setBlendEnabled(mBlendEnabled && (!texture.isOpaque() || mAlpha < OPAQUE_ALPHA));
    if (!bindTexture(texture)) return;
    convertCoordinate(source, target, texture);
    setTextureCoords(source);
    mGLState.setTextureAlpha(mAlpha);
    textureRect(target.left, target.top, target.width(), target.height());
  }
  /**
   * Method that load the gles texture and apply to the requestor frame (which includes fix the
   * aspect ratio and/or effects and borders)
   *
   * @param requestor The requestor target
   * @param ti The original texture information (the one with the bitmap one)
   */
  private void applyToRequestor(TextureRequestor requestor, GLESTextureInfo ti) {
    // Transform requestor dimensions to screen dimensions
    RectF dimens = requestor.getRequestorDimensions();
    Rect pixels =
        new Rect(
            0,
            0,
            (int) (mScreenDimensions.width() * dimens.width() / 2),
            (int) (mScreenDimensions.height() * dimens.height() / 2));

    final Disposition disposition = requestor.getDisposition();
    synchronized (mEffectsSync) {
      if (disposition.hasFlag(Disposition.EFFECT_FLAG)) {
        ti.effect = mEffects.getNextEffect();
      }
      if (disposition.hasFlag(Disposition.BORDER_FLAG)) {
        ti.border = mBorders.getNextBorder();
      }
    }

    // Check if we have to apply any correction to the image
    GLESTextureInfo dst;
    if (ti.bitmap != null && Preferences.General.isFixAspectRatio(mContext)) {

      // Create a texture of power of two here to avoid scaling the bitmap twice
      int w = pixels.width();
      int h = pixels.height();
      if (!BitmapUtils.isPowerOfTwo(w, h)
          && PreferencesProvider.Preferences.General.isPowerOfTwo(mContext)) {
        w = h = BitmapUtils.calculateUpperPowerOfTwo(Math.min(w, h));
      }

      // Create a thumbnail of the image
      Bitmap thumb = BitmapUtils.createScaledBitmap(ti.bitmap, w, h, BitmapUtils.ScalingLogic.CROP);
      if (!thumb.equals(ti.bitmap)) {
        ti.bitmap.recycle();
      }
      dst = GLESUtil.loadTexture(mContext, thumb, ti.effect, ti.border, pixels);
    } else {
      // Load the texture without any correction
      dst = GLESUtil.loadTexture(mContext, ti.bitmap, ti.effect, ti.border, pixels);
    }

    // Swap references
    ti.bitmap = dst.bitmap;
    ti.handle = dst.handle;
    ti.effect = null;
    ti.border = null;
    dst.handle = 0;
    dst.bitmap = null;

    // And notify to the requestor
    requestor.setTextureHandle(ti);

    // Clean up memory
    if (ti.bitmap != null) {
      ti.bitmap.recycle();
      ti.bitmap = null;
    }
  }
Exemplo n.º 25
0
  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 boolean onSingleTapConfirmed(MotionEvent e) {
    if (this.photoViewAttacher == null) return false;

    ImageView imageView = photoViewAttacher.getImageView();

    if (null != photoViewAttacher.getOnPhotoTapListener()) {
      final RectF displayRect = photoViewAttacher.getDisplayRect();

      if (null != displayRect) {
        final float x = e.getX(), y = e.getY();

        // Check to see if the user tapped on the photo
        if (displayRect.contains(x, y)) {

          float xResult = (x - displayRect.left) / displayRect.width();
          float yResult = (y - displayRect.top) / displayRect.height();

          photoViewAttacher.getOnPhotoTapListener().onPhotoTap(imageView, xResult, yResult);
          return true;
        }
      }
    }
    if (null != photoViewAttacher.getOnViewTapListener()) {
      photoViewAttacher.getOnViewTapListener().onViewTap(imageView, e.getX(), e.getY());
    }

    return false;
  }
Exemplo n.º 27
0
  @Override
  public void onUpdate(long elapsedTime) {
    mScreenRect.set(0, 0, mGameView.getWidth(), mGameView.getHeight());
    mLoops++;

    mTimer += elapsedTime;

    if (mTimer > mMaxTimer) {
      gameOver();
    }

    mRobot.update(elapsedTime);

    // Check if robot escaped
    if (!RectF.intersects(mScreenRect, mRobot.getRect())) {
      // change to random positions
      mRobot.randomized(mScreenRect);

      // penalty
      mScore -= 50;
      if (mScore < 0) {
        mScore = 0;
      }
    }

    if (mRobot.isDead()) {
      if (mScreenRect.width() > 0 && mScreenRect.height() > 0) {
        mRobot.randomized(mScreenRect);
      }
    }
  }
Exemplo n.º 28
0
  private float[] pointToSatVal(float x, float y) {

    final RectF rect = mSatValRect;
    float[] result = new float[2];

    float width = rect.width();
    float height = rect.height();

    if (x < rect.left) {
      x = 0f;
    } else if (x > rect.right) {
      x = width;
    } else {
      x = x - rect.left;
    }

    if (y < rect.top) {
      y = 0f;
    } else if (y > rect.bottom) {
      y = height;
    } else {
      y = y - rect.top;
    }

    result[0] = 1.f / width * x;
    result[1] = 1.f - (1.f / height * y);

    return result;
  }
 protected void onDraw(Canvas canvas) {
   super.onDraw(canvas);
   // Draw the rim
   canvas.drawArc(circleBounds, 360, 360, false, rimPaint);
   // Draw the bar
   if (isSpinning) {
     canvas.drawArc(circleBounds, progress - 90, barLength, false, barPaint);
   } else {
     canvas.drawArc(circleBounds, -90, progress, false, barPaint);
   }
   // Draw the inner circle
   canvas.drawCircle(
       (circleBounds.width() / 2) + rimWidth + paddingLeft,
       (circleBounds.height() / 2) + rimWidth + paddingTop,
       circleRadius,
       circlePaint);
   // Draw the text (attempts to center it horizontally and vertically)
   int offsetNum = 0;
   for (String s : splitText) {
     float offset = textPaint.measureText(s) / 2;
     canvas.drawText(
         s,
         this.getWidth() / 2 - offset,
         this.getHeight() / 2
             + (textSize * (offsetNum))
             - ((splitText.length - 1) * (textSize / 2)),
         textPaint);
     offsetNum++;
   }
 }
  private void interpolate(View view1, View view2, float interpolation) {
    getOnScreenRect(mRect1, view1);
    getOnScreenRect(mRect2, view2);

    float scaleX = 1.0F + interpolation * (mRect2.width() / mRect1.width() - 1.0F);
    float scaleY = 1.0F + interpolation * (mRect2.height() / mRect1.height() - 1.0F);
    float translationX =
        0.5F * (interpolation * (mRect2.left + mRect2.right - mRect1.left - mRect1.right));
    float translationY =
        0.5F * (interpolation * (mRect2.top + mRect2.bottom - mRect1.top - mRect1.bottom));

    view1.setTranslationX(translationX);
    view1.setTranslationY(translationY - mHeader.getTranslationY());
    view1.setScaleX(scaleX);
    view1.setScaleY(scaleY);
  }