【问题标题】:how to implement like gallery view in android using the image slider and view Pager in android如何使用图像滑块在 android 中实现类似画廊视图并在 android 中查看 Pager
【发布时间】:2013-02-18 07:33:07
【问题描述】:

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

  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>

当我单击顶部图像时,我需要显示大图像,如下面的屏幕所示。谁能帮帮我

【问题讨论】:

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


    【解决方案1】:
    img = (ImageView) findViewById(R.id.imageView1);
    imageView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        img.setImageBitmap(bm);
        img.setId(mThumbIds);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多