我从一本书中复制了这段代码并尝试运行它,基本上它在屏幕上绘制一个矩形,然后尝试对其应用缩放和拖动。但不幸的是它不起作用并给我胎儿错误。有什么问题?
这是代码:
public class DrawStuffActivity extends Activity implements
OnTouchListener {
MyView myView;
int numberOfFingers = 0;
float oldX[] = new float[2], oldY[] = new float[2];
Rect rectangle = new Rect(0, 0, 100, 100);
DisplayMetrics metrics = new DisplayMetrics();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myView = new MyView(this);
setContentView(myView);
myView.setOnTouchListener(this);
getWindowManager().getDefaultDisplay().
getMetrics(metrics);
}
public boolean onTouch(View view, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
numberOfFingers = 1;
oldX[0] = event.getX(0);
oldY[0] = event.getY(0);
break;
case MotionEvent.ACTION_POINTER_DOWN:
numberOfFingers = 2;
oldX[1] = event.getX(1);
oldY[1] = event.getY(1);
break;
case MotionEvent.ACTION_MOVE:
handleMove(event);
break;
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_UP:
numberOfFingers--;
break;
}
view.invalidate();
return true;
}
float newX[] = new float[2], newY[] = new float[2];
int xChange[] = new int[2], yChange[] = new int[2];
int diffX, diffY;
int newLeft = rectangle.left, newTop = rectangle.top,
newRight = rectangle.right,
newBottom = rectangle.bottom;
void handleMove(MotionEvent event) {
newX[0] = Math.round(event.getX(0));
newY[0] = Math.round(event.getY(0));
xChange[0] = Math.round(newX[0] - oldX[0]);
yChange[0] = Math.round(newY[0] - oldY[0]);
oldX[0] = newX[0];
oldY[0] = newY[0];
switch (numberOfFingers) {
case 1:
newLeft = rectangle.left + xChange[0];
newTop = rectangle.top + yChange[0];
newRight = rectangle.right + xChange[0];
newBottom = rectangle.bottom + yChange[0];
if (newLeft < 0 || newRight > metrics.widthPixels) {
newLeft = rectangle.left;
newRight = rectangle.right;
}
if (newTop < 0 || newBottom > metrics.heightPixels) {
newTop = rectangle.top;
newBottom = rectangle.bottom;
}
rectangle =
new Rect(newLeft, newTop, newRight, newBottom);
break;
case 2:
newX[1] = Math.round(event.getX(1));
newY[1] = Math.round(event.getY(1));
diffX =
Math.abs(Math.round(newX[1] - newX[0]))
- Math.abs(Math.round(oldX[1] - oldX[0]));
diffY =
Math.abs(Math.round(newY[1] - newY[0]))
- Math.abs(Math.round(oldY[1] - oldY[0]));
oldX[1] = newX[1];
oldY[1] = newY[1];
newLeft = rectangle.left - diffX / 2;
newTop = rectangle.top - diffY / 2;
newRight = rectangle.right + diffX / 2;
newBottom = rectangle.bottom + diffY / 2;
rectangle =
new Rect(newLeft, newTop, newRight, newBottom);
break;
}
}
// The handleMove method is in Listing 3-6.
class MyView extends View {
Paint whitePaint = new Paint();
MyView(Context context) {
super(context);
whitePaint.setColor(Color.WHITE);
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawRect(rectangle, whitePaint);
}
}
}
这是错误:
05-03 08:40:57.597: E/AndroidRuntime(278): FATAL EXCEPTION: main
05-03 08:40:57.597: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.allmycode.draw/com.allmycode.draw.DrawstaffActivity}: java.lang.ClassNotFoundException: com.allmycode.draw.DrawstaffActivity in loader dalvik.system.PathClassLoader[/data/app/com.allmycode.draw-1.apk]
05-03 08:40:57.597: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
05-03 08:40:57.597: E/AndroidRuntime(278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-03 08:40:57.597: E/AndroidRuntime(278): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-03 08:40:57.597: E/AndroidRuntime(278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-03 08:40:57.597: E/AndroidRuntime(278): at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 08:40:57.597: E/AndroidRuntime(278): at android.os.Looper.loop(Looper.java:123)
05-03 08:40:57.597: E/AndroidRuntime(278): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-03 08:40:57.597: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 08:40:57.597: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521)
05-03 08:40:57.597: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-03 08:40:57.597: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-03 08:40:57.597: E/AndroidRuntime(278): at dalvik.system.NativeStart.main(Native Method)
05-03 08:40:57.597: E/AndroidRuntime(278): Caused by: java.lang.ClassNotFoundException: com.allmycode.draw.DrawstaffActivity in loader dalvik.system.PathClassLoader[/data/app/com.allmycode.draw-1.apk]
05-03 08:40:57.597: E/AndroidRuntime(278): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
05-03 08:40:57.597: E/AndroidRuntime(278): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
05-03 08:40:57.597: E/AndroidRuntime(278): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
05-03 08:40:57.597: E/AndroidRuntime(278): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-03 08:40:57.597: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
05-03 08:40:57.597: E/AndroidRuntime(278): ... 11 more
manifest.xml 似乎还可以:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.allmycode.draw"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".DrawstaffActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>