android action\u up有时工作不完美?
在我的应用程序中,使用android touch events中的矩阵拖放图像,在此android action\u up有时工作不完美?,android,multi-touch,Android,Multi Touch,在我的应用程序中,使用android touch events中的矩阵拖放图像,在此MotionEvent.ACTION\u DOWN&MotionEvent.ACTION\u MOVE工作正常,但MotionEvent.ACTION\u UP不工作。actionup有时工作,有时不工作,如何解决我的问题 public class MainActivity extends Activity { // mainLayout is the child of the HorizontalSc
MotionEvent.ACTION\u DOWN
&MotionEvent.ACTION\u MOVE
工作正常,但MotionEvent.ACTION\u UP
不工作。actionup有时工作,有时不工作,如何解决我的问题
public class MainActivity extends Activity {
// mainLayout is the child of the HorizontalScrollView ...
private LinearLayout mainLayout;
// this is an array that holds the IDs of the drawables ...
private int[] images = { R.drawable.gg, R.drawable.gg, R.drawable.gg,
R.drawable.gg, R.drawable.gg, R.drawable.gg };
ImageButton btn1, btn2, btn3;
float dy; // postTranslate Y distance
float matrixX = 0; // X coordinate of matrix inside the ImageView
float matrixY = 0; // Y coordinate of matrix inside the ImageView
float[] matrixValues = new float[9];
float height = 0;
HorizontalScrollView hsv;
boolean hTouch = false;
/** Called when the activity is first created. */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
mainLayout = (LinearLayout) findViewById(R.id.linearLayout);
for (int i = 0; i < images.length; i++) {
hsv = (HorizontalScrollView) findViewById(R.id.hsv);
final Matrix matrix = new Matrix();
final Matrix savedMatrix = new Matrix();
final PointF startPoint = new PointF();
matrixValues = new float[9];
View cell = getLayoutInflater().inflate(R.layout.image, null);
final ImageView img = (ImageView) cell.findViewById(R.id.imageView);
img.setImageResource(images[i]);
btn1 = (ImageButton) cell.findViewById(R.id.button1);
btn2 = (ImageButton) cell.findViewById(R.id.button2);
btn3 = (ImageButton) cell.findViewById(R.id.button3);
img.setOnTouchListener(new View.OnTouchListener() {
@SuppressWarnings("deprecation")
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
switch (event.getAction() ) {
case MotionEvent.ACTION_DOWN:
System.out.println("action down");
savedMatrix.set(matrix);
startPoint.set(view.getLeft(), event.getY());
break;
case MotionEvent.ACTION_UP:
matrix.set(savedMatrix);
matrix.getValues(matrixValues);
matrixX = matrixValues[2];
matrixY = matrixValues[5];
height = matrixValues[4]
* (((ImageView) view).getDrawable()
.getIntrinsicHeight());
dy = event.getY() - startPoint.y;
System.out.println("matrixY: " + matrixY);
if (matrixY + dy < 0) {
System.out.println("1st if checking");
dy = -matrixY;
}
if (matrixY + dy + height > view.getHeight()
+ btn1.getBottom()) {
System.out.println("2nd if checking");
dy = (view.getHeight() + btn1.getBottom())
- matrixY - (height);
}
float bottom = (btn1.getBottom()+2);
if (dy > 0.01 && dy < (bottom)) {
System.out.println("1st if");
dy = btn1.getBottom() ;
}
if (dy < -0.01 && dy > (-bottom)) {
System.out.println("2nd if");
dy = -btn1.getBottom();
}
matrix.postTranslate(0, dy);
view.setImageMatrix(matrix);
view.invalidate();
break;
case MotionEvent.ACTION_MOVE:
System.out.println("");
matrix.set(savedMatrix);
matrix.getValues(matrixValues);
matrixX = matrixValues[2];
matrixY = matrixValues[5];
height = matrixValues[4]
* (((ImageView) view).getDrawable()
.getIntrinsicHeight());
dy = event.getY() - startPoint.y;
if (matrixY + dy < 0) {
dy = -matrixY;
}
if (matrixY + dy + height > view.getHeight()
+ btn1.getBottom()) {
dy = (view.getHeight() + btn1.getBottom())
- matrixY - (height);
}
matrix.postTranslate(0, dy);
view.setImageMatrix(matrix);
break;
}
return true;
}
});
mainLayout.addView(cell);
}
}
}
公共类MainActivity扩展活动{
//mainLayout是HorizontalScrollView的子级。。。
私人线路布局主要布局;
//这是一个数组,它保存可绘图项的ID。。。
private int[]image={R.drawable.gg,R.drawable.gg,R.drawable.gg,
R.drawable.gg,R.drawable.gg,R.drawable.gg};
图像按钮btn1、btn2、btn3;
float dy;//平移后Y距离
float matrixX=0;//ImageView中矩阵的X坐标
float matrixY=0;//ImageView中矩阵的Y坐标
浮动[]矩阵值=新浮动[9];
浮动高度=0;
水平滚动视图hsv;
布尔hTouch=false;
/**在首次创建活动时调用*/
@TargetApi(构建版本代码蜂窝MR2)
@凌驾
创建公共空间(捆绑冰柱){
超级冰柱;
setContentView(R.layout.activity_main);
主布局=(线性布局)findViewById(R.id.LinearLayout);
对于(int i=0;iview.getHeight()
+btn1.getBottom()){
System.out.println(“第二次如果检查”);
dy=(view.getHeight()+btn1.getBottom())
-母体(高度);
}
浮动底部=(btn1.getBottom()+2);
如果(dy>0.01&&dy<(底部)){
系统输出打印项次(“第一个if”);
dy=btn1.getBottom();
}
如果(dy<-0.01&&dy>(-底部)){
系统输出打印项次(“第二个if”);
dy=-btn1.getBottom();
}
矩阵后翻译(0,dy);
视图.setImageMatrix(矩阵);
view.invalidate();
打破
case MotionEvent.ACTION\u移动:
System.out.println(“”);
矩阵集(savedMatrix);
矩阵。获取值(矩阵值);
matrixX=matrixValues[2];
矩阵x=矩阵x值[5];
高度=矩阵值[4]
*(((ImageView)视图).getDrawable()
.getIntrinsicHeight());
dy=event.getY()-startPoint.y;
if(矩阵y+dy<0){
dy=-矩阵;
}
if(matrixY+dy+height>view.getHeight()
+btn1.getBottom()){
dy=(view.getHeight()+btn1.getBottom())
-母体(高度);
}
矩阵后翻译(0,dy);
视图.setImageMatrix(矩阵);
打破
}
返回true;
}
});
mainLayout.addView(单元);
}
}
}
不是这样的
MotionEvent.ACTION\u CANCEL
是另一个事件,当您开始从视图中拖动并在视图外拖动结束时,会触发该事件。此时它不会调用MotionEvent.ACTION\u UP
<代码>向上在操作启动时被调用