![](/img/trans.png)
[英]Whenever I clicked on the play button it's starts a same song (previous song is also playing ) , it goes in infinite times
[英]Place the same image on screen multiple times whenever button clicked
當用戶單擊按鈕時,相同的圖像將在多個位置一次又一次地顯示。 我正在使用觸摸監聽器來移動圖像。 這是有效的,但我無法使用相同的 imageview 或 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 以使其工作,這可能會對您有所幫助:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.