Variables de nivel 5 de escala matricial

Tengo 5 variables.

The bitmap width and height. The screen width and height. The desired percentage

How do I make the bitmap a desired percentage of the screen using a matrix (I.e 1.0 normal, 0.5 half size, 2.0 double size).

The bitmaps are sprites that are different frame widths (I.E explosion is 200x4000 (20 frames of 200x200 while the object is 65x195 so 65x65 per object if that helps).

My aim is to pass something into ResizeBitmap like (Bitmap, 100) which would be 100% of the screen while keeping the aspect ratio.

So ResizeBitmap(Bitmap, 10) would make it 10% of the screen.

This is what I've got so far, it crashes show's the wrong sizes.

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

Can anyone fix my code or at least help me because I've wasted so much time on this function.

Gracias.

preguntado el 30 de enero de 12 a las 19:01

1 Respuestas

Probado:

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

To avoid scaling ratio problems use this in your 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"/>

Respondido el 31 de enero de 12 a las 02:01

I just created it now and it crashs on load :-( Here is the code: code int percentage = screen_percentage / 100; 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); // RECREATE THE NEW BITMAP return Bitmap.createScaledBitmap (bitmap, newWidth, newHeight, true); - Oliver Dixon

Don't get a chance to see it, the app just crashes. - Oliver Dixon

If you use eclipse, go to the Debug perspective, watch the output in Logcat window, you should see an error message - Entreco

The error is: 01-30 21:22:20.531: E/AndroidRuntime(14844): at com.forwardapps.live.BattlefieldThread.run(BattlefieldThread.java:42) - Oliver Dixon

01-30 21:22:20.531: E/AndroidRuntime(14844): java.lang.IllegalArgumentException: width and height must be > 0 - Oliver Dixon

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.