java - 每当单击按钮时,多次在屏幕上放置相同的图像
问题描述
当用户单击按钮时,相同的图像将在多个位置一次又一次地显示。我正在使用触摸监听器来移动图像。这是有效的,但我无法使用相同的图像视图或 for 循环创建多个图像,请帮助。这是我要多次创建的图像 这是我的 xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/main_html"
tools:context="com.incubers.signondoc.ui.HtmlActivity">
<Button
android:id="@+id/save_edited"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
style="?android:button"
android:text="@string/hint_save" />
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/viewpdfHtml"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageSign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/no_connection" />
</com.github.barteksc.pdfviewer.PDFView>`
这是我用于移动图像和其他代码的 ontouchlistener
ImageView imageView;
ViewGroup mainLayout;
private View.OnTouchListener onTouchListener() {
return (view, event) -> {
final int x = (int) event.getRawX();
final int y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams)
view.getLayoutParams();
xDelta = x - lParams.leftMargin;
yDelta = y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
float xD = event.getX();
float yD = event.getY();
DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
float XDis = displayMetrics.xdpi;
float YDis = displayMetrics.ydpi;
finalX = xD/XDis*100;
finalY = yD/YDis*100;
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = x - xDelta;
layoutParams.topMargin = y - yDelta;
layoutParams.rightMargin = 0;
layoutParams.bottomMargin = 0;
view.setLayoutParams(layoutParams);
break;
}
mainLayout.invalidate();
return true;
};
}
这是应该在点击时显示图像的按钮
buttonSign.setOnClickListener(view ->{
}
});
解决方案
您需要动态创建 ImageView 以使其正常工作,这可能会对您有所帮助:
推荐阅读
- parameters - 在詹金斯声明性管道中的多行 sh 脚本中包含参数
- java - 数据对象的静态数组列表是否会在主屏幕小部件运行时导致内存泄漏
- excel - VBA:如何使用字符编号根据单元格值插入符号
- terminal - 在 Mac 终端中禁用 VT100 仿真
- azure - 设置 Azure Key Vault
- list - 是否可以根据条件查询 Cassandra 中的所有记录?
- time-complexity - 他们如何计算这个问题的时间复杂度
- ios - 快速在表格视图内的集合视图中显示内容时出现问题
- machine-learning - 克利夫兰心脏病数据集 - 无法描述该类
- swift - 如何自定义搜索栏 iOS