【问题标题】:I want to display few images randomly in java (android)我想在java(android)中随机显示一些图像
【发布时间】:2017-05-17 12:11:03
【问题描述】:
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 随机生成这些图像。该代码有效,但问题是我第一次单击按钮时生成的图像仍然存在,下次单击按钮时它们不会更改。每次单击按钮时我都需要生成。我能做些什么?我应该在哪里声明对象?我还能怎么做?提前谢谢!

将对象的声明移到 onClick 事件之外后的 logcat: 05-17 13:49:26.236 8757-8757/com.example.paul_2.a5mai E/AndroidRuntime: 致命异常: main 进程: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)

【问题讨论】:

  • 我忘了提我有 3 张图片复制到“drawable”文件夹中。
  • 所以在我提出一个编码解决方案只是为了确认之前,当你点击按钮时,你的代码会生成带有图像的 imageViews(这对你来说完美吗?)并且你希望随机分发 3 个图像每次单击按钮时都会出现 3 个图像视图。对吗?
  • 第一次单击按钮时,它会随机生成 3 张图像并用它们制作动画。第二次动画再次开始,但问题是与第一次相同的图像。随机函数又不行了……
  • 如何解决下一个错误:java.lang.IllegalStateException: The specified child has a parent.您必须首先在孩子的父母上调用 removeView()。 ?

标签: java android oop


【解决方案1】:

不要每次都添加 ImageViews。问题是,第一次添加的图像没有被删除。所以它仍然显示。

在您的按钮点击之外只执行一次以下操作。

ImageView imgView = new ImageView(MainActivity.this);
ImageView imgView2 = new ImageView(MainActivity.this);
ImageView imgView3 = new ImageView(MainActivity.this);

现在在您的点击事件中,获取图像视图并像这样更改资源

imgView.setImageResource(R.drawable.bike);

不要一次又一次地添加视图。这些语句应该在你的代码中只执行一次

thirdRow.addView(imgView3, params1);

【讨论】:

  • 还有我应该写的addView?
  • 以及如何解决下一个错误:java.lang.IllegalStateException: The specified child has a parent.您必须首先在孩子的父母上调用 removeView()。 ?
【解决方案2】:

您认为每次都得到相同的数字,但事实并非如此。您正在将所有内容添加到第一行,但您只能看到那些第一次显示在第一行顶部的图像。在按钮侦听器中创建 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);

【讨论】:

  • 如何解决下一个错误:java.lang.IllegalStateException: The specified child has a parent.您必须首先在孩子的父母上调用 removeView()。 ?
  • 把整个日志贴在可以清楚地看到哪个视图有问题的地方。
  • 我编辑了帖子并添加了日志。你能看看这个吗?我真的需要帮助。
  • “将对象的声明移到 onClick 事件之外:”是什么意思?我测试了我发布的答案,没有任何异常。如果你试试我的答案,它只改变了三行,没有例外,你会得到你的行
  • 我编辑了我的整个代码,现在看看这个,你会看到我得到的异常。我现在能做什么?附:我不想像您发布的那样在不同的行中显示图像(稍后我需要其余的行)。
猜你喜欢
  • 1970-01-01
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 2013-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多