如何在Android中使用图像滑块实现类似画廊视图,并在Android中查看Pager

时间:2013-02-18 07:33:08

标签: android android-layout android-widget android-viewpager android-gallery

Hai我有可绘制文件夹中的图像列表。我需要实现从左到右逐个滑动的图像。当用户点击任何一个图像时,我需要显示大图像。我正面临问题在点击监听器上显示大图像。我发布了代码和屏幕截图我需要的是任何人都可以帮助我。

  public class MainActivity extends Activity {

LinearLayout myGallery;

public final Integer[] mThumbIds = { R.drawable.blue_snow_icon,
        R.drawable.coffee_fireworks_icon, R.drawable.blue_snow_icon,
        R.drawable.coffee_fireworks_icon, };
ImageView img;
HorizontalScrollView scrool;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myGallery = (LinearLayout) findViewById(R.id.mygallery);
    scrool = (HorizontalScrollView) findViewById(R.id.horizantalScrool);
    scrool.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(MainActivity.this, v.getId(), 
                  Toast.LENGTH_SHORT)
                    .show();

        }
    });

    img = (ImageView) findViewById(R.id.imageView1);
    for (int i = 0; i < mThumbIds.length; i++)
        myGallery.addView(insertPhoto(mThumbIds[i]));
}

View insertPhoto(Integer mThumbIds2) {
    Bitmap bm = decodeSampledBitmapFromUri(this.getResources(), mThumbIds2,
            220, 220);

    LinearLayout layout = new LinearLayout(getApplicationContext());
    layout.setLayoutParams(new LayoutParams(250, 250));
    layout.setGravity(Gravity.CENTER);

    ImageView imageView = new ImageView(getApplicationContext());
    imageView.setLayoutParams(new LayoutParams(220, 220));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setImageBitmap(bm);
    imageView.setId(mThumbIds2);
    imageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        }
    });

    layout.addView(imageView);
    return layout;
}

public Bitmap decodeSampledBitmapFromUri(Resources res, Integer resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

public int calculateInSampleSize(

BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) 
                                                                         reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) 
                                        reqWidth);
        }
    }

    return inSampleSize;
}

  }


  and my xml file


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<HorizontalScrollView
    android:id="@+id/horizantalScrool"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/mygallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" />
</HorizontalScrollView>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_below="@+id/horizantalScrool"
    android:src="@drawable/ic_launcher" />

   </RelativeLayout>

when  i click on the top image i need to display big image as shown in the bellow screen

当我点击顶部图像时,我需要显示大图像,如下图所示。请任何人帮助我

1 个答案:

答案 0 :(得分:0)

img = (ImageView) findViewById(R.id.imageView1);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    img.setImageBitmap(bm);
    img.setId(mThumbIds);
}