public DrawView(Context context) {
      super(context);

      rect = new Rect(0, 0, 100, 100);
      rect1 = new Rect(0, 0, 500, 200);
      p = new Paint(Paint.ANTI_ALIAS_FLAG);
      picture = new Picture();

      Canvas canvas = picture.beginRecording(300, 300);

      p.setColor(Color.GREEN);
      canvas.drawCircle(150, 100, 80, p);

      p.setColor(Color.BLUE);
      canvas.drawRect(20, 70, 150, 200, p);

      p.setColor(Color.RED);
      path = new Path();
      path.moveTo(170, 80);
      path.lineTo(240, 210);
      path.lineTo(100, 210);
      path.close();
      canvas.drawPath(path, p);

      picture.endRecording();
    }
Exemplo n.º 2
0
 /**
  * Returns a bitmap of a given WebView.
  *
  * @param webView the webView to save a bitmap from
  * @return a bitmap of the given web view
  */
 private Bitmap getBitmapOfWebView(final WebView webView) {
   Picture picture = webView.capturePicture();
   Bitmap b =
       Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
   Canvas c = new Canvas(b);
   picture.draw(c);
   return b;
 }
Exemplo n.º 3
0
 public Picture getContourPicture() {
   Picture p = new Picture();
   Canvas canvas = p.beginRecording(mWidth, mHeight);
   if (mPath != null) {
     canvas.drawPath(mPath, mPaint);
   }
   p.endRecording();
   return p;
 }
 public PictureBitmapTextureAtlasSource(
     final Picture pPicture, final int pTextureX, final int pTextureY, final float pScale) {
   this(
       pPicture,
       pTextureX,
       pTextureY,
       Math.round(pPicture.getWidth() * pScale),
       Math.round(pPicture.getHeight() * pScale));
 }
Exemplo n.º 5
0
    public SampleView(Context context) {
      super(context);
      setFocusable(true);
      setFocusableInTouchMode(true);

      mPicture = new Picture();
      drawSomething(mPicture.beginRecording(200, 100));
      mPicture.endRecording();

      mDrawable = new PictureDrawable(mPicture);
    }
  private void createCompassRosePicture() {
    // Paint design of north triangle (it's common to paint north in red color)
    final Paint northPaint = new Paint();
    northPaint.setColor(0xFFA00000);
    northPaint.setAntiAlias(true);
    northPaint.setStyle(Style.FILL);
    northPaint.setAlpha(220);

    // Paint design of south triangle (black)
    final Paint southPaint = new Paint();
    southPaint.setColor(Color.BLACK);
    southPaint.setAntiAlias(true);
    southPaint.setStyle(Style.FILL);
    southPaint.setAlpha(220);

    // Create a little white dot in the middle of the compass rose
    final Paint centerPaint = new Paint();
    centerPaint.setColor(Color.WHITE);
    centerPaint.setAntiAlias(true);
    centerPaint.setStyle(Style.FILL);
    centerPaint.setAlpha(220);

    // final int picBorderWidthAndHeight = (int) ((mCompassRadius + 5) * 2 * mScale);
    final int picBorderWidthAndHeight = (int) ((mCompassRadius + 5) * 2);
    final int center = picBorderWidthAndHeight / 2;

    final Canvas canvas =
        mCompassRose.beginRecording(picBorderWidthAndHeight, picBorderWidthAndHeight);

    // Blue triangle pointing north
    final Path pathNorth = new Path();
    pathNorth.moveTo(center, center - (mCompassRadius - 3) * mScale);
    pathNorth.lineTo(center + 4 * mScale, center);
    pathNorth.lineTo(center - 4 * mScale, center);
    pathNorth.lineTo(center, center - (mCompassRadius - 3) * mScale);
    pathNorth.close();
    canvas.drawPath(pathNorth, northPaint);

    // Red triangle pointing south
    final Path pathSouth = new Path();
    pathSouth.moveTo(center, center + (mCompassRadius - 3) * mScale);
    pathSouth.lineTo(center + 4 * mScale, center);
    pathSouth.lineTo(center - 4 * mScale, center);
    pathSouth.lineTo(center, center + (mCompassRadius - 3) * mScale);
    pathSouth.close();
    canvas.drawPath(pathSouth, southPaint);

    // Draw a little white dot in the middle
    canvas.drawCircle(center, center, 2, centerPaint);

    mCompassRose.endRecording();
  }
