在 Android 中使用自动生成的 ImageView 创建图库

Making a gallery with automatically generated ImageView s in Android

我正在编写一个 Android 应用程序,用于使用相机拍照并生成 ImageView 以供预览。

这就是我拍照并放入的方式 ImageView:

public class TakePhoto extends Activity {

    private static final int CAMERA_REQUEST = 1888; 

    Button btnCaputre;
    ImageView image;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_photo);

         btnCaputre = (Button)findViewById(R.id.btnCapture);
         image = (ImageView)findViewById(R.id.imageView1);

             btnCaputre.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                 startActivityForResult(cameraIntent, CAMERA_REQUEST);      
                 }
            });
            }

      protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
            if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
                Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                image.setImageBitmap(photo);          
            }  
        } 

这是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="100dp" >

    <Button
        android:id="@+id/btnCapture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Camera" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

如何在同一张Activity中动态添加更多ImageView(用于预览制作的新照片)?

编辑: 这是为动态创建的 ImageView:

实现 setOnClickListener 的新编辑代码
public class TakePhoto extends Activity {

    private static final int CAMERA_REQUEST = 1888; 

    Button btnCaputre;
    ImageView image;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_photo);

         btnCaputre = (Button)findViewById(R.id.btnCapture);
         image = (ImageView)findViewById(R.id.imageView1);

             btnCaputre.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                 startActivityForResult(cameraIntent, CAMERA_REQUEST);      
                 }
            });

        image.setOnClickListener(new OnClickListener() {                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub          

                    Log.e("Image id","-->"+ image.getId());

                }
            });
            }

      protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
          if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
          Bitmap photo = (Bitmap) data.getExtras().get("data"); 
          image = new ImageView(this);
          LayoutParams param=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
          image.setLayoutParams(param);
          image.setImageBitmap(photo);  
          image.setTag(java.util.UUID.randomUUID().toString());
          mLayout.addView(image); }  

        } 

要动态添加 Imageview,您需要对代码进行以下更改

布局中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="100dp" >

    <Button
        android:id="@+id/btnCapture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Camera" />

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

</LinearLayout>

中Java代码:

在 OnCreate() 中:从 xml

中获取线性布局视图 Viewgroup
LinearLayout mLayout=(LinearLayout)findViewById(R.id.layout);

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
            if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
                Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                 ImageView image=new ImageView(this);
                 LayoutParam param=new LayoutParam(LayoutParam.WRAP_CONTENT,LayoutParam.WRAP_CONTENT);
image.setLayoutParam(param);
image.setImageBitmap(photo);  
mLayout.addView(image);      
            }  
        }