내 레이아웃에서 두 개의 ImageView를 만들었습니다. imageviewTop
및 imageviewBottom
이라고 부릅니다.
두 개의 이미지를 드로어 블에 저장했습니다 (green_image.png 및 red_image.png). 버튼을 클릭하면 ImageView 중 하나가 임의로 선택되고 green_image에서 red_image로 변경됩니다.무작위로 두 개의 ImageView 중 하나를 선택하고 ResourceImage를 업데이트하십시오.
switch/case 문을 생성하고 1 또는 2와 같은 임의의 숫자를 생성하려고 이미 시도했습니다.
이 숫자를 기반으로 case 문은 위쪽 또는 아래쪽 이미지를 업데이트합니다.
이것은 2 개의 ImageViews에서 잘 작동하지만, 100이 될 경우를 대비하여 코드에서 100 개의 사례를 만들어야합니다.
더 동적 인 옵션을 찾고 있습니다.
ImageView의 이미지를 업데이트하는 방법을 알고 있습니다. 가능한 경우 하나의 ImageView를 임의로 선택하는 방법에 대해 고민 중입니다.
public class MainActivity extends Activity {
ImageView imagevieTop, imageviewBottom;
Button randomButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagevieTop = (ImageView) findViewById(R.id.imageViewTop);
imageviewBottom = (ImageView) findViewById(R.id.imageViewBottom);
randomButton = (Button) findViewById(R.id.buttonRandom);
randomButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// randomly select one of the two imageviews
// for example: randomly selected imageviewTop
// set imageresource red_image to imageviewTop
//at next start up it would select either top or bottom, 50%-50% and then assign the image to it
}
});
}
}
ImageView의 좌표와 그 내부의 이미지를 임의로 랜덤하게 생성하는 이유는 무엇입니까? 또는 int a = new Random(). nextInt (100)와 같은 코드를 작성하면됩니다. x = (x - x % 4)/4 * 배수; y = x % 4 * 다중; imgId = a; 다음 이미지의 좌표와 이미지를 간단하게 설정하십시오. –
이미 선행 이미지를 설정하고 싶습니다. 예를 들어 시작시 특정 좌표에 4 개의 녹색 이미지가 있어야하고 버튼 클릭시 빨간색 중 하나가 표시되어야합니다. – Gexo