Android UI-开源框架ImageLoader的完美例子
Posted 专注it
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android UI-开源框架ImageLoader的完美例子相关的知识,希望对你有一定的参考价值。
Android开源框架ImageLoader的完美例子
2013年8月19日开源框架之Universal_Image_Loader学习
很多人都在讨论如何让图片能在异步加载更加流畅,可以显示大量图片,在拖动ListView的时候不会出现卡的现象。关于ImageLoader这个开源框架的使用有很多网友都介绍过,不过还不够清楚,这里有一个关于这个开源项目的完美例子,ListView的图片加载、GridView的图片加载、ViewPager的图片加载、Gallery画廊的图片加载、Widget的使用。很完善的一个例子,在这里我把所有界面效果做出博客分享出来,需要源码的朋友到我的资源页下载
下载地址:http://download.csdn.net/detail/wwj_748/5975847
要使用ImageLoader就要到这里下载jar包:
https://github.com/nostra13/Android-Universal-Image-Loader
然后导入项目中去就行了
项目文档结构图:
从界面说起,界面本身是没什么好说的,就是如何在xml当中进行定义罢了
有以下这么多个布局文件
一个一个来看呗
首先是这样的效果
这个在android4.2.2比较好看,在Android2.3.3就显得比较挫。
/2013.8.19_Universal_Image_Loader_Demo/res/layout/ac_home.xml
- <?xml version="1.0" encoding="utf-8"?>
- <ScrollView xmlns:android="http://schemas.android.com/apk/res/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="10dip"
- android:paddingTop="20dip"
- android:text="@string/label_activity_examples"
- android:textSize="24sp" />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dip"
- android:onClick="onImageListClick"
- android:text="@string/button_image_list" />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dip"
- android:onClick="onImageGridClick"
- android:text="@string/button_image_grid" />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dip"
- android:onClick="onImagePagerClick"
- android:text="@string/button_image_pager" />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dip"
- android:onClick="onImageGalleryClick"
- android:text="@string/button_image_gallery" />
- </LinearLayout>
- </ScrollView>
列表异步加载图片效果
/2013.8.19_Universal_Image_Loader_Demo/res/layout/ac_image_list.xml
- <?xml version="1.0" encoding="utf-8"?>
- <ListView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@android:id/list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" />
/2013.8.19_Universal_Image_Loader_Demo/res/layout/item_list_image.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <ImageView
- android:id="@+id/image"
- android:layout_width="72dip"
- android:layout_height="72dip"
- 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="20dip"
- android:textSize="22sp" />
- </LinearLayout>
GridView异步加载图片显示
/2013.8.19_Universal_Image_Loader_Demo/res/layout/ac_image_grid.xml
- <?xml version="1.0" encoding="utf-8"?>
- <GridView xmlns:android="http://schemas.android.com/apk/res/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" />
/2013.8.19_Universal_Image_Loader_Demo/res/layout/item_grid_image.xml
- <?xml version="1.0" encoding="utf-8"?>
- <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/image"
- android:layout_width="fill_parent"
- android:layout_height="120dip"
- android:adjustViewBounds="true"
- android:contentDescription="@string/descr_image"
- android:scaleType="centerCrop" />
ViewPager异步加载图片显示
/2013.8.19_Universal_Image_Loader_Demo/res/layout/ac_image_pager.xml
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/pager"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" />
/2013.8.19_Universal_Image_Loader_Demo/res/layout/item_pager_image.xml
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/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画廊异步加载图片显示
/2013.8.19_Universal_Image_Loader_Demo/res/layout/ac_image_gallery.xml
- <?xml version="1.0" encoding="utf-8"?>
- <Gallery xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/gallery"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:spacing="1dip" />
/2013.8.19_Universal_Image_Loader_Demo/res/layout/item_gallery_image.xml
- <?xml version="1.0" encoding="utf-8"?>
- <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/image"
- android:layout_width="120dip"
- android:layout_height="120dip"
- android:layout_gravity="center"
- android:adjustViewBounds="true"
- android:contentDescription="@string/descr_image"
- android:scaleType="centerCrop" />
还有一个就是桌面小部件
以上只是布局文件,没有什么可以说,具体Activity代码实现如下:
先是这个:
/2013.8.19_Universal_Image_Loader_Demo/src/com/nostra13/example/universalimageloader/HomeActivity.java
主界面Activity代码
- /*******************************************************************************
- * Copyright 2011-2013 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
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * 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.nostra13.example.universalimageloader;
- import static com.nostra13.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.nostra13.example.universalimageloader.Constants.Extra;
- import com.nostra13.universalimageloader.utils.L;
- /**
- * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
- */
- public class HomeActivity extends BaseActivity {
- private static final String TEST_FILE_NAME = "Universal Image Loader @#&=+-_.,!()~‘%20.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[8192];
- 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(); // 启动线程
- }
- }
/2013.8.19_Universal_Image_Loader_Demo/src/com/nostra13/example/universalimageloader/BaseActivity.java
- /*******************************************************************************
- * Copyright 2011-2013 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
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * 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.nostra13.example.universalimageloader;
以上是关于Android UI-开源框架ImageLoader的完美例子的主要内容,如果未能解决你的问题,请参考以下文章