Exemplo n.º 7
0
    @Override
    protected void onDraw(Canvas canvas) {
      canvas.drawColor(Color.WHITE);

      canvas.drawPicture(mPicture);

      canvas.drawPicture(mPicture, new RectF(0, 100, getWidth(), 200));

      mDrawable.setBounds(0, 200, getWidth(), 300);
      mDrawable.draw(canvas);

      ByteArrayOutputStream os = new ByteArrayOutputStream();
      mPicture.writeToStream(os);
      InputStream is = new ByteArrayInputStream(os.toByteArray());
      canvas.translate(0, 300);
      canvas.drawPicture(Picture.createFromStream(is));
    }
  @Override
  public Bitmap onLoadBitmap(final Config pBitmapConfig) {
    final Picture picture = this.mPicture;
    if (picture == null) {
      Debug.e("Failed loading Bitmap in " + this.getClass().getSimpleName() + ".");
      return null;
    }

    final Bitmap bitmap =
        Bitmap.createBitmap(this.mTextureWidth, this.mTextureHeight, pBitmapConfig);
    final Canvas canvas = new Canvas(bitmap);

    final float scaleX = (float) this.mTextureWidth / this.mPicture.getWidth();
    final float scaleY = (float) this.mTextureHeight / this.mPicture.getHeight();
    canvas.scale(scaleX, scaleY, 0, 0);

    picture.draw(canvas);

    return bitmap;
  }
Exemplo n.º 9
0
  @Override
  public void draw(final Canvas c, final MapView mapView, final boolean shadow) {

    if (shadow) {
      return;
    }

    // If map views is animating, don't update, scale will be wrong.
    if (mapView.isAnimating()) {
      return;
    }

    final int zoomLevel = mapView.getZoomLevel();

    if (zoomLevel >= minZoom) {
      final Projection projection = mapView.getProjection();

      if (projection == null) {
        return;
      }

      final IGeoPoint center = projection.fromPixels((screenWidth / 2), screenHeight / 2);
      if (zoomLevel != lastZoomLevel
          || (int) (center.getLatitudeE6() / 1E6) != (int) (lastLatitude / 1E6)) {
        lastZoomLevel = zoomLevel;
        lastLatitude = center.getLatitudeE6();
        createScaleBarPicture(mapView);
      }

      mBounds.set(projection.getScreenRect());
      mBounds.offset((int) xOffset, (int) yOffset);

      mBounds.set(
          mBounds.left,
          mBounds.top,
          mBounds.left + scaleBarPicture.getWidth(),
          mBounds.top + scaleBarPicture.getHeight());
      c.drawPicture(scaleBarPicture, mBounds);
    }
  }
  public MxmMyLocationOverlay(
      final Context ctx,
      final MapView mapView,
      final Activity mActivity,
      final ResourceProxy pResourceProxy) {
    super(pResourceProxy);
    mMapView = mapView;
    thisActivity = mActivity;
    sogTxt = (TextView) thisActivity.findViewById(R.id.sog);
    cogTxt = (TextView) thisActivity.findViewById(R.id.cog);
    btnFollow = (Button) thisActivity.findViewById(R.id.btnFollow);
    mLocationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
    mSensorManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE);
    final WindowManager windowManager =
        (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
    mDisplay = windowManager.getDefaultDisplay();

    mMapController = mapView.getController();
    mCirclePaint.setARGB(0, 100, 100, 255);
    mCirclePaint.setAntiAlias(true);

    NULLDIRECTION_ICON = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.circle);
    DIRECTION_ARROW = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.ship);

    NULLDIRECTION_ICON_CENTER_X = NULLDIRECTION_ICON.getWidth() / 2 - 0.5f;
    NULLDIRECTION_ICON_CENTER_Y = NULLDIRECTION_ICON.getHeight() / 2 - 0.5f;

    DIRECTION_ARROW_CENTER_X = DIRECTION_ARROW.getWidth() / 2 - 0.5f;
    DIRECTION_ARROW_CENTER_Y = DIRECTION_ARROW.getHeight() / 2 - 0.5f;

    createCompassFramePicture();
    createCompassRosePicture();

    COMPASS_FRAME_CENTER_X = mCompassFrame.getWidth() / 2 - 0.5f;
    COMPASS_FRAME_CENTER_Y = mCompassFrame.getHeight() / 2 - 0.5f;
    COMPASS_ROSE_CENTER_X = mCompassRose.getWidth() / 2 - 0.5f;
    COMPASS_ROSE_CENTER_Y = mCompassRose.getHeight() / 2 - 0.5f;
  }
  /**
   * Create a screen shot for the current view.
   *
   * @return A bitmap of the screen shot.
   */
  private Bitmap createScreenshot() {
    Picture thumbnail = mWebView.capturePicture();
    if (thumbnail == null) {
      return null;
    }

    float density = mContext.getResources().getDisplayMetrics().density;

    int thumbnailWidth = (int) (Constants.BOOKMARK_THUMBNAIL_WIDTH_FACTOR * density);
    int thumbnailHeight = (int) (Constants.BOOKMARK_THUMBNAIL_HEIGHT_FACTOR * density);

    Bitmap bm = Bitmap.createBitmap(thumbnailWidth, thumbnailHeight, Bitmap.Config.ARGB_4444);

    Canvas canvas = new Canvas(bm);

    if (thumbnail.getWidth() > 0) {
      float scaleFactor = (float) thumbnailWidth / (float) thumbnail.getWidth();
      canvas.scale(scaleFactor, scaleFactor);
    }

    thumbnail.draw(canvas);
    return bm;
  }
