【问题标题】:How Can I Some ImageView Add to relativeLayout如何将一些 ImageView 添加到 relativeLayout
【发布时间】:2016-06-30 22:50:37
【问题描述】:

我想将一些 ImageViews 添加到 RelativeLayout 视图。

我使用的代码如下:

Thread thread = new Thread(new Runnable() {
        ArrayList<ImageView> icons = new ArrayList<ImageView>();

        @Override
        public void run() {
            for (int i = 0; i < 20; i++) {

                ImageView imageView1 = new ImageView(G.context);
                imageView1.setImageResource(R.drawable.music_icon);
                imageView1.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT));
                imageView1.getLayoutParams().width = (int) convertDpToPixel(20);
                imageView1.getLayoutParams().height = (int) convertDpToPixel(20);

                icons.add(imageView1);

                Log.i("LOG", "Icons Size: " + icons.size());
                Log.i("LOG", "I: " + i);

                relativeLayout.addView(icons.get(i));

                icons.get(i).startAnimation(animationMusic);

                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    thread.start();

第一次循环运行正常,但第二次出现错误:

relativeLayout.addView(icons.get(i));

问题出在哪里?

【问题讨论】:

    标签: android imageview add android-relativelayout


    【解决方案1】:

    一个主要问题是从后台线程操作视图(例如添加视图)。您可以通过使用 Android 的 AsyncTask 对象并通过 onPostExecute 方法进行任何 UI 操作来获得类似的异步行为。

    如果不了解您正在做什么,就很难找到适合您的解决方案。我主要是指出您的代码的一个主要问题。

    以下是尝试以一种可能在 Android 环境中更好地工作的方式重写您的代码:

    public void addImageViews() {
        for(int i = 0; i < 20; i++) {
            asyncAdd(i, G.context);
        }
    }
    
    protected void asyncAdd(final int index, final Context c) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    Thread.sleep(3000 * index);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }
    
            @Override
            protected void onPostExecute(Void result) {
                relativeLayout.addView(generateImageView(c));
            }
    
        }.execute();
    }
    
    protected ImageView generateImageView(Context c) {
        ImageView imageView1 = new ImageView(c);
        imageView1.setImageResource(R.drawable.music_icon);
        imageView1.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT));
        imageView1.getLayoutParams().width = (int) convertDpToPixel(20);
        imageView1.getLayoutParams().height = (int) convertDpToPixel(20);
        return imageView1;
    }
    

    【讨论】:

    • 你能看一个例子吗?
    • 谢谢亲爱的朋友。我试过你的代码,它很好。但我无法将我的动画用于 imageView。你能帮忙吗?
    • 你到底想用动画做什么?说实话,这段代码有点奇怪,把图像放在一起并添加动画......可能有一种更简洁的方法来实现你所追求的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多