我正在Android中开发一个小应用程序,我必须在其中显示图像。我的应用程序中有2个图像,但是我必须通过手指触摸来移动图像,因此我在我的应用程序中使用了viewflipper,我还想要缩放和缩放功能。
我还为xoom制作了3个课程。但是当我运行应用程序时,它会显示第一个图像,并且在触摸和翻转它时也会缩放它,然后它将移动到第二个图像,之后我将它翻转回来尝试缩放它我的应用程序将崩溃并在logcat中给出以下消息
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): FATAL EXCEPTION: main
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): java.lang.ClassCastException: android.widget.LinearLayout
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at org.example.touch.Touch.onTouch(Touch.java:117)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.view.View.dispatchTouchEvent(View.java:3762)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.os.Looper.loop(Looper.java:123)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at java.lang.reflect.Method.invoke(Method.java:521)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at dalvik.system.NativeStart.main(Native Method)
这是我的java代码
/***
* Excerpted from "Hello, Android! 3e",
* published by The Pragmatic Bookshelf.
* Copyrights apply to this code. It may not be used to create training material,
* courses, books, articles, and the like. Contact us if you are in doubt.
* We make no guarantees that this code is fit for any purpose.
* Visit http://www.pragmaticprogrammer.com/titles/eband3 for more book information.
***/
package org.example.touch;
import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;
public class Touch extends Activity implements OnTouchListener {
private static final String TAG = "Touch";
// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
float downXValue;
int counter = 0;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView view = (ImageView) findViewById(R.id.imageView);
ImageView view1 = (ImageView) findViewById(R.id.imageView1);
view.setOnTouchListener(this);
LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
layMain.setOnTouchListener((OnTouchListener) this);
// ...
// Work around a Cupcake bug
matrix.setTranslate(1f, 1f);
view.setImageMatrix(matrix);
view1.setImageMatrix(matrix);
}
@Override
/* public boolean onTouch(View arg0, MotionEvent arg1) {
// Get the action that was done on this touch event
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
{
// store the X value when the user's finger was pressed down
downXValue = arg1.getX();
break;
}
case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
float currentX = arg1.getX();
// going backwards: pushing stuff to the right
if (downXValue < currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
// vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
// Flip!
if(counter > 0){
vf.showPrevious();
counter--;
}
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
//vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
// Flip!
if(counter < 1){
vf.showNext();
counter++;
}
}
break;
}
}
// if you return false, these actions will not be recorded
return true;
}*/
public boolean onTouch(View v, MotionEvent rawEvent) {
WrapMotionEvent event = WrapMotionEvent.wrap(rawEvent);
// ...
ImageView view = (ImageView) v;
// Dump touch event to log
dumpEvent(event);
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG");
mode = DRAG;
downXValue = rawEvent.getX();
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;
case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
float currentX = rawEvent.getX();
// going backwards: pushing stuff to the right
if (downXValue < currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
// vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
// Flip!
if(counter > 0){
vf.showPrevious();
counter--;
}
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
//vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
// Flip!
if(counter < 1){
vf.showNext();
counter++;
}
}
}
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;
}
view.setImageMatrix(matrix);
return true; // indicate event was handled
}
/** Show an event in the LogCat view, for debugging */
private void dumpEvent(WrapMotionEvent event) {
// ...
String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
"POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("event ACTION_").append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN
|| actionCode == MotionEvent.ACTION_POINTER_UP) {
sb.append("(pid ").append(
action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")");
}
sb.append("[");
for (int i = 0; i < event.getPointerCount(); i++) {
sb.append("#").append(i);
sb.append("(pid ").append(event.getPointerId(i));
sb.append(")=").append((int) event.getX(i));
sb.append(",").append((int) event.getY(i));
if (i + 1 < event.getPointerCount())
sb.append(";");
}
sb.append("]");
Log.d(TAG, sb.toString());
}
/** Determine the space between the first two fingers */
private float spacing(WrapMotionEvent event) {
// ...
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
/** Calculate the mid point of the first two fingers */
private void midPoint(PointF point, WrapMotionEvent event) {
// ...
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
}
答案 0 :(得分:1)
当您将v
投射到ImageView
时,您会收到ClassCastException
。这意味着用户触摸了不是ImageView
的视图。
为了避免这种情况,您可以在onCreate
方法中删除下一行代码(51,52):
LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
layMain.setOnTouchListener((OnTouchListener) this);
或者在第116行的onTouch
方法中添加额外的支票:
if(!(v instanceof ImageView))return false;