[英]I want to display few images randomly in java (android)
package com.example.paul_2.a5mai;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
int first=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
start();
}
void start () {
//root layout
LinearLayout root = new LinearLayout(this);
root.setOrientation(LinearLayout.VERTICAL);
root.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
setContentView(root);
//butonul de start
Button StartBTN = new Button(this);
StartBTN.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
StartBTN.setText("Click Me");
root.addView(StartBTN);
// contine toate cele 5 layouturi
final LinearLayout linearContainer=new LinearLayout(this);
linearContainer.setOrientation(LinearLayout.HORIZONTAL);
linearContainer.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
root.addView(linearContainer);
//LINIA 1
final LinearLayout firstRow = new LinearLayout(this);
firstRow.setOrientation(LinearLayout.VERTICAL);
firstRow.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT));
linearContainer.addView(firstRow);
//dimensiunea imaginii
final RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(260,260);
//params1.leftMargin =10;
//params1.topMargin = 10;
final ImageView imgView = new ImageView(MainActivity.this);
/* final ImageView imgView = new ImageView(this);
Random rand = new Random();
int rndInt = rand.nextInt(3) + 1; // n = the number of images, that start at idx 1
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setImageResource(id);
firstRow.addView(imgView, params1);*/
/*final ImageView imgView2 = new ImageView(this);
Random rand2 = new Random();
int rndInt2 = rand2.nextInt(3) + 1;
String imgName2 = "img" + rndInt2;
int id2 = getResources().getIdentifier(imgName2, "drawable", getPackageName());
imgView2.setImageResource(id2);
firstRow.addView(imgView2, params1);
final ImageView imgView3 = new ImageView(this);
Random rand3 = new Random();
int rndInt3 = rand3.nextInt(3) + 1;
String imgName3 = "img" + rndInt3;
int id3 = getResources().getIdentifier(imgName3, "drawable", getPackageName());
imgView3.setImageResource(id3);
firstRow.addView(imgView3, params1);*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
final LinearLayout secondRow = new LinearLayout(this);
secondRow.setOrientation(LinearLayout.VERTICAL);
secondRow.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT));
linearContainer.addView(secondRow);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
final LinearLayout thirdRow = new LinearLayout(this);
thirdRow.setOrientation(LinearLayout.VERTICAL);
thirdRow.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT));
linearContainer.addView(thirdRow);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
final LinearLayout fourthRow = new LinearLayout(this);
fourthRow.setOrientation(LinearLayout.VERTICAL);
fourthRow.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT));
linearContainer.addView(fourthRow);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
final LinearLayout fivethRow = new LinearLayout(this);
fivethRow.setOrientation(LinearLayout.VERTICAL);
fivethRow.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT));
linearContainer.addView(fivethRow);
StartBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
first=1;
//ImageView imgView = new ImageView(MainActivity.this);
Random rand = new Random();
int rndInt = rand.nextInt(3) + 1; // n = the number of images, that start at idx 1
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setImageResource(id);
firstRow.addView(imgView, params1);
ImageView imgView2 = new ImageView(MainActivity.this);
Random rand2 = new Random();
int rndInt2 = rand2.nextInt(3) + 1;
String imgName2 = "img" + rndInt2;
int id2 = getResources().getIdentifier(imgName2, "drawable", getPackageName());
imgView2.setImageResource(id2);
firstRow.addView(imgView2, params1);
ImageView imgView3 = new ImageView(MainActivity.this);
Random rand3 = new Random();
int rndInt3 = rand3.nextInt(3) + 1;
String imgName3 = "img" + rndInt3;
int id3 = getResources().getIdentifier(imgName3, "drawable", getPackageName());
imgView3.setImageResource(id3);
firstRow.addView(imgView3, params1);
final Animation animSlide = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide);
firstRow.startAnimation(animSlide);
}
});
}
}
我想通過單擊StartBTN隨機生成此圖像。 該代碼有效,但是問題是,第一次單擊按鈕時生成的圖像仍然保留,而下次單擊按鈕時,它們不會改變。 我需要在每次單擊botton時生成。 我能做什么? 我應該在哪里聲明對象? 我還能怎么做? 先感謝您!
在將對象的聲明移到異地后,會發出logcat onClick事件:05-17 13:49:26.236 8757-8757 / com.example.paul_2.a5mai E / AndroidRuntime:致命例外:主進程:com.example.paul_2.a5mai, PID:8757 java.lang.IllegalStateException:指定的子代已經有一個父代。 您必須先在孩子的父母上調用removeView()。
在android.view.ViewGroup.addViewInner(ViewGroup.java:4417)
在android.view.ViewGroup.addView(ViewGroup.java:4258)
在android.view.ViewGroup.addView(ViewGroup.java:4230)
在com.example.paul_2.a5mai.MainActivity $ 1.onClick(MainActivity.java:127)
在android.view.View.performClick(View.java:5637)
在android.view.View $ PerformClick.run(View.java:22429)
在android.os.Handler.handleCallback(Handler.java:751)
在android.os.Handler.dispatchMessage(Handler.java:95)
在android.os.Looper.loop(Looper.java:154)
在android.app.ActivityThread.main(ActivityThread.java:6119)
在java.lang.reflect.Method.invoke(本機方法)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
您認為您每次都得到相同的數字,但您卻沒有。 您正在將所有內容添加到第一行,但是您只能在第一行的頂部看到第一次顯示的那些圖像。 在按鈕偵聽器中創建ImageView結束時,將ImageView視圖添加到第二行和第三行,您將生成帶有隨機圖像的新行:
StartBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView imgView = new ImageView(MainActivity.this);
Random rand = new Random();
int rndInt = rand.nextInt(3) + 1; // n = the number of images, that start at idx 1
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setImageResource(id);
firstRow.addView(imgView, params1);
ImageView imgView2 = new ImageView(MainActivity.this);
Random rand2 = new Random();
int rndInt2 = rand2.nextInt(3) + 1;
String imgName2 = "img" + rndInt2;
int id2 = getResources().getIdentifier(imgName2, "drawable", getPackageName());
imgView2.setImageResource(id2);
secondRow.addView(imgView2, params1);
ImageView imgView3 = new ImageView(MainActivity.this);
Random rand3 = new Random();
int rndInt3 = rand3.nextInt(3) + 1;
String imgName3 = "img" + rndInt3;
int id3 = getResources().getIdentifier(imgName3, "drawable", getPackageName());
imgView3.setImageResource(id3);
thirdRow.addView(imgView3, params1);
final Animation animSlide = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide);
firstRow.startAnimation(animSlide);
secondRow.startAnimation(animSlide);
thirdRow.startAnimation(animSlide);
}
});
編輯刪除視圖,然后添加新視圖
即:
firstRow.removeAllViews();
firstRow.addView(imgView, params1);
要么
firstRow.removeView(v);
firstRow.addView(imgView, params1);
要么
firstRow.removeAllViewsInLayout();
firstRow.addView(imgView, params1);
不要每次都添加ImageViews。 問題是,第一次添加的圖像不會被刪除。 所以它仍然顯示。
在按鈕單擊之外,僅執行一次以下操作。
ImageView imgView = new ImageView(MainActivity.this);
ImageView imgView2 = new ImageView(MainActivity.this);
ImageView imgView3 = new ImageView(MainActivity.this);
現在,在您的click事件中,獲取圖像視圖並像這樣更改資源
imgView.setImageResource(R.drawable.bike);
不要一次又一次地添加視圖。 這些語句只能在您的代碼中執行一次
thirdRow.addView(imgView3, params1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.