简体   繁体   中英

Android- Matrix can not be modified

I'm trying to set a background for my SurfaceView, but can't get it to fill the entire screen. The right and bottom are left black around my drawable, so I would like to scale it just a tad bit to fit each screen. A solution I found was to use a matrix to scale my bitmap. I set up what I would believe to be correct, based on a few examples, but it just crashes before opening, with errors about matrix can not be modified. Can't seem to figure out what I could be doing wrong.

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

public class MySurface extends SurfaceView implements Runnable {


SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning = true;

Bitmap Background;
Bitmap clouda;

Matrix matrix = new Matrix();


public MySurface(Context context) {
    super(context);
    init(context);

}

public MySurface(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public MySurface(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}

private void init(Context context) {
    // do stuff that was in your original constructor...
    ourHolder = getHolder();
    ourThread = new Thread(this);
    ourThread.start();

    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager windowManager = (WindowManager)     context.getSystemService(Context.WINDOW_SERVICE);
    windowManager.getDefaultDisplay().getMetrics(metrics);

    int screenHeight = metrics.heightPixels;
    int screenWidth = metrics.widthPixels;

    Background = BitmapFactory.decodeResource(getResources(), R.drawable.island);
    clouda = BitmapFactory.decodeResource(getResources(), R.drawable.clouda);


    Matrix matrix = getMatrix();

    RectF drawableRect = new RectF(0, 0, Background.getWidth(), Background.getHeight());
    RectF viewRect = new RectF(0, 0, screenWidth, screenHeight);
    Log.d("tag", drawableRect.toString());
    Log.d("tag", viewRect.toString());

    matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);





}


@Override
public void run() {
    // TODO Auto-generated method stub
    while(isRunning){
        if (!ourHolder.getSurface().isValid())
            continue;


        Canvas canvas = ourHolder.lockCanvas(); 


        canvas.drawBitmap(Background, matrix, null);
        canvas.drawBitmap(clouda, 0, 0, null);

        ourHolder.unlockCanvasAndPost(canvas);

       }

   }



}

And here is my logcat, which I have two tags so that I can see the height and width of what I'm drawing:

07-26 00:25:23.649: D/dalvikvm(3966): GC_FOR_ALLOC freed 59K, 7% free 2562K/2736K, paused 2ms, total 3ms
07-26 00:25:23.681: D/dalvikvm(3966): GC_FOR_ALLOC freed 2K, 6% free 2900K/3076K, paused 3ms, total 4ms
07-26 00:25:23.693: I/dalvikvm-heap(3966): Grow heap (frag case) to 5.285MB for 2457612-byte allocation
07-26 00:25:23.697: D/dalvikvm(3966): GC_FOR_ALLOC freed <1K, 4% free 5300K/5480K, paused 3ms, total 3ms
07-26 00:25:23.709: D/dalvikvm(3966): GC_CONCURRENT freed <1K, 4% free 5300K/5480K, paused 7ms+0ms, total 9ms
07-26 00:25:23.745: D/tag(3966): RectF(0.0, 0.0, 640.0, 960.0)
07-26 00:25:23.749: D/tag(3966): RectF(0.0, 0.0, 768.0, 1184.0)
07-26 00:25:23.749: D/AndroidRuntime(3966): Shutting down VM
07-26 00:25:23.749: W/dalvikvm(3966): threadid=1: thread exiting with uncaught exception (group=0xa61b9908)
07-26 00:25:23.753: E/AndroidRuntime(3966): FATAL EXCEPTION: main
07-26 00:25:23.753: E/AndroidRuntime(3966): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hour24.by8by8/com.hour24.by8by8.MainActivity}: java.lang.IllegalStateException: Matrix can not be modified
07-26 00:25:23.753: E/AndroidRuntime(3966):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at android.os.Looper.loop(Looper.java:137)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at java.lang.reflect.Method.invokeNative(Native Method)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at java.lang.reflect.Method.invoke(Method.java:511)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at dalvik.system.NativeStart.main(Native Method)
07-26 00:25:23.753: E/AndroidRuntime(3966): Caused by: java.lang.IllegalStateException:  Matrix can not be modified
07-26 00:25:23.753: E/AndroidRuntime(3966):     at android.graphics.Matrix$1.oops(Matrix.java:43)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at android.graphics.Matrix$1.setRectToRect(Matrix.java:205)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at com.hour24.by8by8.MySurface.init(MySurface.java:69)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at com.hour24.by8by8.MySurface.<init>(MySurface.java:31)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at com.hour24.by8by8.MainActivity.onCreate(MainActivity.java:78)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at android.app.Activity.performCreate(Activity.java:5104)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-26 00:25:23.753: E/AndroidRuntime(3966):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-26 00:25:23.753: E/AndroidRuntime(3966):     ... 11 more

Consider using the Canvas#drawBitmap(Bitmap, Rect, RectF, Paint) method instead. Your code is most of the way there already. Change drawableRect 's type to Rect , make it and viewRect class members, and pass null for the Paint parameter.

Side note: In keeping with Java naming conventions, Background should be background .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM