【发布时间】:2014-11-20 13:23:22
【问题描述】:
我编写了一个程序,在该程序中我显示图像的网格视图,当我单击图像时,会打开另一个活动,其中有一个 viewpager 可以滑动我的图像,并且在同一页面的底部还有一个水平滚动视图具有在我的 gridview 中看到的相同图像,直到这里一切正常,是的,所有图像都从我的 sdcard 中读取 现在,当我单击水平滚动视图中的图像时,在我的 viewpager 中显示的图像应该会发生变化,例如 s4 的画廊,所以我不知道该怎么做,需要一些帮助
我的代码:在我的 imageview.onclick 中,我已经用我的 viewpager 设置了当前的 Items 位置,但它不起作用
public class FullScreenViewActivity extends Activity {
private Utils utils;
private FullScreenImageAdapter adapter;
private ViewPager viewPager;
LinearLayout myGallery;
ImageView iView;
int i;
String path;
int id;
File[] files;
HorizontalScrollView scrollView;
int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen_view);
viewPager = (ViewPager) findViewById(R.id.pager);
myGallery = (LinearLayout) findViewById(R.id.mygallery);
scrollView = (HorizontalScrollView) findViewById(R.id.horizontal1);
scrollView.setBackground(getResources().getDrawable(R.drawable.border));
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory().getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/Pictures/raw";
Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG)
.show();
File targetDirector = new File(targetPath);
files = targetDirector.listFiles();
for (File file : files) {
myGallery.addView(insertPhoto(file.getAbsolutePath()));
}
utils = new Utils(getApplicationContext());
Intent i = getIntent();
position = i.getIntExtra("position", 0);
adapter = new FullScreenImageAdapter(FullScreenViewActivity.this,
utils.getFilePaths());
viewPager.setAdapter(adapter);
// displaying selected image first
viewPager.setCurrentItem(position);
}
private View insertPhoto(final String path) {
final Bitmap bm = decodeSampledBitmapFromUri(path, 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(i);
// iView.setId(i);
// viewPager.setId(i);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int id = v.getId();
viewPager.setCurrentItem(id);
}
});
layout.addView(imageView);
return layout;
}
public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth,
int reqHeight) {
Bitmap bm = null;
// First decode with inJustDecodeBounds=true to check dimensions final
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
}
任何建议都会有很大帮助 谢谢你
【问题讨论】: