Android基于SurfaceView缩小位图



因此,我试图根据表面视图的大小缩小位图,我编写了以下代码

public Bitmap scaleBitMap(int width, int height, Bitmap b){
    // scale down based on the size of the Surface view.
    // width and height = canvas.getHeight(), canvas.getWidth();
    // this should set the height and width of the bitmap based on a percentage 
    // the bit map is returned and then displayed by canvas.drawBitmap(scaleBitMap(canvas.getHeight(),canvas.getWidth(),bitmapHere)....
    Bitmap bitmap = null;
    try{
       bitmap =  Bitmap.createScaledBitmap(b, (height/100 * 10), (width/100 * 10), true);
    }catch(Exception e){
    }
    return bitmap;
}

好的,这段代码可以工作!返回缩放后的位图。

我不知道你想做什么,但我以前做过类似的事情。此代码缩放位图以匹配屏幕大小。也许它会对你有帮助。

public Bitmap scaleBitMap(int width, int height, Bitmap b){
    // scale down based on the size of the Surface view.
    // width and height = canvas.getHeight(), canvas.getWidth();
    // this should set the height and width of the bitmap based on a percentage 
    // the bit map is returned and then displayed by canvas.drawBitmap(scaleBitMap(canvas.getHeight(),canvas.getWidth(),bitmapHere)....
    Bitmap bitmap = null;
    try{
        float scaledvalues[] = scale(b.getWidth(), b.getHeight(), width, height);
        bitmap =  Bitmap.createScaledBitmap(b, scaledvalues[1], scaledvalues[0], true);
    }catch(Exception e){
    }
    return bitmap;
}
    public float[] scale(int bitmapWidth, int bitmapHeight, int viewWidth, int viewHeight){
        float scaledheight = -1f;
        float scaledwidth = -1f;
        float scaledheightpros = -1f;
        float scaledwidthpros = -1f;
        float finalheight = -1f;
        float finalwidth = -1f;
        if(bitmapHeight > viewHeight){
            scaledheight = bitmapHeight - viewHeight;
            float s = scaledheight*100f;
            scaledheightpros = s / 100f;
        }
        if(bitmapWidth > viewWidth){
            scaledwidth = bitmapWidth - viewWidth;
            float z = scaledwidth * 100f;
            scaledwidthpros = z / bitmapWidth;
        }
            if(scaledheightpros > scaledwidthpros){
                float a = bitmapHeight/100f;
                float b = bitmapWidth/100f;
                finalheight = bitmapHeight - (a * scaledheightpros);
                finalwidth = bitmapWidth - (b * scaledheightpros);
            }
            else{
                float a = bitmapHeight/100f;
                float b = bitmapWidth/100f;
                finalheight = bitmapHeight - (a * scaledwidthpros);
                finalwidth = bitmapWidth - (b * scaledwidthpros);
            }
        float array[] = {finalwidth, finalheight};
        return array;
    }

它工作…但也许我应该检查一下我的数学!下面是工作代码....

public Bitmap scaleBitMap(int width, int height, Bitmap b){
    // scale down based on the size of the Surface view.
    // width and height = canvas.getHeight(), canvas.getWidth();
    // this should set the height and width of the bitmap based on a percentage
    // the bit map is returned and then displayed by canvas.drawBitmap(scaleBitMap(canvas.getHeight(),canvas.getWidth(),bitmapHere)....
    Bitmap bitmap = null;
    try{
        bitmap = (Bitmap) createScaledBitmap(b, (height/100 * 22), (width/100 * 22), false);
    }catch(Exception e){
        Log.d("Bitmap","Issue: "+e);
    }
    return bitmap;
}

最新更新