Exemplo n.º 12
0
  public void captureRandCodePic(int randcodex, int randcodey) {
    try {
      Picture pic = wv.capturePicture();
      Bitmap bmp = Bitmap.createBitmap(pic.getWidth(), pic.getHeight(), Config.ARGB_8888);
      Canvas canvas = new Canvas(bmp);
      pic.draw(canvas);
      Bitmap code = Bitmap.createBitmap(293, 190, Config.ARGB_8888);
      Canvas canCode = new Canvas(code);
      canCode.drawBitmap(
          bmp,
          new Rect(
              randcodex * rate,
              randcodey * rate,
              randcodex * rate + 293 * rate,
              randcodey * rate + 190 * rate),
          new Rect(0, 0, 293, 190),
          null);
      BitmapUtils.savePicture(
          code, Environment.getExternalStorageDirectory().getAbsolutePath(), "capture.bmp");
    } catch (Exception e) {

    }
  }
  private void createCompassFramePicture() {
    // The inside of the compass is white and transparent
    final Paint innerPaint = new Paint();
    innerPaint.setColor(Color.WHITE);
    innerPaint.setAntiAlias(true);
    innerPaint.setStyle(Style.FILL);
    innerPaint.setAlpha(200);

    // The outer part (circle and little triangles) is gray and transparent
    final Paint outerPaint = new Paint();
    outerPaint.setColor(Color.GRAY);
    outerPaint.setAntiAlias(true);
    outerPaint.setStyle(Style.STROKE);
    outerPaint.setStrokeWidth(2.0f);
    outerPaint.setAlpha(200);

    final int picBorderWidthAndHeight = (int) ((mCompassRadius + 5) * 2);
    final int center = picBorderWidthAndHeight / 2;

    final Canvas canvas =
        mCompassFrame.beginRecording(picBorderWidthAndHeight, picBorderWidthAndHeight);

    // draw compass inner circle and border
    canvas.drawCircle(center, center, mCompassRadius * mScale, innerPaint);
    canvas.drawCircle(center, center, mCompassRadius * mScale, outerPaint);

    // Draw little triangles north, south, west and east (don't move)
    // to make those move use "-bearing + 0" etc. (Note: that would mean to draw the triangles
    // in the onDraw() method)
    drawTriangle(canvas, center, center, mCompassRadius * mScale, 0, outerPaint);
    drawTriangle(canvas, center, center, mCompassRadius * mScale, 90, outerPaint);
    drawTriangle(canvas, center, center, mCompassRadius * mScale, 180, outerPaint);
    drawTriangle(canvas, center, center, mCompassRadius * mScale, 270, outerPaint);

    mCompassFrame.endRecording();
  }
  @Override
  public void onDraw(Canvas canvas) {
    // zooming animation
    if (scalingInterpolator.isActive()) {
      setCurrentScaling(scalingInterpolator.getCurrentValue());
    }

    // translation animation
    if (translationXInterpolator.isActive() || translationYInterpolator.isActive()) {
      float transX = currentTranslation[0];
      float transY = currentTranslation[1];
      if (translationXInterpolator.isActive()) {
        transX = translationXInterpolator.getCurrentValue();
      }
      if (translationYInterpolator.isActive()) {
        transY = translationYInterpolator.getCurrentValue();
      }
      setCurrentTranslation(transX, transY);
    }

    // center rotation animation
    if (centerRotationInterpolator.isActive()) {
      setCurrentCenterRotation(centerRotationInterpolator.getCurrentValue());
    }

    // calculate current viewport matrix if necessary
    if (getAndClearViewportFlag()) {
      recalculateViewportTransformation();
      recalculateSizeScale();
    }

    // clear background
    canvas.drawColor(0xFF000000);

    // draw audio scene
    canvas.setMatrix(viewportTransformation);
    synchronized (GlobalData.audioScene) {
      GlobalData.audioScene.draw(canvas, currentInverseScaling);
    }

    // reset matrix
    canvas.setMatrix(null);

    // draw size scale
    sizeScalePicture.draw(canvas);
  }
 @Override
 public void endElement(String namespaceURI, String localName, String qName)
     throws SAXException {
   if (localName.equals("svg")) {
     picture.endRecording();
   } else if (localName.equals("linearGradient")) {
     if (gradient.id != null) {
       if (gradient.xlink != null) {
         Gradient parent = gradientRefMap.get(gradient.xlink);
         if (parent != null) {
           gradient = parent.createChild(gradient);
         }
       }
       int[] colors = new int[gradient.colors.size()];
       for (int i = 0; i < colors.length; i++) {
         colors[i] = gradient.colors.get(i);
       }
       float[] positions = new float[gradient.positions.size()];
       for (int i = 0; i < positions.length; i++) {
         positions[i] = gradient.positions.get(i);
       }
       LinearGradient g =
           new LinearGradient(
               gradient.x1,
               gradient.y1,
               gradient.x2,
               gradient.y2,
               colors,
               positions,
               Shader.TileMode.CLAMP);
       if (gradient.matrix != null) {
         g.setLocalMatrix(gradient.matrix);
       }
       gradientMap.put(gradient.id, g);
       gradientRefMap.put(gradient.id, gradient);
     }
   } else if (localName.equals("radialGradient")) {
     if (gradient.id != null) {
       if (gradient.xlink != null) {
         Gradient parent = gradientRefMap.get(gradient.xlink);
         if (parent != null) {
           gradient = parent.createChild(gradient);
         }
       }
       int[] colors = new int[gradient.colors.size()];
       for (int i = 0; i < colors.length; i++) {
         colors[i] = gradient.colors.get(i);
       }
       float[] positions = new float[gradient.positions.size()];
       for (int i = 0; i < positions.length; i++) {
         positions[i] = gradient.positions.get(i);
       }
       if (gradient.xlink != null) {
         Gradient parent = gradientRefMap.get(gradient.xlink);
         if (parent != null) {
           gradient = parent.createChild(gradient);
         }
       }
       RadialGradient g =
           new RadialGradient(
               gradient.x,
               gradient.y,
               gradient.radius,
               colors,
               positions,
               Shader.TileMode.CLAMP);
       if (gradient.matrix != null) {
         g.setLocalMatrix(gradient.matrix);
       }
       gradientMap.put(gradient.id, g);
       gradientRefMap.put(gradient.id, gradient);
     }
   } else if (localName.equals("g")) {
     if (boundsMode) {
       boundsMode = false;
     }
     // Break out of hidden mode
     if (hidden) {
       hiddenLevel--;
       // Util.debug("Hidden down: " + hiddenLevel);
       if (hiddenLevel == 0) {
         hidden = false;
       }
     }
     // Clear gradient map
     gradientMap.clear();
     popTransform();
   }
 }
 @Override
 public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
     throws SAXException {
   // Reset paint opacity
   paint.setAlpha(255);
   // Ignore everything but rectangles in bounds mode
   if (boundsMode) {
     if (localName.equals("rect")) {
       Float x = getFloatAttr("x", atts);
       if (x == null) {
         x = 0f;
       }
       Float y = getFloatAttr("y", atts);
       if (y == null) {
         y = 0f;
       }
       Float width = getFloatAttr("width", atts);
       Float height = getFloatAttr("height", atts);
       bounds = new RectF(x, y, x + width, y + height);
     }
     return;
   }
   if (localName.equals("svg")) {
     int width = (int) Math.ceil(getFloatAttr("width", atts));
     int height = (int) Math.ceil(getFloatAttr("height", atts));
     canvas = picture.beginRecording(width, height);
   } else if (localName.equals("defs")) {
     // Ignore
   } else if (localName.equals("linearGradient")) {
     gradient = doGradient(true, atts);
   } else if (localName.equals("radialGradient")) {
     gradient = doGradient(false, atts);
   } else if (localName.equals("stop")) {
     if (gradient != null) {
       float offset = getFloatAttr("offset", atts);
       String styles = getStringAttr("style", atts);
       StyleSet styleSet = new StyleSet(styles);
       String colorStyle = styleSet.getStyle("stop-color");
       int color = Color.BLACK;
       if (colorStyle != null) {
         if (colorStyle.startsWith("#")) {
           color = Integer.parseInt(colorStyle.substring(1), 16);
         } else {
           color = Integer.parseInt(colorStyle, 16);
         }
       }
       String opacityStyle = styleSet.getStyle("stop-opacity");
       if (opacityStyle != null) {
         float alpha = Float.parseFloat(opacityStyle);
         int alphaInt = Math.round(255 * alpha);
         color |= (alphaInt << 24);
       } else {
         color |= 0xFF000000;
       }
       gradient.positions.add(offset);
       gradient.colors.add(color);
     }
   } else if (localName.equals("g")) {
     // Check to see if this is the "bounds" layer
     if ("bounds".equalsIgnoreCase(getStringAttr("id", atts))) {
       boundsMode = true;
     }
     if (hidden) {
       hiddenLevel++;
       // Util.debug("Hidden up: " + hiddenLevel);
     }
     // Go in to hidden mode if display is "none"
     if ("none".equals(getStringAttr("display", atts))) {
       if (!hidden) {
         hidden = true;
         hiddenLevel = 1;
         // Util.debug("Hidden up: " + hiddenLevel);
       }
     }
     pushTransform(atts);
   } else if (!hidden && localName.equals("rect")) {
     Float x = getFloatAttr("x", atts);
     if (x == null) {
       x = 0f;
     }
     Float y = getFloatAttr("y", atts);
     if (y == null) {
       y = 0f;
     }
     Float width = getFloatAttr("width", atts);
     Float height = getFloatAttr("height", atts);
     Float rx = getFloatAttr("rx", atts);
     Float ry = getFloatAttr("ry", atts);
     if (rx == null) {
       if (ry == null) rx = ry = 0f;
       else rx = ry;
     } else {
       if (ry == null) ry = rx;
     }
     if (rx > width / 2) rx = width * 0.5f;
     if (ry > height / 2) ry = height * 0.5f;
     pushTransform(atts);
     Properties props = new Properties(atts);
     if (doFill(props, gradientMap)) {
       doLimits(x, y, width, height);
       if (rx > 0 && ry > 0)
         canvas.drawRoundRect(new RectF(x, y, x + width, y + height), rx, ry, paint);
       else canvas.drawRect(x, y, x + width, y + height, paint);
     }
     if (doStroke(props, gradientMap)) {
       if (rx > 0 && ry > 0)
         canvas.drawRoundRect(new RectF(x, y, x + width, y + height), rx, ry, paint);
       else canvas.drawRect(x, y, x + width, y + height, paint);
     }
     popTransform();
   } else if (!hidden && localName.equals("line")) {
     Float x1 = getFloatAttr("x1", atts);
     Float x2 = getFloatAttr("x2", atts);
     Float y1 = getFloatAttr("y1", atts);
     Float y2 = getFloatAttr("y2", atts);
     Properties props = new Properties(atts);
     if (doStroke(props, gradientMap)) {
       pushTransform(atts);
       doLimits(x1, y1);
       doLimits(x2, y2);
       canvas.drawLine(x1, y1, x2, y2, paint);
       popTransform();
     }
   } else if (!hidden && localName.equals("circle")) {
     Float centerX = getFloatAttr("cx", atts);
     Float centerY = getFloatAttr("cy", atts);
     Float radius = getFloatAttr("r", atts);
     if (centerX != null && centerY != null && radius != null) {
       pushTransform(atts);
       Properties props = new Properties(atts);
       if (doFill(props, gradientMap)) {
         doLimits(centerX - radius, centerY - radius);
         doLimits(centerX + radius, centerY + radius);
         canvas.drawCircle(centerX, centerY, radius, paint);
       }
       if (doStroke(props, gradientMap)) {
         canvas.drawCircle(centerX, centerY, radius, paint);
       }
       popTransform();
     }
   } else if (!hidden && localName.equals("ellipse")) {
     Float centerX = getFloatAttr("cx", atts);
     Float centerY = getFloatAttr("cy", atts);
     Float radiusX = getFloatAttr("rx", atts);
     Float radiusY = getFloatAttr("ry", atts);
     if (centerX != null && centerY != null && radiusX != null && radiusY != null) {
       pushTransform(atts);
       Properties props = new Properties(atts);
       rect.set(centerX - radiusX, centerY - radiusY, centerX + radiusX, centerY + radiusY);
       if (doFill(props, gradientMap)) {
         doLimits(centerX - radiusX, centerY - radiusY);
         doLimits(centerX + radiusX, centerY + radiusY);
         canvas.drawOval(rect, paint);
       }
       if (doStroke(props, gradientMap)) {
         canvas.drawOval(rect, paint);
       }
       popTransform();
     }
   } else if (!hidden && (localName.equals("polygon") || localName.equals("polyline"))) {
     NumberParse numbers = getNumberParseAttr("points", atts);
     if (numbers != null) {
       Path p = new Path();
       ArrayList<Float> points = numbers.numbers;
       if (points.size() > 1) {
         pushTransform(atts);
         Properties props = new Properties(atts);
         p.moveTo(points.get(0), points.get(1));
         for (int i = 2; i < points.size(); i += 2) {
           float x = points.get(i);
           float y = points.get(i + 1);
           p.lineTo(x, y);
         }
         // Don't close a polyline
         if (localName.equals("polygon")) {
           p.close();
         }
         if (doFill(props, gradientMap)) {
           doLimits(p);
           canvas.drawPath(p, paint);
         }
         if (doStroke(props, gradientMap)) {
           canvas.drawPath(p, paint);
         }
         popTransform();
       }
     }
   } else if (!hidden && localName.equals("path")) {
     Path p = doPath(getStringAttr("d", atts));
     pushTransform(atts);
     Properties props = new Properties(atts);
     if (doFill(props, gradientMap)) {
       doLimits(p);
       canvas.drawPath(p, paint);
     }
     if (doStroke(props, gradientMap)) {
       canvas.drawPath(p, paint);
     }
     popTransform();
   } else if (!hidden) {
     //                Log.d(TAG, "UNRECOGNIZED SVG COMMAND: " + localName);
   }
 }
 public PictureBitmapTextureAtlasSource(
     final Picture pPicture, final int pTextureX, final int pTextureY) {
   this(pPicture, pTextureX, pTextureY, pPicture.getWidth(), pPicture.getHeight());
 }
  private void recalculateSizeScale() {
    float sizeInMeters;
    double upperBoundingPowOfTen;
    double scaleLengthInMeters;
    float scaleLengthInPixels;
    float scaleStartInPixels = 20.0f;

    // get size of short display edge in meters
    sizeInMeters =
        inverseViewportTransformation.mapRadius(
            Math.min(getWidth(), getHeight()) - scaleStartInPixels * 2.0f);

    // find upper bounding power of ten (0.01, 0.1, 1, 10, 100, etc.)
    if (sizeInMeters > 1.0) {
      upperBoundingPowOfTen = 10.0;
      while (true) {
        if (sizeInMeters / upperBoundingPowOfTen > 1.0f) {
          upperBoundingPowOfTen *= 10.0;
        } else {
          break;
        }
      }
    } else {
      upperBoundingPowOfTen = 1.0;
      while (true) {
        if (sizeInMeters / upperBoundingPowOfTen >= 0.1f) {
          break;
        } else {
          upperBoundingPowOfTen *= 0.1;
        }
      }
    }

    // map to subdivisions
    scaleLengthInMeters = sizeInMeters / upperBoundingPowOfTen;
    if (scaleLengthInMeters > 0.75) scaleLengthInMeters = 0.75;
    else if (scaleLengthInMeters > 0.5) scaleLengthInMeters = 0.5;
    else if (scaleLengthInMeters > 0.3) scaleLengthInMeters = 0.3;
    else if (scaleLengthInMeters > 0.2) scaleLengthInMeters = 0.2;
    else if (scaleLengthInMeters > 0.15) scaleLengthInMeters = 0.15;
    else scaleLengthInMeters = 0.1;
    scaleLengthInMeters *= upperBoundingPowOfTen;

    // get corresponding scale length in pixels
    scaleLengthInPixels = viewportTransformation.mapRadius((float) scaleLengthInMeters);

    // generate picture
    Canvas canvas = sizeScalePicture.beginRecording(0, 0);
    float y = getHeight() - 5.0f;
    float xHalf = scaleStartInPixels + scaleLengthInPixels / 2.0f;
    float scaleEndInPixels = scaleStartInPixels + scaleLengthInPixels;

    SourcesView.paint.setARGB(255, 255, 255, 255);
    SourcesView.paint.setAntiAlias(true);
    canvas.drawLine(scaleStartInPixels - 1.0f, y, scaleEndInPixels + 1.0f, y, SourcesView.paint);
    canvas.drawLine(scaleStartInPixels, y, scaleStartInPixels, y - 5.0f, SourcesView.paint);
    canvas.drawLine(xHalf, y, xHalf, y - 5.0f, SourcesView.paint);
    canvas.drawLine(scaleEndInPixels, y, scaleEndInPixels, y - 5.0f, SourcesView.paint);

    canvas.drawText("0m", scaleStartInPixels, y - 8.0f, SourcesView.paint);
    canvas.drawText(
        String.valueOf((float) scaleLengthInMeters / 2.0f) + "m",
        xHalf,
        y - 8.0f,
        SourcesView.paint);
    canvas.drawText(
        String.valueOf((float) scaleLengthInMeters) + "m",
        scaleEndInPixels,
        y - 8.0f,
        SourcesView.paint);
  }
