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

时间:2015-04-29 08:45:42

标签: android android-camera android-imageview

我正在编写一个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>

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

修改 这是新编辑的代码,其中setOnClickListener的实现用于动态创建的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 个答案:

答案 0 :(得分:3)

要动态添加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

获取Linearlayout视图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);      
            }  
        }