Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bitmapshader and canvas.scaling android

Does anyone have a hint or explanation for the following problem ? I draw a path with a bitmapshader. When canvas is not scaled, it looks good ( first picture ). When I scale into ( zooming in ) the bitmapshader is not be scaled and looks very ugly. I tried several things with recreating the bitmapshader after zooming in, but did not succeed :-(. Does anyone have a hint ?

No Scaling it looks good :

enter image description here

when scaling it looks ugly:

enter image description here

Code :

    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;
}
like image 712
mcfly soft Avatar asked Mar 03 '13 10:03

mcfly soft


1 Answers

Not sure exactly if this is what your looking for. But if you use a matrix to scale the bitmap it retains more quality than normal scaling.

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

Also when going from a lesser resolution to a higher you can try this as well:

Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);
like image 193
Brianjs Avatar answered Nov 18 '22 15:11

Brianjs