如何在android中创建动态图像视图的幻灯片?

时间:2013-09-27 21:39:34

标签: android android-imageview

我正在处理应用程序,我需要从库中获取图片并将其显示到动态ImageViews中。我成功地做到了这一点,但现在我必须创建一个幻灯片放映,其中包含从图库中提取的图片。我怎么能这样做?

这是我的代码:

LinearLayout lLayout;
Bitmap yourSelectedImage;
int flag=1;
ImageView slidingimage;
int imageId=1;
int imageindex=1;

int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    createLayout();


}

public void createLayout(){

    LinearLayout parentLayout = (LinearLayout)findViewById(R.id.Playout);
       lLayout = new LinearLayout(this);
        lLayout.setOrientation(LinearLayout.HORIZONTAL);


        LinearLayout.LayoutParams myLayoutParams = new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

        myLayoutParams.leftMargin = 0;
        //myLayoutParams.topMargin = 50 * i;
        myLayoutParams.width = 480;
        myLayoutParams.height = 150;

        lLayout.setLayoutParams(myLayoutParams);

        parentLayout.addView(lLayout);


        btn1 = new Button(this); 
        btn1.setBackgroundResource(R.drawable.add);
        //btn1.setId(i);



        LinearLayout.LayoutParams rel_btn = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            /*rel_btn.leftMargin = 150;
            rel_btn.topMargin = myYPosition;*/
            rel_btn.width = 160;
            rel_btn.height = 150;

            btn1.setLayoutParams(rel_btn);

            lLayout.addView(btn1);
            btn1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Perform action on click   

                    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                    photoPickerIntent.setType("image/*");
                    startActivityForResult(photoPickerIntent, 1);


                }
            });


}

protected void onActivityResult(int requestCode, int resultCode, 
           Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

        switch(requestCode) { 
        case 1:
            if(resultCode == RESULT_OK){  
                Uri selectedImage = imageReturnedIntent.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(
                                   selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();
                yourSelectedImage = BitmapFactory.decodeFile(filePath);

                ImageView img = new ImageView(this);
                LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(160,150);
                img.setLayoutParams(parms);
                img.setId(imageId);

                imageId++;
                img.setImageBitmap(yourSelectedImage);
                lLayout.addView(img, 0);
                flag++;

                if(flag>3){

                    btn1.setVisibility(View.INVISIBLE);
                    createLayout();
                    flag=1;
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以创建所有已拍摄图像的列表,然后在图像视图或列表视图中显示它们,无论您想要什么.......

 ArrayList<Bitmap/or drawable (you choose)> variable1=null;

create a loop
for(till all images are fetched............)
{

Bitmap/Drawable variable2=new Bitmap/Drawable();
variable2=your image from gallery;
variable1.add(variable2);
}

所有图片现已获取并包含在列表中(variable1).... 逐个显示它们或者你想要............