本文主要是介绍加载网络图片显示大图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.将图片的uri列表和下标传给ImagePagerActivity
public void imageBrower(int position, ArrayList<String> urls2) {Intent intent = new Intent(this, ImagePagerActivity.class); intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2); intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position); this.startActivity(intent); }
2.给ImagePagerActivity布局:layout里布局:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.yangzi.homeimprovement.imagedemo.HackyViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black"/> <TextView android:id="@+id/indicator" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="@android:color/transparent" android:gravity="center" android:text="@string/viewpager_indicator" android:textColor="@android:color/white" android:textSize="18sp"/> <!-- viewpager_indicator===>> string文件夹下: <string name="viewpager_indicator">%1$d/%2$d</string>--> </FrameLayout>
3.关联布局--->写一个继承FragmentStatePagerAdapter的类显示图片
--->更新下标
/** * 图片查看器 */ public class ImagePagerActivity extends FragmentActivity {private static final String STATE_POSITION = "STATE_POSITION"; public static final String EXTRA_IMAGE_INDEX = "image_index"; public static final String EXTRA_IMAGE_URLS = "image_urls"; private ViewPager mPager; private int pagerPosition; private TextView indicator; @Override public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.image_detail_pager); pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0); ArrayList<String> urls = getIntent().getStringArrayListExtra(EXTRA_IMAGE_URLS);//intent传过来的显示大图的Uri mPager = (ViewPager) findViewById(R.id.pager); ImagePagerAdapter mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), urls); mPager.setAdapter(mAdapter); indicator = (TextView) findViewById(R.id.indicator); CharSequence text = getString(R.string.viewpager_indicator, 1, mPager.getAdapter().getCount()); indicator.setText(text); // 更新图片的下标 mPager.setOnPageChangeListener(new OnPageChangeListener() {@Override public void onPageScrollStateChanged(int arg0) {}@Override public void onPageScrolled(int arg0, float arg1, int arg2) {}@Override public void onPageSelected(int arg0) {CharSequence text = getString(R.string.viewpager_indicator, arg0 + 1, mPager.getAdapter().getCount());// 后面的参数填充参数1 indicator.setText(text); }}); if (savedInstanceState != null) {pagerPosition = savedInstanceState.getInt(STATE_POSITION); }mPager.setCurrentItem(pagerPosition); }@Override public void onSaveInstanceState(Bundle outState) {outState.putInt(STATE_POSITION, mPager.getCurrentItem()); }// 显示图片 private class ImagePagerAdapter extends FragmentStatePagerAdapter {//当 Fragment 对应的数据集发生改变时,用它较好 public ArrayList<String> fileList; public ImagePagerAdapter(FragmentManager fm, ArrayList<String> fileList) {super(fm); this.fileList = fileList; }@Override public int getCount() {return fileList == null ? 0 : fileList.size();//如果uri列表不为空,得到uriList的大小 }@Override public Fragment getItem(int position) {String url = fileList.get(position); return ImageDetailFragment.newInstance(url); }} }
4.显示图片 这里用到了使用Android-Universal-Image-Loader 图片异步加载类库,
/** * 单张图片显示Fragment */ public class ImageDetailFragment extends Fragment {private String mImageUrl; private ImageView mImageView; private ProgressBar progressBar; private PhotoViewAttacher mAttacher; public static ImageDetailFragment newInstance(String imageUrl) {final ImageDetailFragment f = new ImageDetailFragment(); final Bundle args = new Bundle(); args.putString("url", imageUrl); f.setArguments(args);//Fragment不要用构造方法传参 return f; }@Override public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); //在构造方法里用setArguments传递过来的参数不为空,就得到大图的Url mImageUrl = getArguments() != null ? getArguments().getString("url") : null; }@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {final View v = inflater.inflate(R.layout.image_detail_fragment, container, false); mImageView = (ImageView) v.findViewById(R.id.image); mAttacher = new PhotoViewAttacher(mImageView); mAttacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {@Override public void onPhotoTap(View arg0, float arg1, float arg2) {getActivity().finish(); }}); progressBar = (ProgressBar) v.findViewById(R.id.loading); return v; }@Override public void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState); ImageLoader.getInstance().displayImage(mImageUrl, mImageView, new SimpleImageLoadingListener() {@Override public void onLoadingStarted(String imageUri, View view) {progressBar.setVisibility(View.VISIBLE); }@Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) {String message = null; switch (failReason.getType()) {case IO_ERROR:message = "下载错误"; break; case DECODING_ERROR:message = "图片无法显示"; break; case NETWORK_DENIED:message = "网络有问题,无法下载"; break; case OUT_OF_MEMORY:message = "图片太大无法显示"; break; case UNKNOWN:message = "未知的错误"; break; }Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show(); progressBar.setVisibility(View.GONE); }@Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {progressBar.setVisibility(View.GONE); mAttacher.update(); }}); } }
这篇关于加载网络图片显示大图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!