P Srinivas Goud
P Srinivas Goud

Reputation: 896

How to display clicked image from a set of images from a linear layout?

i got images from SD card folder and added them to LinearLayout dynamically which is inside a HorizontalScrollview.now when i click an image i want to show that particular image in another ImageView in the same activity as big image. how can i do that? i tried by getting getId(),getTag() but i am unable to do, please give me suggestions. Note:I dont want use gallery widget because of center-locking feature. My code :

    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>

Upvotes: 2

Views: 2318

Answers (2)

Yugandhar Babu
Yugandhar Babu

Reputation: 10349

Add below line before imageView.setOnClickListener(new View.OnClickListener() {

imageView.setId(i);

Use below lines in place of iv.setImageResource(v.getId());

int id = v.getId();
Bitmap b = decodeFile(sdDirFiles[id].getAbsoluteFile()); // this gives small image bcoz you implemented decodeFile like that.
iv.setImageBitmap(b);

or

int id = v.getId();                 
Uri uri = Uri.fromFile(sdDirFiles[id].getAbsoluteFile()); // it will produce original resolution or fit to screen
iv.setImageURI(uri);

I tested this code, its working fine.

Upvotes: 1

WarrenFaith
WarrenFaith

Reputation: 57672

You need to do two things:

  1. you need to toggle the visibility of your ImageView so that it will work like an overlay. Make it visible if you touched one of your images and turn it off if you touch your big ImageView (just a sample behavior...)
  2. Use the setTag() method to store the ID, in your case the variable i in the small ImageView. In your onClick method, you get the tag by getTag() and than use it to load the image with the ID i from the sdDirFiles list.

that should be all you need.

Sample code (read the comments!):

    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);
        imageView.setTag(i); // SET THE TAG!
        path = sdDirFiles[i].getAbsolutePath();
        imageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // set the big imageview with the clicked image
                Bitmap b = decodeFile(sdDirFiles[Integer.valueOf(v.getId())].getAbsoluteFile());
                iv.setImageBitmap(b);
            }
        });
        linearLayout.addView(imageView);
    }

Upvotes: 0

Related Questions