// ==============================================================================
  public final int[] renderGlyph(
      char glyph, Paint paint, android.graphics.Matrix matrix, Rect bounds) {
    Path p = new Path();
    paint.getTextPath(String.valueOf(glyph), 0, 1, 0.0f, 0.0f, p);

    RectF boundsF = new RectF();
    p.computeBounds(boundsF, true);
    matrix.mapRect(boundsF);

    boundsF.roundOut(bounds);
    bounds.left--;
    bounds.right++;

    final int w = bounds.width();
    final int h = bounds.height();

    Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

    Canvas c = new Canvas(bm);
    matrix.postTranslate(-bounds.left, -bounds.top);
    c.setMatrix(matrix);
    c.drawPath(p, paint);

    final int sizeNeeded = w * h;
    if (cachedRenderArray.length < sizeNeeded) cachedRenderArray = new int[sizeNeeded];

    bm.getPixels(cachedRenderArray, 0, w, 0, 0, w, h);
    bm.recycle();
    return cachedRenderArray;
  }
  private void drawFooter(Canvas canvas) {
    final ZLView view = ZLApplication.Instance().getCurrentView();
    final ZLView.FooterArea footer = view.getFooterArea();

    if (footer == null) {
      myFooterBitmap = null;
      return;
    }

    if (myFooterBitmap != null
        && (myFooterBitmap.getWidth() != getWidth()
            || myFooterBitmap.getHeight() != footer.getHeight())) {
      myFooterBitmap = null;
    }
    if (myFooterBitmap == null) {
      myFooterBitmap = Bitmap.createBitmap(getWidth(), footer.getHeight(), Bitmap.Config.RGB_565);
    }
    final ZLAndroidPaintContext context =
        new ZLAndroidPaintContext(
            new Canvas(myFooterBitmap),
            getWidth(),
            footer.getHeight(),
            view.isScrollbarShown() ? getVerticalScrollbarWidth() : 0);
    footer.paint(context);
    canvas.drawBitmap(myFooterBitmap, 0, getHeight() - footer.getHeight(), myPaint);
  }
Exemple #3
0
  public static Bitmap[] load(String src, int width, int height) {
    Bitmap[] texture = null;
    InputStream is = null;
    AssetManager am = resources.getAssets();
    String[] nameOfBoxs = null;

    try {
      nameOfBoxs = am.list(src);
    } catch (IOException e) {
      Log.d("GraphicMaster", "Culd not find folder " + src);
    }

    Log.d("GraphicMaster", "NameOfBoxs.length = " + nameOfBoxs.length);
    texture = new Bitmap[nameOfBoxs.length];

    for (int i = 0; i < nameOfBoxs.length; i++) {
      try {
        is = resources.getAssets().open(src + "/" + nameOfBoxs[i]);

        texture[i] = BitmapFactory.decodeStream(is);

        /*	if(width > 0 && height > 0)
        texture[i] = Bitmap.createScaledBitmap(texture[i], width, height, false);*/

        Log.d(
            "GraphicMaster",
            "Scaled from "
                + new Integer((int) (texture[i].getWidth())).toString()
                + " "
                + new Integer((int) (texture[i].getHeight())).toString()
                + " to "
                + new Integer((int) (texture[i].getWidth() * GameSettings.widthM)).toString()
                + " "
                + new Integer((int) (GameSettings.heightM * texture[i].getWidth())).toString());

        Log.d(
            "GraphicMaster",
            "Scale"
                + new Double(GameSettings.widthM).toString()
                + " "
                + new Double(GameSettings.heightM).toString());

        if (GameSettings.widthM != 1 && GameSettings.heightM != 1)
          texture[i] =
              Bitmap.createScaledBitmap(
                  texture[i],
                  (int) (texture[i].getWidth() * GameSettings.widthM),
                  (int) (texture[i].getHeight() * GameSettings.heightM),
                  false);

      } catch (IOException e) {
        Log.d("GraphicMaster", "Culd not read: " + src + nameOfBoxs[i]);
      }
    }

    return texture;
  }
