简体   繁体   English

Android产生多个具有不同位置的ImageView

[英]Android Spawn Multiple ImageView with Different Position

I'm creating an android application and I want to spawn multiple imageviews with same image and different position.I have a class for the image and with for loop I created 15 images and add it to ArrayList and set differen position with enhanced for loop ,but now they are 15 images with only one position and it seems like 1 image.Look at my code 我正在创建一个android应用程序,并希望生成具有相同图像和不同位置的多个图像视图。我有一个图像类,并使用for循环创建了15张图像并将其添加到ArrayList中,并通过增强的for循环设置了不同的位置,但是现在它们是15张图片,只有一个位置,好像是1张图片。看看我的代码

This is the main class 这是主班

private ImageView pumpkinImg;
private int imageY = 50;
private int imageX = 50;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_play_second);

    pumpkinImg = (ImageView) findViewById(R.id.pumpkin_second);

    ArrayList<Pumpkin> pumpkins = new ArrayList<Pumpkin>();
    for(int i = 0; i < 15; i ++){
        Pumpkin p = new Pumpkin(pumpkinImg);

        pumpkins.add(p);
    }
    for(Pumpkin pmk : pumpkins){

        pmk.setV(pumpkinImg);
        pmk.setPosX(pmk.getV(), imageX);
        pmk.setPosY(pmk.getV(), imageY);

        imageX += 20;
        imageY += 20;
    }


}

And here is my pumpkin class 这是我的南瓜课

public class Pumpkin {

private int x;
private int y;
ImageView v;

public Pumpkin(ImageView v){


}

public void setPosX(ImageView v,int posX){
    v.setX(posX);
}
public void setPosY(ImageView v,int posY){
    v.setY(posY);
}

public ImageView getV() {
    return v;
}

public void setV(ImageView v) {
    this.v = v;
}

} }

Here is my XML FILE : 这是我的XML FILE:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="150dp"
    android:text="@string/level_two_welcome"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ImageView
    android:id="@+id/pumpkin_second"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/level_2" />

</RelativeLayout>

you are only creating one Instance of an ImageView and are referencing exactly this instance! 您仅创建一个ImageView实例,并且完全引用了该实例! In addition, you have to add the Images to a proper Android-layout! 此外,您必须将图片添加到适当的Android布局中!

The current layout you're referencing in your Activity-class is 您在“活动”类中引用的当前布局是

R.layout.activity_play_second

Can you provide, which layout this is? 您能提供这是哪种布局?

It seems, that this layout holds an ImageView 看来,此布局包含一个ImageView

R.id.pumpkin_second

that displays the pumpkin-image. 显示南瓜图像。 I think, this is not what you want! 我认为,这不是您想要的!

Put the dynamic creation of all ImageViews into your for-Loop: 将所有ImageView的动态创建放入循环中:

//create ImageView from drawable resource
ImageView iv = new ImageView( this );
iv.setBackgroundResource( R.drawable.my_resource );

//add to main layout
( (ViewGroup)this.findViewById( R.layout.activity_play_second ) ).addView( iv );

Please find a complete code sample - Make sure to adjust the resource identifiers! 请找到完整的代码示例-确保调整资源标识符!

    setContentView( R.layout.my_layout );
    ViewGroup view = (ViewGroup)findViewById( R.id.my_container_id_in_my_layout );

    ImageView iv = new ImageView( this );
    iv.setImageResource( R.drawable.my_drawable_resource );

    ImageView iv2 = new ImageView( this );
    iv2.setImageResource( R.drawable.my_drawable_resource );

    view.addView( iv  );
    view.addView( iv2 );

You need an ID of an Container view that lies INSIDE your layout-file. 您需要位于布局文件内部的“容器”视图的ID。 Here is an example layout file ( res/layout/my_layout.xml ): 这是一个示例布局文件(res / layout / my_layout.xml):

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/my_container_id_in_my_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        />

</RelativeLayout>

Hope this helps? 希望这可以帮助?

Greetings 问候

Christopher 克里斯托弗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 设置ImageView位置 - Android - Set an ImageView position - Android 与 ImageView 位置相比,OnTouch 给出不同的位置 - OnTouch giving different position, compared to ImageView position Android在imageView的错误位置上绘图 - Android is drawing on a wrong position of imageView 在Android中更改ImageView位置时出现问题 - Problems changing the position of an ImageView in Android 在不同的进程上使用不同的 JDK 位置可能会导致 Gradle 产生多个守护进程 (Android 4.1.2) - Using different JDK locations on different processes might cause Gradle to spawn multiple daemons (Android 4.1.2) 如何在保持Android中每个ImageView位置的同时将多个ImageView保存为一个位图? - How to save multiple ImageViews as one Bitmap while maintaining position of each ImageView in Android? 在Android的自定义视图中将大小和位置设置为imageview - Set size and position to imageview in custom view in Android 如何在ListView Android中获取位置imageView - How to get position imageView in ListView Android 设置以编程方式创建的imageview android的位置 - set position of programmatically created imageview android 如何在 android 工作室中更改 imageview 上的文本 position? - How to change position of text on imageview in android studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM