我正在使用矩阵拖放图像,但它跨越了屏幕边界。当我向上拖动时,它跨越了屏幕边界,以及如何将图像向下拖动到某个高度。
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.dd, R.drawable.ss };
int Measuredwidth = 0, MeasuredHeight = 0;
float x = 0;
View cell = null;
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
PointF startPoint = new PointF();
static final int DRAG = 1;
int mode = NONE;
/** Called when the activity is first created. */
@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++) {
cell = getLayoutInflater().inflate(R.layout.image, null);
final ImageView img = (ImageView) cell.findViewById(R.id.imageView);
img.setImageResource(images[i]);
Point size = new Point();
WindowManager w = getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
w.getDefaultDisplay().getSize(size);
MeasuredHeight = size.y;
Measuredwidth = size.x;
} else {
Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
MeasuredHeight = d.getHeight();
}
AbsoluteLayout aalayout = (AbsoluteLayout) cell
.findViewById(R.id.absLayout);
Button btn = (Button) cell.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "mcdk", 2000)
.show();
}
});
aalayout.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
System.out.println("matrix=" + savedMatrix.toString());
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
System.out.println("imge dowm press");
savedMatrix.set(matrix);
startPoint.set(img.getLeft(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
System.out.println("imgeup press");
float dx = event.getX() -startPoint.x;
float dy = event.getY() - startPoint.y;
System.out.println("startpointy: "+startPoint.y);
System.out.println("dy: "+event.getY());
matrix.set(savedMatrix);
int height = img.getHeight();
int h1 = height/1;
// if (( event.getY() >10) && (event.getY() < h1)){
System.out.println("imge height: "+h1);
matrix.postTranslate(img.getLeft(), dy);
// }
break;
}
img.setImageMatrix(matrix);
return true;
}
});
System.out.println("bmgbgklb");
mainLayout.addView(cell);
}
}
}