位置: 编程技术 - 正文
推荐整理分享Android UI-开源框架ImageLoader的完美例子(异步加载大量图片)(android ui开发),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:开源android ui界面模板,开源android ui界面模板,android原生开发框架,android开源库,android 开源ui框架,开源android ui界面模板,开源android ui界面模板,android开源库,内容如对您有帮助,希望把文章链接给更多的朋友!
Android开源框架ImageLoader的完美例子
很多人都在讨论如何让图片能在异步加载更加流畅,可以显示大量图片,在拖动ListView的时候不会出现卡的现象。关于ImageLoader这个开源框架的使用有很多网友都介绍过,不过还不够清楚,这里有一个关于这个开源项目的完美例子,ListView的图片加载、GridView的图片加载、ViewPager的图片加载、Gallery画廊的图片加载、Widget的使用。很完善的一个例子,在这里我把所有界面效果做出博客分享出来,需要源码的朋友到我的资源页下载
下载地址: view plaincopy<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android=" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingBottom="dip" android:paddingTop="dip" android:text="@string/label_activity_examples" android:textSize="sp" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="dip" android:onClick="onImageListClick" android:text="@string/button_image_list" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="dip" android:onClick="onImageGridClick" android:text="@string/button_image_grid" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="dip" android:onClick="onImagePagerClick" android:text="@string/button_image_pager" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="dip" android:onClick="onImageGalleryClick" android:text="@string/button_image_gallery" /> </LinearLayout> </ScrollView> 列表异步加载图片效果
/.8._Universal_Image_Loader_Demo/res/layout/ac_image_list.xml
[html] view plaincopy<?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android=" android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> /.8._Universal_Image_Loader_Demo/res/layout/item_list_image.xml[html] view plaincopy<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageView android:id="@id/image" android:layout_width="dip" android:layout_height="dip" android:layout_margin="3dip" android:adjustViewBounds="true" android:contentDescription="@string/descr_image" android:scaleType="centerCrop" /> <TextView android:id="@id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="left|center_vertical" android:layout_marginLeft="dip" android:textSize="sp" /> </LinearLayout> GridView异步加载图片显示/.8._Universal_Image_Loader_Demo/res/layout/ac_image_grid.xml
[html] view plaincopy<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android=" android:id="@id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:horizontalSpacing="4dip" android:numColumns="3" android:stretchMode="columnWidth" android:verticalSpacing="4dip" android:padding="4dip" />/.8._Universal_Image_Loader_Demo/res/layout/item_grid_image.xml
[html] view plaincopy<?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android=" android:id="@id/image" android:layout_width="fill_parent" android:layout_height="dip" android:adjustViewBounds="true" android:contentDescription="@string/descr_image" android:scaleType="centerCrop" />ViewPager异步加载图片显示
/.8._Universal_Image_Loader_Demo/res/layout/ac_image_pager.xml
[html] view plaincopy<?xml version="1.0" encoding="utf-8"?> <android.support.v4.view.ViewPager xmlns:android=" android:id="@id/pager" android:layout_width="fill_parent" android:layout_height="fill_parent" /> /.8._Universal_Image_Loader_Demo/res/layout/item_pager_image.xml[html] view plaincopy<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android=" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="1dip" > <ImageView android:id="@id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:adjustViewBounds="true" android:contentDescription="@string/descr_image" /> <ProgressBar android:id="@id/loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:visibility="gone" /> </FrameLayout>Gallery画廊异步加载图片显示
/.8._Universal_Image_Loader_Demo/res/layout/ac_image_gallery.xml
[html] view plaincopy<?xml version="1.0" encoding="utf-8"?> <Gallery xmlns:android=" android:id="@id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:spacing="1dip" />/.8._Universal_Image_Loader_Demo/res/layout/item_gallery_image.xml
[html] view plaincopy<?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android=" android:id="@id/image" android:layout_width="dip" android:layout_height="dip" android:layout_gravity="center" android:adjustViewBounds="true" android:contentDescription="@string/descr_image" android:scaleType="centerCrop" />还有一个就是桌面小部件
以上只是布局文件,没有什么可以说,具体Activity代码实现如下:
先是这个:
/.8._Universal_Image_Loader_Demo/src/com/nostra/example/universalimageloader/HomeActivity.java
主界面Activity代码
[java] view plaincopy/******************************************************************************* * Copyright - Sergey Tarasevich * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package com.nostra.example.universalimageloader; import static com.nostra.example.universalimageloader.Constants.IMAGES; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import android.content.Intent; import android.os.Bundle; import android.view.View; import com.nostra.example.universalimageloader.Constants.Extra; import com.nostra.universalimageloader.utils.L; /** * @author Sergey Tarasevich (nostra[at]gmail[dot]com) */ public class HomeActivity extends BaseActivity { private static final String TEST_FILE_NAME = "Universal Image Loader @#&=-_.,!()~'%.png"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ac_home); // 定义文件对象,目录:/mnt/sdcard, 文件名:TEST_FILE_NAME File testImageOnSdCard = new File("/mnt/sdcard", TEST_FILE_NAME); if (!testImageOnSdCard.exists()) { // 如果文件不存在 // 把文件复制到SD卡 copyTestImageToSdCard(testImageOnSdCard); } } // 点击进入ListView展示界面 public void onImageListClick(View view) { Intent intent = new Intent(this, ImageListActivity.class); intent.putExtra(Extra.IMAGES, IMAGES); startActivity(intent); } // 点击进入GridView展示界面 public void onImageGridClick(View view) { Intent intent = new Intent(this, ImageGridActivity.class); intent.putExtra(Extra.IMAGES, IMAGES); startActivity(intent); } // 点击进入ViewPager展示界面 public void onImagePagerClick(View view) { Intent intent = new Intent(this, ImagePagerActivity.class); intent.putExtra(Extra.IMAGES, IMAGES); startActivity(intent); } // 点击进入画廊展示界面 public void onImageGalleryClick(View view) { Intent intent = new Intent(this, ImageGalleryActivity.class); intent.putExtra(Extra.IMAGES, IMAGES); startActivity(intent); } @Override public void onBackPressed() { imageLoader.stop(); // 停止加载图片 super.onBackPressed(); } /** * 开一个线程把assert目录下的图片复制到SD卡目录下 * @param testImageOnSdCard */ private void copyTestImageToSdCard(final File testImageOnSdCard) { new Thread(new Runnable() { @Override public void run() { try { InputStream is = getAssets().open(TEST_FILE_NAME); FileOutputStream fos = new FileOutputStream(testImageOnSdCard); byte[] buffer = new byte[]; int read; try { while ((read = is.read(buffer)) != -1) { fos.write(buffer, 0, read); // 写入输出流 } } finally { fos.flush(); // 写入SD卡 fos.close(); // 关闭输出流 is.close(); // 关闭输入流 } } catch (IOException e) { L.w("Can't copy test image onto SD card"); } } }).start(); // 启动线程 } } /.8._Universal_Image_Loader_Demo/src/com/nostra/example/universalimageloader/BaseActivity.java[java] view plaincopy/******************************************************************************* * Copyright - Sergey Tarasevich * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package com.nostra.example.universalimageloader; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import com.nostra.universalimageloader.core.ImageLoader; /** * @author Sergey Tarasevich (nostra[at]gmail[dot]com) */ public abstract class BaseActivity extends Activity { protected ImageLoader imageLoader = ImageLoader.getInstance(); @Override public boolean onCreateOptionsMenu(Menu menu) { // 加载菜单 getMenuInflater().inflate(R.menu.main_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.item_clear_memory_cache: imageLoader.clearMemoryCache(); // 清除内存缓存 return true; case R.id.item_clear_disc_cache: imageLoader.clearDiscCache(); // 清除SD卡中的缓存 return true; default: return false; } } }/.8._Universal_Image_Loader_Demo/src/com/nostra/example/universalimageloader/AbsListViewBaseActivity.java
[java] view plaincopy/******************************************************************************* * Copyright - Sergey Tarasevich * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package com.nostra.example.universalimageloader; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.AbsListView; import com.nostra.universalimageloader.core.assist.PauseOnScrollListener; /** * * * @author Sergey Tarasevich (nostra[at]gmail[dot]com) */ public class AbsListViewBaseActivity extends BaseActivity { protected static final String STATE_PAUSE_ON_SCROLL = "STATE_PAUSE_ON_SCROLL"; protected static final String STATE_PAUSE_ON_FLING = "STATE_PAUSE_ON_FLING"; protected AbsListView listView; protected boolean pauseOnScroll = false; protected boolean pauseOnFling = true; @Override public void onRestoreInstanceState(Bundle savedInstanceState) { pauseOnScroll = savedInstanceState.getBoolean(STATE_PAUSE_ON_SCROLL, false); pauseOnFling = savedInstanceState.getBoolean(STATE_PAUSE_ON_FLING, true); } @Override public void onResume() { super.onResume(); applyScrollListener(); } private void applyScrollListener() { listView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling)); } @Override public void onSaveInstanceState(Bundle outState) { outState.putBoolean(STATE_PAUSE_ON_SCROLL, pauseOnScroll); outState.putBoolean(STATE_PAUSE_ON_FLING, pauseOnFling); } @Override public boolean onPrepareOptionsMenu(Menu menu) { MenuItem pauseOnScrollItem = menu.findItem(R.id.item_pause_on_scroll); pauseOnScrollItem.setVisible(true); pauseOnScrollItem.setChecked(pauseOnScroll); MenuItem pauseOnFlingItem = menu.findItem(R.id.item_pause_on_fling); pauseOnFlingItem.setVisible(true); pauseOnFlingItem.setChecked(pauseOnFling); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.item_pause_on_scroll: pauseOnScroll = !pauseOnScroll; item.setChecked(pauseOnScroll); applyScrollListener(); return true; case R.id.item_pause_on_fling: pauseOnFling = !pauseOnFling; item.setChecked(pauseOnFling); applyScrollListener(); return true; default: return super.onOptionsItemSelected(item); } } }/.8._Universal_Image_Loader_Demo/src/com/nostra/example/universalimageloader/Constants.java
常量类代码
[java] view plaincopy/******************************************************************************* * Copyright - Sergey Tarasevich * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package com.nostra.example.universalimageloader; /** * @author Sergey Tarasevich (nostra[at]gmail[dot]com) * 常量类 */ public final class Constants { // 一堆图片链接 public static final String[] IMAGES = new String[] { // Heavy images " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " // Light images " " " " " " " " " " " " " " " " " " " " " " " " " " // Special cases " // very large image " Image Loader @#&=-_.,!()~'%.png", // Image from SD card with encoded symbols " Things @#&=-_.,!()~'%.jpg", // Image from assets " R.drawable.ic_launcher, // Image from drawables " // Link with UTF-8 " // Image from HTTPS " // Redirect link " // EXIF "", // Empty link " // Wrong link }; private Constants() { } // 配置 public static class Config { public static final boolean DEVELOPER_MODE = false; } // 额外类 public static class Extra { public static final String IMAGES = "com.nostra.example.universalimageloader.IMAGES"; public static final String IMAGE_POSITION = "com.nostra.example.universalimageloader.IMAGE_POSITION"; } } /.8._Universal_Image_Loader_Demo/src/com/nostra/example/universalimageloader/ImageListActivity.java[java] view plaincopy/******************************************************************************* * Copyright - Sergey Tarasevich * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package com.nostra.example.universalimageloader; import java.util.Collections; import java.util.LinkedList; import java.util.List; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.nostra.example.universalimageloader.Constants.Extra; import com.nostra.universalimageloader.core.DisplayImageOptions; import com.nostra.universalimageloader.core.assist.ImageLoadingListener; import com.nostra.universalimageloader.core.assist.SimpleImageLoadingListener; import com.nostra.universalimageloader.core.display.FadeInBitmapDisplayer; import com.nostra.universalimageloader.core.display.RoundedBitmapDisplayer; /** * @author Sergey Tarasevich (nostra[at]gmail[dot]com) */ public class ImageListActivity extends AbsListViewBaseActivity { DisplayImageOptions options; // DisplayImageOptions是用于设置图片显示的类 String[] imageUrls; // 图片路径 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ac_image_list); Bundle bundle = getIntent().getExtras(); imageUrls = bundle.getStringArray(Extra.IMAGES); // 使用DisplayImageOptions.Builder()创建DisplayImageOptions options = new DisplayImageOptions.Builder() .showStubImage(R.drawable.ic_stub) // 设置图片下载期间显示的图片 .showImageForEmptyUri(R.drawable.ic_empty) // 设置图片Uri为空或是错误的时候显示的图片 .showImageOnFail(R.drawable.ic_error) // 设置图片加载或解码过程中发生错误显示的图片 .cacheInMemory(true) // 设置下载的图片是否缓存在内存中 .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中 .displayer(new RoundedBitmapDisplayer()) // 设置成圆角图片 .build(); // 创建配置过得DisplayImageOption对象 listView = (ListView) findViewById(android.R.id.list); ((ListView) listView).setAdapter(new ItemAdapter()); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // 点击列表项转入ViewPager显示界面 startImagePagerActivity(position); } }); } @Override public void onBackPressed() { AnimateFirstDisplayListener.displayedImages.clear(); super.onBackPressed(); } private void startImagePagerActivity(int position) { Intent intent = new Intent(this, ImagePagerActivity.class); intent.putExtra(Extra.IMAGES, imageUrls); intent.putExtra(Extra.IMAGE_POSITION, position); startActivity(intent); } /** * * 自定义列表项适配器 * */ class ItemAdapter extends BaseAdapter { private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener(); private class ViewHolder { public TextView text; public ImageView image; } @Override public int getCount() { return imageUrls.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { View view = convertView; final ViewHolder holder; if (convertView == null) { view = getLayoutInflater().inflate(R.layout.item_list_image, parent, false); holder = new ViewHolder(); holder.text = (TextView) view.findViewById(R.id.text); holder.image = (ImageView) view.findViewById(R.id.image); view.setTag(holder); // 给View添加一个外的数据 } else { holder = (ViewHolder) view.getTag(); // 把数据取出来 } holder.text.setText("Item " (position 1)); // TextView设置文本 /** * 显示图片 * 参数1:图片url * 参数2:显示图片的控件 * 参数3:显示图片的设置 * 参数4:监听器 */ imageLoader.displayImage(imageUrls[position], holder.image, options, animateFirstListener); return view; } } /** * 图片加载第一次显示监听器 * @author Administrator * */ private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener { static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>()); @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { if (loadedImage != null) { ImageView imageView = (ImageView) view; // 是否第一次显示 boolean firstDisplay = !displayedImages.contains(imageUri); if (firstDisplay) { // 图片淡入效果 FadeInBitmapDisplayer.animate(imageView, ); displayedImages.add(imageUri); } } } } } /.8._Universal_Image_Loader_Demo/src/com/nostra/example/universalimageloader/ImageGridActivity.java网视图Activity
[java] view plaincopy/******************************************************************************* * Copyright - Sergey Tarasevich * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package com.nostra.example.universalimageloader; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import com.nostra.example.universalimageloader.Constants.Extra; import com.nostra.universalimageloader.core.DisplayImageOptions; /** * 网视图显示Activity * @author Sergey Tarasevich (nostra[at]gmail[dot]com) */ public class ImageGridActivity extends AbsListViewBaseActivity { String[] imageUrls; // 图片Url DisplayImageOptions options; // 显示图片的设置 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ac_image_grid); Bundle bundle = getIntent().getExtras(); imageUrls = bundle.getStringArray(Extra.IMAGES); options = new DisplayImageOptions.Builder() .showStubImage(R.drawable.ic_stub) .showImageForEmptyUri(R.drawable.ic_empty) .showImageOnFail(R.drawable.ic_error) .cacheInMemory(true) .cacheOnDisc(true) .bitmapConfig(Bitmap.Config.RGB_) //设置图片的解码类型 .build(); listView = (GridView) findViewById(R.id.gridview); ((GridView) listView).setAdapter(new ImageAdapter()); // 填充数据 listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { startImagePagerActivity(position); } }); } private void startImagePagerActivity(int position) { Intent intent = new Intent(this, ImagePagerActivity.class); intent.putExtra(Extra.IMAGES, imageUrls); intent.putExtra(Extra.IMAGE_POSITION, position); startActivity(intent); } public class ImageAdapter extends BaseAdapter { @Override public int getCount() { return imageUrls.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ImageView imageView; if (convertView == null) { imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false); } else { imageView = (ImageView) convertView; } // 将图片显示任务增加到执行池,图片将被显示到ImageView当轮到此ImageView imageLoader.displayImage(imageUrls[position], imageView, options); return imageView; } } }/.8._Universal_Image_Loader_Demo/src/com/nostra/example/universalimageloader/ImagePagerActivity.java
ViewPager视图Activity
[java] view plaincopy/******************************************************************************* * Copyright - Sergey Tarasevich * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package com.nostra.example.universalimageloader; import android.graphics.Bitmap; import android.os.Bundle; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.Toast; import com.nostra.example.universalimageloader.Constants.Extra; import com.nostra.universalimageloader.core.DisplayImageOptions; import com.nostra.universalimageloader.core.assist.FailReason; import com.nostra.universalimageloader.core.assist.ImageScaleType; import com.nostra.universalimageloader.core.assist.SimpleImageLoadingListener; import com.nostra.universalimageloader.core.display.FadeInBitmapDisplayer; /** * ViewPager页面显示Activity * @author Sergey Tarasevich (nostra[at]gmail[dot]com) */ public class ImagePagerActivity extends BaseActivity { private static final String STATE_POSITION = "STATE_POSITION"; DisplayImageOptions options; ViewPager pager; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ac_image_pager); Bundle bundle = getIntent().getExtras(); String[] imageUrls = bundle.getStringArray(Extra.IMAGES); // 当前显示View的位置 int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0); // 如果之前有保存用户数据 if (savedInstanceState != null) { pagerPosition = savedInstanceState.getInt(STATE_POSITION); } options = new DisplayImageOptions.Builder() .showImageForEmptyUri(R.drawable.ic_empty) .showImageOnFail(R.drawable.ic_error) .resetViewBeforeLoading(true) .cacheOnDisc(true) .imageScaleType(ImageScaleType.EXACTLY) .bitmapConfig(Bitmap.Config.RGB_) .displayer(new FadeInBitmapDisplayer()) .build(); pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(new ImagePagerAdapter(imageUrls)); pager.setCurrentItem(pagerPosition); // 显示当前位置的View } @Override public void onSaveInstanceState(Bundle outState) { // 保存用户数据 outState.putInt(STATE_POSITION, pager.getCurrentItem()); } private class ImagePagerAdapter extends PagerAdapter { private String[] images; private LayoutInflater inflater; ImagePagerAdapter(String[] images) { this.images = images; inflater = getLayoutInflater(); } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager) container).removeView((View) object); } @Override public void finishUpdate(View container) { } @Override public int getCount() { return images.length; } @Override public Object instantiateItem(ViewGroup view, int position) { View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false); ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image); final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading); imageLoader.displayImage(images[position], imageView, options, new SimpleImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, View view) { spinner.setVisibility(View.VISIBLE); } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { String message = null; switch (failReason.getType()) { // 获取图片失败类型 case IO_ERROR: // 文件I/O错误 message = "Input/Output error"; break; case DECODING_ERROR: // 解码错误 message = "Image can't be decoded"; break; case NETWORK_DENIED: // 网络延迟 message = "Downloads are denied"; break; case OUT_OF_MEMORY: // 内存不足 message = "Out Of Memory error"; break; case UNKNOWN: // 原因不明 message = "Unknown error"; break; } Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show(); spinner.setVisibility(View.GONE); } @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { spinner.setVisibility(View.GONE); // 不显示圆形进度条 } }); ((ViewPager) view).addView(imageLayout, 0); // 将图片增加到ViewPager return imageLayout; } @Override public boolean isViewFromObject(View view, Object object) { return view.equals(object); } @Override public void restoreState(Parcelable state, ClassLoader loader) { } @Override public Parcelable saveState() { return null; } @Override public void startUpdate(View container) { } } } /.8._Universal_Image_Loader_Demo/src/com/nostra/example/universalimageloader/ImageGalleryActivity.java画廊视图的Activity
[java] view plaincopy/******************************************************************************* * Copyright - Sergey Tarasevich * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package com.nostra.example.universalimageloader; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import com.nostra.example.universalimageloader.Constants.Extra; import com.nostra.universalimageloader.core.DisplayImageOptions; /** * 画廊视图Activity * @author Sergey Tarasevich (nostra[at]gmail[dot]com) */ @SuppressWarnings("deprecation") public class ImageGalleryActivity extends BaseActivity { String[] imageUrls; DisplayImageOptions options; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ac_image_gallery); Bundle bundle = getIntent().getExtras(); imageUrls = bundle.getStringArray(Extra.IMAGES); options = new DisplayImageOptions.Builder() .showStubImage(R.drawable.ic_stub) .showImageForEmptyUri(R.drawable.ic_empty) .showImageOnFail(R.drawable.ic_error) .cacheInMemory(true) .cacheOnDisc(true) .bitmapConfig(Bitmap.Config.RGB_) .build(); // 自API 之后就被抛弃了 Gallery gallery = (Gallery) findViewById(R.id.gallery); gallery.setAdapter(new ImageGalleryAdapter()); gallery.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { startImagePagerActivity(position); } }); } private void startImagePagerActivity(int position) { Intent intent = new Intent(this, ImagePagerActivity.class); intent.putExtra(Extra.IMAGES, imageUrls); intent.putExtra(Extra.IMAGE_POSITION, position); startActivity(intent); } private class ImageGalleryAdapter extends BaseAdapter { @Override public int getCount() { return imageUrls.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = (ImageView) convertView; if (imageView == null) { imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_gallery_image, parent, false); } imageLoader.displayImage(imageUrls[position], imageView, options); return imageView; } } } 关于桌面控件的代码我就不贴了,朋友们可以自己下载我这个Demo还有就是Manifest文件代码
[html] view plaincopy<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=" package="com.nostra.example.universalimageloader" android:versionCode="" android:versionName="1.8.7" > <uses-sdk android:minSdkVersion="5" android:targetSdkVersion="" /> <!-- 网络访问权限 --> <uses-permission android:name="android.permission.INTERNET" /> <!-- 文件写入SD卡权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:name=".UILApplication" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".HomeActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ImageListActivity" android:label="@string/ac_name_image_list" /> <activity android:name=".ImageGridActivity" android:label="@string/ac_name_image_grid" /> <activity android:name=".ImagePagerActivity" android:label="@string/ac_name_image_pager" /> <activity android:name=".ImageGalleryActivity" android:label="@string/ac_name_image_gallery" /> <!-- Widget --> <receiver android:name=".widget.UILWidgetProvider" > <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider" /> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> </receiver> </application> </manifest>以上的代码已经很详尽了,读者可以把它整合到你的项目当中,就可以实现完美异步加载图片。在这里要感谢那些为我们开源的大神们,他们的贡献让我们这些开发者更快更有效的完成工作,希望我们的开源会越做越好,我相信我们的开发者有这个能力。
总体把握Android中的触摸事件处理 先看几个函数:提到Android的触摸事件处理,不少人都会立刻想到某些令人头疼的函数,这里列举一下,刺激一下你的小神经:@OverridepublicbooleanonTouchEvent
使用Xutils 框架 下载功能进度条无法更新 在下载列表页面DownloadListActivity可以看到下载进度的变化,但是我使用对话方式的显示下载进度的时候,进度没有发生变化。经过一步一步的调试最后找
Android Network -------------------判断网络状态(网络的连接,改变,和判断2G/3G/4G) 现在app大多都需要从网络上获得数据。所以访问网络是在所难免。但是再访问网络之前,我们应该先做一下网络的状态判断。其实在访问网络之前我们
标签: android ui开发
本文链接地址:https://www.jiuchutong.com/biancheng/382277.html 转载请保留说明!友情链接: 武汉网站建设