Android:使用Horizo​​ntalScrollview在图库中打开所选图片

时间:2014-01-27 12:09:49

标签: android click imageview gallery horizontalscrollview

我有一个用horizo​​ntalScrollview创建的图库。而且我不知道如何在图像视图库中单击它,它会在其他图库中的所选图像的新活动中打开。这是我的代码:

Main.class

    float ancho = 0, alto = 0;

    LinearLayout galeria = (LinearLayout) findViewById(R.id.galeria);

    try {
        final String[] listaImagenes = getAssets().list(directorioImagenes);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int anchoPantalla = metrics.widthPixels;

        if (listaImagenes.length > 0) {

            for (String nombreImagen : listaImagenes) {

                InputStream is = getAssets().open(directorioImagenes + "/" + nombreImagen);
                final Bitmap bitmap = BitmapFactory.decodeStream(is);
                final ImageView imageView = new ImageView(getApplicationContext());

                alto = bitmap.getHeight();
                ancho = bitmap.getWidth();

                final float calculo = ancho / (alto / ALTO_IMAGEN);

                imageView.setLayoutParams(new LinearLayout.LayoutParams(
                        anchoPantalla, LayoutParams.MATCH_PARENT));

                imageView.setImageBitmap(bitmap);
                imageView.setPadding(2, 2, 2, 2);
                imageView.setBackgroundColor(colorResources);

                imageView.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {

                        Intent i = new Intent(DetalleMonumento.this, AbrirImagen.class);;
                        startActivity(i);
                    }
                });

                galeria.addView(imageView);
            }

            ImageView iv2 = new ImageView(this);
            iv2.setLayoutParams(new LinearLayout.LayoutParams(
                    anchoPantalla, LayoutParams.MATCH_PARENT));
        }  

main.xml

       <LinearLayout
            android:id="@+id/fondoGaleria"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:paddingBottom="5dp" >

            <HorizontalScrollView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:scrollbars="none" >

                <LinearLayout
                    android:id="@+id/galeria"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@color/estandar"
                    android:orientation="horizontal"
                    android:padding="2dp" />
            </HorizontalScrollView>
        </LinearLayout>

感谢

1 个答案:

答案 0 :(得分:0)

for (final String nombreImagen : listaImagenes) {//make nombreImagen final

                InputStream is = getAssets().open(directorioImagenes + "/" + nombreImagen);
                final Bitmap bitmap = BitmapFactory.decodeStream(is);
                final ImageView imageView = new ImageView(getApplicationContext());

                alto = bitmap.getHeight();
                ancho = bitmap.getWidth();

                final float calculo = ancho / (alto / ALTO_IMAGEN);

                imageView.setLayoutParams(new LinearLayout.LayoutParams(
                        anchoPantalla, LayoutParams.MATCH_PARENT));

                imageView.setImageBitmap(bitmap);
                imageView.setPadding(2, 2, 2, 2);
                imageView.setBackgroundColor(colorResources);

                imageView.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {

                        Intent i = new Intent(DetalleMonumento.this, AbrirImagen.class);
                        i.putExtra("selectedImage",nombreImagen );//Put extra
                        startActivity(i);
                    }
                });

                galeria.addView(imageView);
            }

onCreate的{​​{1}}中获取此字符串:

AbrirImagen