Android dragandpush

实现图片拖拉效果

代码如下

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ima = (ImageView) findViewById(R.id.iv_logo);
		ima.setTag(IMAGEVIEW_TAG);

		ima.setOnLongClickListener(new View.OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				ClipData.Item item = new ClipData.Item((CharSequence) v
						.getTag());

				String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
				ClipData dragData = new ClipData(v.getTag().toString(),
						mimeTypes, item);

				// Instantiates the drag shadow builder.
				View.DragShadowBuilder myShadow = new DragShadowBuilder(ima);

				// Starts the drag
				v.startDrag(dragData, // the data to be dragged
						myShadow, // the drag shadow builder
						null, // no need to use local data
						0 // flags (not currently used, set to 0)
				);
				return true;
			}
		});

		ima.setOnDragListener(new OnDragListener() {
			@Override
			public boolean onDrag(View v, DragEvent event) {
				switch (event.getAction()) {
				case DragEvent.ACTION_DRAG_STARTED:
					layoutParams = (RelativeLayout.LayoutParams) v
							.getLayoutParams();
					Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED");
					// Do nothing
					break;
				case DragEvent.ACTION_DRAG_ENTERED:
					Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED");
					int x_cord = (int) event.getX();
					int y_cord = (int) event.getY();
					break;
				case DragEvent.ACTION_DRAG_EXITED:
					Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED");
					x_cord = (int) event.getX();
					y_cord = (int) event.getY();
					layoutParams.leftMargin = x_cord;
					layoutParams.topMargin = y_cord;
					v.setLayoutParams(layoutParams);
					break;
				case DragEvent.ACTION_DRAG_LOCATION:
					Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION");
					x_cord = (int) event.getX();
					y_cord = (int) event.getY();
					break;
				case DragEvent.ACTION_DRAG_ENDED:
					Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENDED");
					// Do nothing
					break;
				case DragEvent.ACTION_DROP:
					Log.d(msg, "ACTION_DROP event");
					// Do nothing
					break;
				default:
					break;
				}
				return true;
			}
		});

	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值