2013-06-06 2 views
0

Так что я пытаюсь сократить масштабы растрового изображения на основе размера вида поверхности я написал следующий код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; 
} 

Ok так что этот код работает! возвращает масштабированное растровое изображение.

+0

вместо canvas.drawBitmap (scaleBitMap, ... называйте canvas.drawBitmap (б, матрица, краска) – pskink

+0

pskink, да что вероятно, будет работать, но для простоты из приложения ... не то, что я искал спасибо. – NathanEvans

ответ

1

Я не уверен, что вы пытаетесь сделать, но я уже делал подобное. Этот код масштабирует растровое изображение для соответствия экранам. Может быть, это поможет вам.

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; 
    } 
+0

Почему масштабировать, когда вы просто хотите нарисовать растровое изображение разного размера? Его потеря памяти, время процессора и батарея – pskink

+0

Ну, что я сделал, получается высоту и ширину SufaceView и уменьшать изображение на основе процента. Это позволяет приложению работать в системах с меньшими экранами. – NathanEvans

0

Это работает .. но, возможно, я должен проверить свою математику! вот рабочий код ....

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; 
}