图片移动并进行边界判断
进行 图片 判断 移动 边界
2023-09-14 08:58:19 时间
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img1 = (ImageView) findViewById(R.id.imageView1);
img2 = (ImageView) findViewById(R.id.imageView2);
DisplayMetrics dm = getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels - 50;
img1.setOnTouchListener(movingEventListener);
img2.setOnTouchListener(movingEventListener);
}
private OnTouchListener movingEventListener = new OnTouchListener() {
int lastX, lastY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) event.getRawX() - lastX;
int dy = (int) event.getRawY() - lastY;
int left = v.getLeft() + dx;
int top = v.getTop() + dy;
int right = v.getRight() + dx;
int bottom = v.getBottom() + dy;
// 设置不能出界
if (left 0) {
left = 0;
right = left + v.getWidth();
}
if (right screenWidth) {
right = screenWidth;
left = right - v.getWidth();
}
if (top 0) {
top = 0;
bottom = top + v.getHeight();
}
if (bottom screenHeight) {
bottom = screenHeight;
top = bottom - v.getHeight();
}
v.layout(left, top, right, bottom);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
};
}
xml 代码
Java代码
?xml version="1.0" encoding="utf-8"?
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
RelativeLayout android:id="@+id/relativeLayout1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
ImageView android:id="@+id/imageView1"
android:layout_width="wrap_content" android:background="@drawable/icon"
android:layout_height="wrap_content" android:layout_alignParentLeft="true" /ImageView
ImageView android:id="@+id/imageView2"
android:layout_width="wrap_content" android:background="@drawable/icon"
android:layout_height="wrap_content" android:layout_toRightOf="@+id/imageView1"
android:layout_alignTop="@+id/imageView1" android:layout_alignBottom="@+id/imageView1" /ImageView
/RelativeLayout
/LinearLayout
其中 在onTouch 代码中 如果返回 false 就不能捕捉到ACTION_MOVE 事件。
对于onTouchEvent 中onTouch返回值
1 、如果return false 说明还没有消费onTouch事件,在执行onTouch里代码后,onTouch事件并没有结束。
2、如果return true 说明消费了onTouch事件 onTouch事件结束了
但在实际操作中 除了ACTION_DOWN事件以外,其余的事件只有返回true的那个方法才能捕捉到。所以 返回false的时候只能捕捉到每次的第一个DOWN事件 后面的MOVE 和UP事件就捕捉不到了。
DisplayMetics 类:
Andorid.util 包下的DisplayMetrics 类提供了一种关于显示的通用信息,如显示大小,分辨率和字体。
为了获取DisplayMetrics 成员,首先初始化一个对象如下:
Java代码
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics;
//getWindowManager() 获取显示定制窗口的管理器
//getDefaultDisplay() 获取默认显示Display对象
//getMetrics(dm) 通过Display对象的数据来初始化一个DisplayMetrics对象
v.layout(left, top, right, bottom);
Assign a size and position to a view and all of its descendants
This is the second phase of the layout mechanism. (The first is measuring). In this phase, each parent calls layout on all of its children to position them. This is typically done using the child measurements that were stored in the measure pass(). Derived classes with children should override onLayout. In that method, they should call layout on each of their their children.
Parameters:
l Left position, relative to parent
t Top position, relative to parent
r Right position, relative to parent
b Bottom position, relative to parent
判断一个点是否在RotatedRect中 openCV函数pointPolygonTest(): C++: double pointPolygonTest(InputArray contour, Point2f pt, bool measureDist) 用于判断一个点是否在轮廓中 当measureDist设置为true时,若返回值为正,表示点在轮廓内部,返回值为负,表示在轮廓外部,返回值为0,表示在轮廓上。
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img1 = (ImageView) findViewById(R.id.imageView1);
img2 = (ImageView) findViewById(R.id.imageView2);
DisplayMetrics dm = getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels - 50;
img1.setOnTouchListener(movingEventListener);
img2.setOnTouchListener(movingEventListener);
}
private OnTouchListener movingEventListener = new OnTouchListener() {
int lastX, lastY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) event.getRawX() - lastX;
int dy = (int) event.getRawY() - lastY;
int left = v.getLeft() + dx;
int top = v.getTop() + dy;
int right = v.getRight() + dx;
int bottom = v.getBottom() + dy;
// 设置不能出界
if (left 0) {
left = 0;
right = left + v.getWidth();
}
if (right screenWidth) {
right = screenWidth;
left = right - v.getWidth();
}
if (top 0) {
top = 0;
bottom = top + v.getHeight();
}
if (bottom screenHeight) {
bottom = screenHeight;
top = bottom - v.getHeight();
}
v.layout(left, top, right, bottom);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
};
}
xml 代码
Java代码
?xml version="1.0" encoding="utf-8"?
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
RelativeLayout android:id="@+id/relativeLayout1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
ImageView android:id="@+id/imageView1"
android:layout_width="wrap_content" android:background="@drawable/icon"
android:layout_height="wrap_content" android:layout_alignParentLeft="true" /ImageView
ImageView android:id="@+id/imageView2"
android:layout_width="wrap_content" android:background="@drawable/icon"
android:layout_height="wrap_content" android:layout_toRightOf="@+id/imageView1"
android:layout_alignTop="@+id/imageView1" android:layout_alignBottom="@+id/imageView1" /ImageView
/RelativeLayout
/LinearLayout
其中 在onTouch 代码中 如果返回 false 就不能捕捉到ACTION_MOVE 事件。
对于onTouchEvent 中onTouch返回值
1 、如果return false 说明还没有消费onTouch事件,在执行onTouch里代码后,onTouch事件并没有结束。
2、如果return true 说明消费了onTouch事件 onTouch事件结束了
但在实际操作中 除了ACTION_DOWN事件以外,其余的事件只有返回true的那个方法才能捕捉到。所以 返回false的时候只能捕捉到每次的第一个DOWN事件 后面的MOVE 和UP事件就捕捉不到了。
DisplayMetics 类:
Andorid.util 包下的DisplayMetrics 类提供了一种关于显示的通用信息,如显示大小,分辨率和字体。
为了获取DisplayMetrics 成员,首先初始化一个对象如下:
Java代码
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics;
//getWindowManager() 获取显示定制窗口的管理器
//getDefaultDisplay() 获取默认显示Display对象
//getMetrics(dm) 通过Display对象的数据来初始化一个DisplayMetrics对象
v.layout(left, top, right, bottom);
Assign a size and position to a view and all of its descendants
This is the second phase of the layout mechanism. (The first is measuring). In this phase, each parent calls layout on all of its children to position them. This is typically done using the child measurements that were stored in the measure pass(). Derived classes with children should override onLayout. In that method, they should call layout on each of their their children.
Parameters:
l Left position, relative to parent
t Top position, relative to parent
r Right position, relative to parent
b Bottom position, relative to parent
判断一个点是否在RotatedRect中 openCV函数pointPolygonTest(): C++: double pointPolygonTest(InputArray contour, Point2f pt, bool measureDist) 用于判断一个点是否在轮廓中 当measureDist设置为true时,若返回值为正,表示点在轮廓内部,返回值为负,表示在轮廓外部,返回值为0,表示在轮廓上。
相关文章
- 在Hibernate中使用HibernateTemplate来进行包含sql语句的查询
- android读取sd卡图片并进行缩放操作
- 使用Python的PIL模块来进行图片对比
- 对图片进行管理的工具类
- 【IOS-COCOS2D游戏开发之十二】浅析使用C++/C/OC进行IOS游戏混编出现“FAILED WITH EXIT”问题与小结;
- IOS使用xcode可视化图像编辑功能进行图片拉伸
- IOS中使用像素位图(CGImageRef)对图片进行处理
- 【原创】如何在vim中使用tab进行python代码补全
- 使用Python的PIL模块来进行图片对比
- 数据重组:对一堆相似字典进行分类统计(shidebin)
- Qt音视频开发17-vlc内核回调拿图片进行绘制
- OpenCV对图片进行边缘像素填充
- OpenCV将两图片进行混合,达到两张图片合并成一张图片的效果
- 如何利用CANN DVPP进行图片的等比例缩放?
- nodejs 使用mysql 进行查询的问题
- JumpServer 堡垒机里资产,使用WinSCP、Xftp、Filezilla 等工具进行文件管理
- 使用GDI+进行图片处理时要注意的问题
- 基于深度学习的病毒检测技术无需沙箱环境,直接将样本文件转换为二维图片,进而应用改造后的卷积神经网络 Inception V4 进行训练和检测
- 利用PyCharm的Profile工具进行Python性能分析
- 使用 Learner Lab - 在 Lambda 建立 Pillow 层,进行 S3 的图片镜相操作
- IPython:利用python语言将后缀为ipynb文件中的输出的图片在py文件中编程进行可视化—即如何将IPython.core.display.HTML类型的数据进行图表可视化
- 【Python 实战】---- 使用【Tinypng图片压缩非API】进行批量图片压缩