如何将 eclipse 中的图像(已经在 drawables 文件中)移动到屏幕上的特定位置?这是我尝试过的代码,不要认为它甚至接近......
这看起来怎么样?
SurfaceHolder surfaceHolder;
SurfaceView surface = new SurfaceView(this);
Rect rect = new Rect(0,0,100,100);
这是在 onCreate
LinearLayout layout = (LinearLayout) findViewById(R.layout.activity_test2);
surfaceHolder = surface.getHolder();
layout.addView(surface);
(公众视野在我的主要活动中)
public void onDraw(Canvas canvas) {
canvas = surfaceHolder.lockCanvas();
Bitmap image1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
canvas.drawBitmap(image1, null, rect, null);
surfaceHolder.unlockCanvasAndPost(canvas);
}
LOGCAT
02-15 01:42:16.824: W/Trace(2782): Unexpected value from nativeGetEnabledTags: 0
02-15 01:42:16.824: W/Trace(2782): Unexpected value from nativeGetEnabledTags: 0
02-15 01:42:49.993: W/Trace(2782): Unexpected value from nativeGetEnabledTags: 0
02-15 01:43:03.026: W/Trace(2992): Unexpected value from nativeGetEnabledTags: 0
02-15 01:43:03.026: W/Trace(2992): Unexpected value from nativeGetEnabledTags: 0
02-15 01:43:03.026: W/Trace(2992): Unexpected value from nativeGetEnabledTags: 0
02-15 01:43:03.474: W/Trace(2992): Unexpected value from nativeGetEnabledTags: 0
02-15 01:43:03.505: W/Trace(2992): Unexpected value from nativeGetEnabledTags: 0
02-15 01:43:03.544: D/AndroidRuntime(2992): Shutting down VM
02-15 01:43:03.544: W/dalvikvm(2992): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
02-15 01:43:03.733: E/AndroidRuntime(2992): FATAL EXCEPTION: main
02-15 01:43:03.733: E/AndroidRuntime(2992): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.test2/com.example.test2.Test2}: java.lang.NullPointerException
02-15 01:43:03.733: E/AndroidRuntime(2992): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
02-15 01:43:03.733: E/AndroidRuntime(2992): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-15 01:43:03.733: E/AndroidRuntime(2992): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-15 01:43:03.733: E/AndroidRuntime(2992): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-15 01:43:03.733: E/AndroidRuntime(2992): at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 01:43:03.733: E/AndroidRuntime(2992): at android.os.Looper.loop(Looper.java:137)
02-15 01:43:03.733: E/AndroidRuntime(2992): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-15 01:43:03.733: E/AndroidRuntime(2992): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 01:43:03.733: E/AndroidRuntime(2992): at java.lang.reflect.Method.invoke(Method.java:511)
02-15 01:43:03.733: E/AndroidRuntime(2992): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-15 01:43:03.733: E/AndroidRuntime(2992): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-15 01:43:03.733: E/AndroidRuntime(2992): at dalvik.system.NativeStart.main(Native Method)
02-15 01:43:03.733: E/AndroidRuntime(2992): Caused by: java.lang.NullPointerException
02-15 01:43:03.733: E/AndroidRuntime(2992): at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
02-15 01:43:03.733: E/AndroidRuntime(2992): at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
02-15 01:43:03.733: E/AndroidRuntime(2992): at android.view.View.<init>(View.java:3224)
02-15 01:43:03.733: E/AndroidRuntime(2992): at android.view.SurfaceView.<init>(SurfaceView.java:181)
02-15 01:43:03.733: E/AndroidRuntime(2992): at com.example.test2.Test2.<init>(Test2.java:44)
02-15 01:43:03.733: E/AndroidRuntime(2992): at java.lang.Class.newInstanceImpl(Native Method)
02-15 01:43:03.733: E/AndroidRuntime(2992): at java.lang.Class.newInstance(Class.java:1319)
02-15 01:43:03.733: E/AndroidRuntime(2992): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
02-15 01:43:03.733: E/AndroidRuntime(2992): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
02-15 01:43:03.733: E/AndroidRuntime(2992): ... 11 more
02-15 01:43:11.183: I/Process(2992): Sending signal. PID: 2992 SIG: 9