海我在可绘制文件夹中有图像列表。我需要实现图像从左到右一一滑动。当用户单击任何一个图像时,我需要显示大图像。我在单击时遇到问题显示大图像的监听器。下面我发布了代码和屏幕截图我需要的是任何人都可以帮助我。
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>
当我单击顶部图像时,我需要显示大图像,如下面的屏幕所示。谁能帮帮我