Making a gallery with automatically generated ImageView s in Android

自古美人都是妖i 提交于 2019-12-13 04:35:30

问题


I'm writing an Android application for taking photos with camera and generating ImageViews for previewing them.

This is how I take pictures and put them in 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);          
            }  
        } 

This is my layout:

<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>

How to dynamically add more ImageView (for previewing the new photos made) in the same Activity?

EDIT: This is the new edited code with implementation of setOnClickListener for dynamically created 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);      
                 }
            });

        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); }  

        } 

回答1:


For adding Imageview dynamically you need to do following changes in your code

In Layout:

<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>

In Java Code:

In OnCreate(): get the Linearlayout view Viewgroup from the xml

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);      
            }  
        }


来源:https://stackoverflow.com/questions/29938640/making-a-gallery-with-automatically-generated-imageview-s-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!