2 slaks SLaks 于 2013.06.13 10:30 提问

如何显示 linear layout 中一组图片里的点击图像

我从SD Card中获取图像,然后把图像动态的添加到HorizontalScrollview中的LinearLayout里。现在当我点击一个图像时,我想在同一个activity中的另一个 ImageView 中,把特定的图像显示成大图像。如何实现?我使用 getting getId(),getTag(),但是没有实现。另外,我不想使用gallery widget ,因为中心锁定功能的缘故。

 package com.pop.cam;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    public class GalleryView extends Activity {
        ImageView iv;
        File[] sdDirFiles;
        LinearLayout linearLayout;
        int i;
        String path;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
            setContentView(R.layout.pictures);
            linearLayout = (LinearLayout) findViewById(R.id.linearView);
            iv = (ImageView) findViewById(R.id.ImageView);
            // getting images from SD card folder
            File sdDir = new File("/sdcard/Pictures/"
                    + MyCameraAppActivity.DIR_NAME);
            sdDirFiles = sdDir.listFiles();
            for (i = 0; i < sdDirFiles.length; i++) {
                final ImageView imageView = new ImageView(this);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setLayoutParams(new LayoutParams(130, 100));
                Bitmap b = decodeFile(sdDirFiles[i].getAbsoluteFile());
                imageView.setImageBitmap(b);
                path = sdDirFiles[i].getAbsolutePath();
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Here i want to set clicked image as big.
                                        //I tried following but not workin
                        iv.setImageResource(v.getId());
                                        iv.setImageURI(Uri.parse(path));
Bitmap b = decodeFile(sdDirFiles[Integer.valueOf(v.getId())].getAbsoluteFile());
                    iv.setImageBitmap(b);
                    }
                });
                linearLayout.addView(imageView);
            }
        }
        // decodes image and scales it to reduce memory consumption
        private Bitmap decodeFile(File f) {
            try {
                // Decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(new FileInputStream(f), null, o);
                // The new size we want to scale to
                final int REQUIRED_SIZE = 70;
                // Find the correct scale value. It should be the power of 2.
                int scale = 1;
                while (o.outWidth / scale / 2 >= REQUIRED_SIZE
                        && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                    scale *= 2;
                // Decode with inSampleSize
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = scale;
                return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
            } catch (FileNotFoundException e) {
            }
            return null;
        }
    }

pictures.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <HorizontalScrollView
        android:id="@+id/horizontalScorllView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <LinearLayout
            android:id="@+id/linearView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>
    </HorizontalScrollView>
    <ImageView
        android:id="@+id/ImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:contentDescription="@string/app_name" >
    </ImageView>
</LinearLayout>

1个回答

u010434246
u010434246   2017.02.14 09:32

记录路径到tag里面,点击imageview 进入大图播放界面时把tag中的路径取出来传到大图播放界面,然后在大图播放界面进行查看

Csdn user default icon
上传中...
上传图片
插入图片