android图片拖动并限制区域

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程
布局文件activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <ImageView
        android:id="@+id/iv_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"/>

</RelativeLayout>

处理Activity类

package com.hbk.motionevent;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends Activity implements OnTouchListener {

	private ImageView iv_main;
	private RelativeLayout parentView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		iv_main = (ImageView) findViewById(R.id.iv_main);
		
		parentView = (RelativeLayout) iv_main.getParent();
		/*
		int right = parentView.getRight(); //0
		int bottom = parentView.getBottom();   //0
		Toast.makeText(this, right+"---"+bottom, 1).show();
		*/
		//设置touch监听 
		iv_main.setOnTouchListener(this);
	}
	
	private int lastX;
	private int lastY;
	private int maxRight;
	private int maxBottom;
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		//得到事件的坐标
		int eventX = (int) event.getRawX();
		int eventY = (int) event.getRawY();
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			
			//得到父视图的right/bottom
			if(maxRight==0) {//保证只赋一次值
				maxRight = parentView.getRight();
				maxBottom = parentView.getBottom();
			}
			
			
			//第一次记录lastX/lastY
			lastX =eventX;
			lastY = eventY;
			break;
		case MotionEvent.ACTION_MOVE:
			//计算事件的偏移
			int dx = eventX-lastX;
			int dy = eventY-lastY;
			//根据事件的偏移来移动imageView
			int left = iv_main.getLeft()+dx;
			int top = iv_main.getTop()+dy;
			int right = iv_main.getRight()+dx;
			int bottom = iv_main.getBottom()+dy;
			
			//限制left  >=0
			if(left<0) {
				right += -left;
				left = 0;
			}
			//限制top
			if(top<0) {
				bottom += -top;
				top = 0;
			}
			//限制right <=maxRight
			if(right>maxRight) {
				left -= right-maxRight;
				right = maxRight;
			}
			//限制bottom <=maxBottom
			if(bottom>maxBottom) {
				top -= bottom-maxBottom;
				bottom = maxBottom;
			}
			
			iv_main.layout(left, top, right, bottom);
			//再次记录lastX/lastY
			lastX = eventX;
			lastY = eventY;
			break;

		default:
			break;
		}
		return true;//所有的motionEvent都交给imageView处理
	}
}

原理,在触摸down的时候,记录lastX和lastY,在每一次move的时候计算偏移量,根据获取父view的右边和下边位置作为最大值,最终调用view的layout方法更新布局定位。在onTouch事件中,return true表示所有motiionEvent都交给imageView处理。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄宝康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值