2012-01-30 47 views
-1

我有5個變量。矩陣縮放5級變量

位圖的寬度和高度。 屏幕寬度和高度。 想要的百分比

如何使用矩陣(I.e 1.0正常,0.5一半大小,2.0雙倍大小)使位圖成爲屏幕的所需百分比。

的位圖是屬於不同的幀寬度精靈(IE爆炸是200x4000(的200×200 20幀,而所述對象是65x195每個對象是否有幫助),以便65x65。

我的目標是東西進入ResizeBitmap像(位圖,100),這將是屏幕的100%,同時保持縱橫比。

所以ResizeBitmap(位圖,10)將使屏幕的它10%。

這是我得到了什麼到目前爲止,它崩潰了節目的大小錯誤

public Bitmap ResizeBitmap(Bitmap bitmap, int screen_percentage) 
{ 
    float scale; 

    int width = bitmap.getWidth(); 
    int height = bitmap.getHeight(); 

    float percentage_of_screen; 
    float percentage_of_percentage_screen; 

    if(screen_width > screen_height) 
    { 
     percentage_of_screen = (screen_width/100) * width; 
     percentage_of_percentage_screen = (screen_width/100) * screen_percentage; 
     scale = percentage_of_screen/percentage_of_percentage_screen; 
    } 

    else 
    { 
     percentage_of_screen = (screen_height/100) * height; 
     percentage_of_percentage_screen = (screen_height/100) * screen_percentage; 
     scale = percentage_of_screen/percentage_of_percentage_screen; 
    } 

    // CREATE A MATRIX FOR THE MANIPULATION 
    Matrix matrix = new Matrix(); 

    // RESIZE THE BIT MAP 
    matrix.postScale(scale, scale); 

    // RECREATE THE NEW BITMAP 
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false); 

    return resizedBitmap; 
} 

任何人都可以修復我的代碼或至少幫助我,因爲我已經浪費了很多時間在這個函數上。

謝謝。

回答

1

測試:

public class StackOverflowActivity extends Activity{ 

    private int screen_width; 
    private int screen_height; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // in main.xml use an ImageView with android:id="@+id/img" 
     ImageView view = (ImageView)findViewById(R.id.img); 

     // in your drawables folder, put an image named circle.png 
     Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.circle); 

     Display display = getWindowManager().getDefaultDisplay(); 
     screen_width = display.getWidth(); 
     screen_height = display.getHeight(); 

     Bitmap scale = resizeBitmap(bmp, 100); 

     view.setImageBitmap(scale); 

    } 

    public Bitmap resizeBitmap(Bitmap bitmap, int screen_percentage) 
    { 
      ////////////// IMPORTANT //////////////////// 
      // Use '/ 100.0f' otherwise your percentage will be 0.. 
      // this will later on, cause the newWidth & newHeight to be 0 as well 
      // which causes the app to crash 
     float percentage = screen_percentage/100.0f; 

     float scale = screen_width/100 * percentage; 
     if(screen_width < screen_height) 
     { 
      scale = screen_height/100 * percentage; 
     } 

     int newWidth = (int) (bitmap.getWidth() * scale); 
     int newHeight = (int) (bitmap.getHeight() * scale); 

      if(newWidth <= 0 || newHeight <= 0) 
      { // Extra check, for invalid width/height 
       Log.e("test", "invalid dimension ("+newWidth+"x"+newHeight+")"); 
       return bitmap; 
      } 
     return Bitmap.createScaledBitmap (bitmap, newWidth, newHeight, true); 
    } 
} 

爲了避免縮放比例問題在main.xml中

<ImageView 
    android:id="@+id/img" 
    android:background="@null" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:layout_gravity="center" 
    android:scaleType="centerInside"/> 
+0

使用此我只是創造了它,現在它crashs負載:-( 下面是代碼: '代碼 \t \t int percentage = screen_percentage/100; \t \t \t \t float scale = screen_width/100 * percentage; \t \t \t 如果\t(SCREEN_WIDTH 2012-01-30 21:04:20

+0

什麼錯誤說? – Entreco 2012-01-30 21:08:17

+0

不要有機會看到它,應用程序崩潰。 – 2012-01-30 21:14:23