在Android中使用自动生成的ImageView制作图库 [英] Making a gallery with automatically generated ImageView s in Android

查看:53
本文介绍了在Android中使用自动生成的ImageView制作图库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

这是我的布局:

<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(用于预览新制作的照片)?

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

这是针对动态创建的ImageView实现的setOnClickListener的新编辑代码:

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

        } 

推荐答案

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

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

在布局中:

<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

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

这篇关于在Android中使用自动生成的ImageView制作图库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