我正在尝试为SurfaceView设置背景,但无法让它填满整个屏幕。在我的drawable周围,右边和底部都是黑色的,所以我想将它缩放到适合每个屏幕的位置。我找到的解决方案是使用矩阵来缩放我的位图。 基于一些例子,我设置了我认为正确的东西,但它只是在打开之前崩溃,关于矩阵的错误无法修改。似乎无法弄清楚我可能做错了什么。
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);
}
}
}
这是我的logcat,我有两个标签,以便我可以看到我正在绘制的高度和宽度:
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
答案 0 :(得分:0)
请考虑使用Canvas#drawBitmap(Bitmap, Rect, RectF, Paint)
方法。你的代码已经完全存在了。将drawableRect
的类型更改为Rect
,将其设为viewRect
类成员,并为null
参数传递Paint
。
附注:根据Java命名约定,Background
应为background
。