Exemplo n.º 19
0
  private void createScaleBarPicture(final MapView mapView) {
    // We want the scale bar to be as long as the closest round-number miles/kilometers
    // to 1-inch at the latitude at the current center of the screen.

    projection = mapView.getProjection();

    if (projection == null) {
      return;
    }

    // Two points, 1-inch apart in x/latitude, centered on screen
    IGeoPoint p1 = projection.fromPixels((screenWidth / 2) - (xdpi / 2), screenHeight / 2);
    IGeoPoint p2 = projection.fromPixels((screenWidth / 2) + (xdpi / 2), screenHeight / 2);

    final int xMetersPerInch = ((GeoPoint) p1).distanceTo(p2);

    p1 = projection.fromPixels(screenWidth / 2, (screenHeight / 2) - (ydpi / 2));
    p2 = projection.fromPixels(screenWidth / 2, (screenHeight / 2) + (ydpi / 2));

    final int yMetersPerInch = ((GeoPoint) p1).distanceTo(p2);

    final Canvas canvas = scaleBarPicture.beginRecording((int) xdpi, (int) ydpi);

    if (latitudeBar) {
      final String xMsg = scaleBarLengthText(xMetersPerInch, imperial, nautical);
      final Rect xTextRect = new Rect();
      textPaint.getTextBounds(xMsg, 0, xMsg.length(), xTextRect);

      final int textSpacing = (int) (xTextRect.height() / 5.0);

      canvas.drawRect(0, 0, xdpi, lineWidth, barPaint);
      canvas.drawRect(
          xdpi, 0, xdpi + lineWidth, xTextRect.height() + lineWidth + textSpacing, barPaint);

      if (!longitudeBar) {
        canvas.drawRect(0, 0, lineWidth, xTextRect.height() + lineWidth + textSpacing, barPaint);
      }

      canvas.drawText(
          xMsg,
          xdpi / 2 - xTextRect.width() / 2,
          xTextRect.height() + lineWidth + textSpacing,
          textPaint);
    }

    if (longitudeBar) {
      final String yMsg = scaleBarLengthText(yMetersPerInch, imperial, nautical);
      final Rect yTextRect = new Rect();
      textPaint.getTextBounds(yMsg, 0, yMsg.length(), yTextRect);

      final int textSpacing = (int) (yTextRect.height() / 5.0);

      canvas.drawRect(0, 0, lineWidth, ydpi, barPaint);
      canvas.drawRect(
          0, ydpi, yTextRect.height() + lineWidth + textSpacing, ydpi + lineWidth, barPaint);

      if (!latitudeBar) {
        canvas.drawRect(0, 0, yTextRect.height() + lineWidth + textSpacing, lineWidth, barPaint);
      }

      final float x = yTextRect.height() + lineWidth + textSpacing;
      final float y = ydpi / 2 + yTextRect.width() / 2;

      canvas.rotate(-90, x, y);
      canvas.drawText(yMsg, x, y + textSpacing, textPaint);
    }

    scaleBarPicture.endRecording();
  }