Exemple #4
0
  /**
   * 自定义的方法,简单绘制一些基本图形
   *
   * @param mCanvas 把图形画在mCanvas上
   */
  public void canvasMethod(Canvas mCanvas) {

    // 创建对应坐标的矩形区域
    RectF mArc = new RectF(mX, mY - 70, mX + 50, mY - 20);
    // 画填充弧,在矩形区域内,从弧的最右边开始,画270度,然后再通过连接圆心来填充
    mCanvas.drawArc(mArc, 0, 270, true, mPaint);

    // 获得icon的Bitmap对象
    Bitmap mBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
    // 画图片
    mCanvas.drawBitmap(mBitmap, mX, mY, mPaint);

    // 画圆,(x轴,y轴,半径,画笔)
    mCanvas.drawCircle(mX + 10, mY + 60, 10, mPaint);

    // 画一条线,(起点横坐标,起点纵坐标,终点横坐标,终点纵坐标,画笔)
    mCanvas.drawLine(mX, mY + 75, mX + 20, mY + 75, mPaint);
    // 画多条线,(坐标数组,画笔)坐标数组里每四个值构成一条线
    mCanvas.drawLines(
        new float[] {mX + 50, mY + 45, mX + 50, mY + 75, mX + 60, mY + 45, mX + 60, mY + 75},
        mPaint);

    // 创建对应矩形区域
    RectF mOval = new RectF(mX, mY + 80, mX + 60, mY + 110);
    // 画椭圆
    mCanvas.drawOval(mOval, mPaint);

    /*
     * Paint qPaint = new Paint(); qPaint.setColor(Color.RED);
     * mCanvas.drawPaint(qPaint);
     */

    // 重置Path里的所有路径
    mPath.reset();
    // 设置Path的起点
    mPath.moveTo(mX, mY + 120);
    // 第二个点
    mPath.lineTo(screenW - 10, mY + 120);
    // 第三个点
    mPath.lineTo(screenW - 10, mY + 150);
    // 画出路径,这里画的是三角形
    mCanvas.drawPath(mPath, mPaint);

    // 重置Path里的所有路径
    qPath.reset();
    // 设置Path的起点
    qPath.moveTo(qStartX, qStartY);
    // 设置贝塞尔曲线的控制点坐标和终点坐标
    qPath.quadTo(qControlX, qCOntrolY, qEndX, qEndY);
    // 画出贝塞尔曲线
    mCanvas.drawPath(qPath, qPaint);

    // 画点
    mCanvas.drawPoint(mX, mY + 155, qPaint);
    // 画多个点,坐标数组每两个值代表一个点的坐标
    mCanvas.drawPoints(new float[] {mX, mY + 160, mX + 5, mY + 160, mX + 5, mY + 160}, qPaint);

    // 画矩形
    mCanvas.drawRect(mX, mY + 170, mX + 100, mY + 220, mPaint);

    // 设置矩形区域
    RectF mRect = new RectF(mX, mY + 230, mX + 100, mY + 260);
    // 画圆角矩形,这个方法的第二第三个参数在后面有图讲解
    mCanvas.drawRoundRect(mRect, 10, 10, mPaint);

    // 画文本
    mCanvas.drawText("drawText", mX, mY + 290, mPaint);
    // 画文本,数组里每两个值代表文本的一个字符的坐标,数组的坐标可以比字符串里的字符多,但不可以少
    mCanvas.drawPosText(
        "哈哈你好",
        new float[] {mX, mY + 310, mX + 20, mY + 310, mX + 40, mY + 310, mX + 60, mY + 310},
        mPaint);

    // 重置Path
    tPath.reset();
    // 添加一个圆形路径,坐标,半径,方向(顺时针还是逆时针)
    tPath.addCircle(mX + 10, mY + 340, 10, Path.Direction.CW);
    // 画出路径
    mCanvas.drawPath(tPath, qPaint);
    // 把文本画在路径上,但不会画出路径
    mCanvas.drawTextOnPath("draw", tPath, 30, 0, mPaint);
  }
  private void stroke(Seria s, Canvas canvas) {
    mPlotPaint.setStyle(Paint.Style.STROKE);
    // draw line
    if (s.mWidth > 0) {
      // create the path
      mPath.reset();
      mPath.moveTo(calcX(s.x[0]), calcY(s.y[0]));
      for (int i = 1; i < s.sz(); i++) {
        mPath.lineTo(calcX(s.x[i]), calcY(s.y[i]));
      }
      // draw the path
      mPlotPaint.setColor(s.mColor);
      mPlotPaint.setStrokeWidth(s.mWidth);
      mPlotPaint.setPathEffect(s.mEffect);
      canvas.drawPath(mPath, mPlotPaint);
    }

    // draw images seria
    if (s.bms != null) {
      for (int i = 0; i < s.sz(); i++) {
        Bitmap bm;
        if (s.bms.length > 1) {
          bm = s.bms[i];
        } else {
          bm = s.bms[0];
        }
        float left = calcX(s.x[i]);
        float top = calcY(s.y[i]);
        if (s.hAlign == HALIGN.CENTER) {
          left -= bm.getWidth() / 2;
        } else if (s.hAlign == HALIGN.RIGHT) {
          left -= bm.getWidth();
        }
        if (s.vAlign == VALIGN.CENTER) {
          top -= bm.getHeight() / 2;
        } else if (s.vAlign == VALIGN.BOTTOM) {
          top -= bm.getHeight();
        }
        canvas.drawBitmap(s.bms[i], left, top, mPlotPaint);
      }
    }
    if (s.texts != null) {
      Align align = Align.CENTER;
      switch (s.hAlign) {
        case LEFT:
          align = Align.RIGHT;
        case RIGHT:
          align = Align.LEFT;
      }

      mPlotPaint.setTextAlign(align);
      mPlotPaint.setStrokeWidth(0);
      mPlotPaint.setColor(s.mColor);
      for (int i = 0; i < s.sz(); i++) {
        float x = calcX(s.x[i]);
        float y = calcY(s.y[i]);
        switch (s.vAlign) {
          case TOP:
            y -= mPlotPaint.getTextSize();
          case CENTER:
            y -= 0.5 * mPlotPaint.getTextSize();
        }
        if (s.deg != 0) {
          canvas.save();
          canvas.rotate(s.deg, x, y);
        }
        canvas.drawText(s.texts[i], x, y, mPlotPaint);
        if (s.deg != 0) {
          canvas.restore();
        }
      }
    }
  }
  public void run() {
    int x, y;
    Bitmap locTile;
    Canvas canvas;
    float useLon, useLat, useLonMax, useLatMax;

    System.gc();
    if ((useOverlay.tileX == 0) && (useOverlay.tileY == 0)) return;
    mapThreadRunning = true;
    try {
      if (locMap == null)
        locMap = Bitmap.createBitmap(tileWidth * 256, tileHeight * 256, Bitmap.Config.ARGB_8888);
    } catch (OutOfMemoryError oome) {
      return;
    }
    locMap.eraseColor(Color.BLACK);
    canvas = new Canvas(locMap);

    useLon =
        (GeoUtils.tilex2long(useOverlay.tileX, useOverlay.m_zoom)
                + GeoUtils.tilex2long(useOverlay.tileX + 1, useOverlay.m_zoom))
            / 2.0f;
    useLat =
        (GeoUtils.tiley2lat(useOverlay.tileY, useOverlay.m_zoom)
                + GeoUtils.tiley2lat(useOverlay.tileY + 1, useOverlay.m_zoom))
            / 2.0f;

    while (mScaleFactor >= 2.0) {
      mScaleFactor /= 2.0f;
      useOverlay.m_zoom++;
      if (useOverlay.m_zoom > 17) {
        useOverlay.m_zoom = 17;
        imgOffsetX -= (scrWidth - (scrWidth * 2.0)) / 2.0;
        imgOffsetY -= (scrHeight - (scrHeight * 2.0)) / 2.0;
      }
    }
    while (mScaleFactor <= 0.5) {
      mScaleFactor *= 2.0f;
      useOverlay.m_zoom--;
      if (useOverlay.m_zoom < 3) {
        useOverlay.m_zoom = 3;
        imgOffsetX -= (scrWidth - (scrWidth * 2.0)) / 2.0;
        imgOffsetY -= (scrHeight - (scrHeight * 2.0)) / 2.0;
      }
    }
    mScaleFactor = 1.0f;

    /*      if ((wasScaleOp) && (lonCenter!=0.0f) && (latCenter!=0.0f))
    {
       tileX=GeoUtils.long2tilex(lonCenter,m_zoom);
       tileY=GeoUtils.lat2tiley(latCenter,m_zoom);

       tileX-=(scrWidth / 256);
       tileY-=(scrWidth / 256);
    }
    else*/
    {
      useOverlay.tileX = GeoUtils.long2tilex(useLon, useOverlay.m_zoom);
      useOverlay.tileY = GeoUtils.lat2tiley(useLat, useOverlay.m_zoom);
    }
    useLon = GeoUtils.tilex2long(useOverlay.tileX - 1, useOverlay.m_zoom);
    useLat = GeoUtils.tiley2lat(useOverlay.tileY - 1, useOverlay.m_zoom);
    useLonMax = GeoUtils.tilex2long(useOverlay.tileX + tileWidth + 1, useOverlay.m_zoom);
    useLatMax = GeoUtils.tiley2lat(useOverlay.tileY + tileHeight + 1, useOverlay.m_zoom);

    /*      if (imgOffsetX>scrWidth)
    {
       imgOffsetX=scrWidth/2;
    }
    if (imgOffsetX<=-scrWidth)
    {
       imgOffsetX=-scrWidth/2;
    }
    if (imgOffsetY>scrHeight)
    {
       imgOffsetY=scrHeight/2;
    }
    if (imgOffsetY<=-scrHeight)
    {
       imgOffsetY=-scrHeight/2;
    }*/

    while (imgOffsetX > 0) {
      imgOffsetX -= 256.0f;
      useOverlay.tileX--;
    }
    while (imgOffsetX <= -256.0) {
      imgOffsetX += 256.0f;
      useOverlay.tileX++;
    }
    while (imgOffsetY > 0) {
      imgOffsetY -= 256.0f;
      useOverlay.tileY--;
    }
    while (imgOffsetY <= -256.0) {
      imgOffsetY += 256.0f;
      useOverlay.tileY++;
    }

    // calculate lat/lon of screen center point
    /*      {
       int useTileX=tileX,useTileY=tileY;

       x=(int)((scrWidth/2.0f)+imgOffsetX);
       y=(int)((scrHeight/2.0f)+imgOffsetY);

       useTileX+=(x / 256);
       x=x % 256;
       useTileY+=(y / 256);
       y=y % 256;

       float tileLon1=GeoUtils.tilex2long(useTileX,m_zoom);
       float tileLat1=GeoUtils.tiley2lat(useTileY,m_zoom);
       float tileLon2=GeoUtils.tilex2long(useTileX+1,m_zoom);
       float tileLat2=GeoUtils.tiley2lat(useTileY+1,m_zoom);

       lonCenter=(((tileLon2-tileLon1)/256.0f)*x)+tileLon1;
       latCenter=(((tileLat2-tileLat1)/256.0f)*y)+tileLat1;
    }*/

    matrix.setTranslate(imgOffsetX, imgOffsetY);
    breakMapThread = false;
    mapThreadRunning = false;
    for (x = 0; x < tileWidth; x++)
      for (y = 0; y < tileHeight; y++) {
        if (breakMapThread) return;
        locTile =
            geoUtils.loadMapTile(
                getContext(),
                useOverlay.tileX + x,
                useOverlay.tileY + y,
                useOverlay.m_zoom,
                allowNetAccess);
        if (locTile != null) canvas.drawBitmap(locTile, x * 256, y * 256, null);
        postInvalidate();
      }
    useOverlay.doDraw(canvas, useLon, useLonMax, useLat, useLatMax, locMap);
    postInvalidate();
    System.gc();
  }