bitmapshader和canvas.scaling android

时间:2013-03-03 10:00:56

标签: android bitmap android-canvas paint

是否有人对以下问题有提示或解释? 我画了一个带有位图的路径。当画布没有缩放时,它看起来不错(第一张图片)。 当我缩放(放大)时,位图处理器不会被缩放并且看起来非常难看。我在放大后尝试了几个重新创建bitmapshader的东西,但没有成功:-(。有人有提示吗?

没有缩放它看起来不错:

enter image description here

缩放时看起来很难看:

enter image description here

代码:

    canvas.scale(scalex, scaley);
    canvas.translate(itranslatex, itranslatey);


    fillBMP = makePatternCross(fscalex, 1, Color.GREEN/*,fscalex,fscaley*/);
    fillBMPshader = new BitmapShader(fillBMP, BitmapShader.TileMode.REPEAT, BitmapShader.TileMode.REPEAT);
    paintshader = new Paint();
    paintshader.setShader(fillBMPshader);   


    canvas.drawPath(cpath.path, paintshader);



private static Bitmap makePatternCross(float fSize, float fStrokewith,int iColor) {
    Log.v("Create Patter makePatternCross","makePatternCross");

    float fBitmapSizeOrig = 10;
    fBitmapSizeOrig=fBitmapSizeOrig*fSize;
    Bitmap bm = Bitmap.createBitmap((int)fBitmapSizeOrig,(int) fBitmapSizeOrig,Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bm);
    //c.scale(200, 200);
    c.drawColor(Color.WHITE);
    Paint p = new Paint();
    p.setColor(iColor);
    //p.setStrokeWidth(iStrokewith);
    p.setStrokeWidth(fStrokewith/fSize);
    p.setStrokeWidth((float) 0.000001);
    c.drawLine(0, 0, fBitmapSizeOrig, fBitmapSizeOrig, p);
    c.drawLine(0, fBitmapSizeOrig, fBitmapSizeOrig, 0, p);

    if (fSize != 1) {
        int iNewSize = (int) (( fBitmapSizeOrig) * fSize);
        bm = Bitmap.createScaledBitmap(bm, iNewSize, iNewSize, false);
    }

    int width = bm.getWidth();
    int height = bm.getHeight();
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            if (bm.getPixel(x, y) == Color.WHITE) {
                bm.setPixel(x, y, Color.TRANSPARENT);
            } else {
                // bm.setPixel(x, y, bm.getPixel(x, y));
            }
        }
    }
    return bm;
}

1 个答案:

答案 0 :(得分:2)

不确定这是否是你想要的。但是如果使用矩阵来缩放位图,它会保留比正常缩放更高的质量。

Matrix matrix = new Matrix();
matrix.postScale(desiredScale, desiredScale);
Bitmap scaledBitmap = Bitmap.createBitmap(sampledSrcBitmap, 0, 0, sampledSrcBitmap.getWidth(), sampledSrcBitmap.getHeight(), matrix, true);

此外,当从较低分辨率转到较高分辨率时,您也可以尝试这样做:

Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);