Exemplo n.º 20
0
  public void addRoutesInSvg(ArrayList<Route> routes) {
    SVG svg1 = SVGParser.getSVGFromResource(getResources(), R.raw.example_map);

    Picture picture1 = svg1.getPicture();

    this.imageHeight = picture1.getHeight();
    this.imageWidth = picture1.getWidth();
    InputStream inputStream = this.getResources().openRawResource(R.raw.example_map);

    InputStreamReader inputreader = new InputStreamReader(inputStream);
    BufferedReader buffreader = new BufferedReader(inputreader);
    String line;
    StringBuilder text = new StringBuilder();
    try {
      while ((line = buffreader.readLine()) != null) {

        //        		Log.d(TAG, "Line: "+count++ +": "+line);
        if (line.contains("</svg>")) {
          Log.d(TAG, "found closing tag");
          String pathSvgString = "";
          for (int i = 0; i < routes.size(); i++) {
            Route r = routes.get(i);
            float fromX = (float) (r.fromX * imageWidth);
            float fromY = (float) (r.fromY * imageHeight);

            float toX = (float) (r.toX * imageWidth);
            float toY = (float) (r.toY * imageHeight);
            pathSvgString =
                pathSvgString
                    + "<line x1=\""
                    + fromX
                    + "\" y1=\""
                    + fromY
                    + "\" x2=\""
                    + toX
                    + "\" y2=\""
                    + toY
                    + "\" style=\"stroke-dasharray: 0;stroke:#003399;stroke-width:3;\"/>\n";

            //	       			    Log.d(TAG, "Path Added: "+"<line x1=\""+fromX+"\" y1=\""+fromY+"\"
            // x2=\""+toX+"\" y2=\""+toY+"\" style=\"stroke-width:1;stroke:#003399;\"/>\n");
          }
          Log.d(TAG, "line before editing: " + line);

          line = line.replace("</svg>", pathSvgString + "</svg>");
          Log.d(TAG, "line after editing: " + line);
        }
        text.append(line + '\n');
      }
    } catch (IOException e) {
    }

    File file = new File("/sdcard/FileWriterTest.svg");
    try {
      if (file.exists()) file.delete();
      FileWriter writer = new FileWriter(file, true);
      writer.write(text.toString());
      writer.flush();
      writer.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
 @Override
 public int getIntrinsicWidth() {
   return mPicture != null ? mPicture.getWidth() : -1;
 }
 @Override
 public int getIntrinsicHeight() {
   return mPicture != null ? mPicture.getHeight() : -1;
 }