我正在尝试创建一个简单的应用程序。该应用程序的步骤是
这是我的主要活动
public class MainActivity extends ActionBarActivity {
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
private static final String TAG = "Touch" ;
private RelativeLayout rl_Main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rl_Main = (RelativeLayout) findViewById(R.id.rl_main);
rl_Main.addView(new MyView(this));
}
class MyView extends View implements OnTouchListener{
Paint paint = new Paint();
Point point = new Point();
protected ImageView imageView;
public MyView(Context context) {
super(context);
paint.setColor(Color.RED);
paint.setStrokeWidth(15);
paint.setStyle(Style.STROKE);
imageView = (ImageView)findViewById(R.id.imageView1);
imageView.buildDrawingCache();
imageView.setOnTouchListener(this);
}
@Override
protected void onDraw(Canvas canvas) {
Bitmap b=imageView.getDrawingCache();//BitmapFactory.decodeResource(getResources(), R.drawable.josepg);
canvas.drawBitmap(b, 0, 0, paint);
canvas.drawCircle(point.x, point.y, 1, paint);
}
@Override
public boolean onTouch(View v,MotionEvent event) {
ImageView view = (ImageView) v;
dumpEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
point.x = event.getX();
point.y = event.getY();
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG" );
mode = DRAG;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE" );
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x,
event.getY() - start.y);
}
else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM" );
}
break;
}
view.setImageMatrix(matrix);
invalidate();
return true;
}
}
class Point {
float x, y;
}
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
}
这是我的XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.imagemerge.MainActivity$PlaceholderFragment" >
<ImageView android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/josepg"
android:scaleType="matrix" >
</ImageView>
</RelativeLayout>
但我一直收到这个错误
07-15 10:36:20.808: W/dalvikvm(17289): threadid=1: thread exiting with uncaught exception (group=0x415ebba8)
07-15 10:36:20.808: E/AndroidRuntime(17289): FATAL EXCEPTION: main
07-15 10:36:20.808: E/AndroidRuntime(17289): Process: com.example.imagenew, PID: 17289
07-15 10:36:20.808: E/AndroidRuntime(17289): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imagenew/com.example.imagenew.MainActivity}: java.lang.NullPointerException
07-15 10:36:20.808: E/AndroidRuntime(17289): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-15 10:36:20.808: E/AndroidRuntime(17289): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-15 10:36:20.808: E/AndroidRuntime(17289): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-15 10:36:20.808: E/AndroidRuntime(17289): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-15 10:36:20.808: E/AndroidRuntime(17289): at android.os.Handler.dispatchMessage(Handler.java:102)
07-15 10:36:20.808: E/AndroidRuntime(17289): at android.os.Looper.loop(Looper.java:136)
07-15 10:36:20.808: E/AndroidRuntime(17289): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-15 10:36:20.808: E/AndroidRuntime(17289): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 10:36:20.808: E/AndroidRuntime(17289): at java.lang.reflect.Method.invoke(Method.java:515)
07-15 10:36:20.808: E/AndroidRuntime(17289): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-15 10:36:20.808: E/AndroidRuntime(17289): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-15 10:36:20.808: E/AndroidRuntime(17289): at dalvik.system.NativeStart.main(Native Method)
07-15 10:36:20.808: E/AndroidRuntime(17289): Caused by: java.lang.NullPointerException
07-15 10:36:20.808: E/AndroidRuntime(17289): at com.example.imagenew.MainActivity$MyView.<init>(MainActivity.java:63)
07-15 10:36:20.808: E/AndroidRuntime(17289): at com.example.imagenew.MainActivity.onCreate(MainActivity.java:48)
07-15 10:36:20.808: E/AndroidRuntime(17289): at android.app.Activity.performCreate(Activity.java:5231)
07-15 10:36:20.808: E/AndroidRuntime(17289): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-15 10:36:20.808: E/AndroidRuntime(17289): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-15 10:36:20.808: E/AndroidRuntime(17289): ... 11 more
答案 0 :(得分:0)
这很简单。显示图像,而不是在下次用户点击屏幕时记录x和y坐标。比在该区域绘制一个红色圆圈(或使用自己的红点纹理)。