如果您想向任何方向移动图像,您需要确定方向,您应该按照以下步骤步骤 -1 float dx = 0, dy = 0, x = 0, y = 0, z = 0, w = 0 , dz = 0, dw = 0; {这你们四个坐标}
public void StickerPopulate(){
LinerLayoutInfate.removeAllViews();
shstickerBaseAdaptor = new ShstickerBaseAdaptor(getApplicationContext());
View shstickerView = getLayoutInflater().inflate(R.layout.shareimage,
null);
Gallery shsticker_gallery = (Gallery) shstickerView
.findViewById(R.id.shsticker_gallery);
shsticker_gallery.setAdapter(new ShstickerBaseAdaptor(
ImageEditingClass.this));
shsticker_gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
StickerRelativeLayout = (RelativeLayout) findViewById(R.id.shsticker_relativeLayout1);
RelativeLayout.LayoutParams layoutParams;
layoutParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final ImageView crossBtn = new ImageView(
getApplicationContext());
crossBtn.setBackgroundResource(R.drawable.cross);
crossBtn.setMaxWidth(300);
crossBtn.setMaxHeight(200);
crossBtn.setPadding(10, 10, 10, 10);
final ImageView view = new ImageView(getApplicationContext());
view.setImageResource(shstickerBaseAdaptor.shstickerImage[arg2]);
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
windowwidth = getWindowManager().getDefaultDisplay()
.getWidth();
windowheight = getWindowManager().getDefaultDisplay()
.getHeight();
android.widget.RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
dx = event.getRawX() - layoutParams.leftMargin;
dy = event.getRawY() - layoutParams.topMargin;
dz = event.getRawX() - layoutParams.bottomMargin;
dw = event.getRawX() - layoutParams.rightMargin;
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
x = event.getRawX();
y = event.getRawY();
layoutParams.leftMargin = (int) (x - dx);
layoutParams.topMargin = (int) (y - dy);
layoutParams.bottomMargin = (int) (z - dz);
layoutParams.rightMargin = (int) (w - dw);
view.setLayoutParams(layoutParams);
break;
default:
break;
}
return true;
}
});
StickerRelativeLayout.addView(crossBtn, layoutParams);
StickerRelativeLayout.addView(view, layoutParams);
// }
Toast.makeText(ImageEditingClass.this, "you click" + arg2,
Toast.LENGTH_SHORT).show();
}
});
LinerLayoutInfate.addView(shstickerView);
}