用xutils或imageloader加载网络图片实现轮播效果

图片轮播效果几乎是每款app的必备,作为一名app开发人员必须熟练掌握这种效果的实现。本地加载图片的实现方式,比较简单,这里就不在叙述,主要讲从网络加载图片实现轮播效果。先上效果图。

 

用xutils或imageloader加载网络图片实现轮播效果_第1张图片

之前看到过许多应用的轮播效果实现,非常之复杂,首先异步加载网络图片,并对加载的图片进行本地化存储,在本地化存储的同时并在内存中进行存储,图片过大的话,还要对图片进行压缩处理,防止OOM。。。而xutils和imageloader的出现,这些问题就变的简单,如imageloader实现这种效果,如果进行本地存储只需配置参数cacheOnDisc(true),内存缓存只需配置参数cacheInMemory(true)等等。。

 

这里我给出两种实现方式,一种是用Xutils,一种用imageloader。而指示点我们用开源框架ViewPageIndicatorLibrary(这里以图片网络图片加载为主,此开源框架的应用就不在介绍)。

代码比较简单:直接贴出:

1.用imageloader实现

package com.example.viewpager2;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;


import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.viewpagerindicator.CirclePageIndicator;


public class MainActivity extends Activity {
String url1="http://p0.so.qhimg.com/bdr/_240_/t01ad3641a09574b2f5.jpg";
    String url2="http://p0.so.qhimg.com/bdr/_240_/t01a3f05c605b07d5d5.jpg";
    String url3="http://p2.so.qhimg.com/bdr/_240_/t0126b04b6e0a3fccb7.jpg";
    String url4="http://p1.so.qhimg.com/bdr/_240_/t01baab02b231be535b.jpg";
    String url5="http://p0.so.qhimg.com/bdr/_240_/t01c5ad4f3a5cf97185.jpg";
    String url6="http://p0.so.qhimg.com/bdr/_240_/t0141df88740d0e417f.jpg";

String url[]={url1,url2,url3,url4,url5,url6};
    private ViewPager viewpager;
    CirclePageIndicator mIndicator;
Mypageradapter mypageradapter;
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
int currentItem = viewpager.getCurrentItem();
if(currentItem currentItem++;
}else{
currentItem=0;
}
viewpager.setCurrentItem(currentItem);
handler.sendEmptyMessageDelayed(0, 3000);

};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
ImageLoaderConfiguration configuration=ImageLoaderConfiguration.createDefault(MainActivity.this);
ImageLoader.getInstance().init(configuration);
viewpager=(ViewPager) findViewById(R.id.pager);
mIndicator=(CirclePageIndicator) findViewById(R.id.indicator);
mypageradapter=new Mypageradapter();
viewpager.setAdapter(mypageradapter);
mIndicator.setViewPager(viewpager);
handler.sendEmptyMessageDelayed(0, 3000);

}



class Mypageradapter extends PagerAdapter{
       
public Mypageradapter() {

}

@Override
public int getCount() {

return url.length;
}


@Override
public boolean isViewFromObject(View view, Object obj) {

return view==obj;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView=new ImageView(MainActivity.this);
container.addView(imageView);
DisplayImageOptions options=new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).build();
ImageLoader.getInstance().displayImage(url[position], imageView,options);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {

container.removeView((View) object);
}



}



}

2.用xutils实现:

package com.example.viewpagere;


import com.lidroid.xutils.BitmapUtils;
import com.viewpagerindicator.CirclePageIndicator;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;


public class MainActivity extends Activity {
    String url1="http://p0.so.qhimg.com/bdr/_240_/t01ad3641a09574b2f5.jpg";
    String url2="http://p0.so.qhimg.com/bdr/_240_/t01a3f05c605b07d5d5.jpg";
    String url3="http://p2.so.qhimg.com/bdr/_240_/t0126b04b6e0a3fccb7.jpg";
    String url4="http://p1.so.qhimg.com/bdr/_240_/t01baab02b231be535b.jpg";
    String url5="http://p0.so.qhimg.com/bdr/_240_/t01c5ad4f3a5cf97185.jpg";
    String url6="http://p0.so.qhimg.com/bdr/_240_/t0141df88740d0e417f.jpg";
String url[]={url1,url2,url3,url4,url5,url6};
private ViewPager view_pager;
CirclePageIndicator mIndicator;
Mypageradapter mypageradapter;
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
int currentItem = view_pager.getCurrentItem();
if(currentItem currentItem++;
}else{
currentItem=0;
}
view_pager.setCurrentItem(currentItem);
handler.sendEmptyMessageDelayed(0, 3000);

};
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
view_pager=(ViewPager) findViewById(R.id.pager);
mypageradapter=new Mypageradapter();
view_pager.setAdapter(mypageradapter);
mIndicator=(CirclePageIndicator) findViewById(R.id.indicator);
mIndicator.setViewPager(view_pager);
handler.sendEmptyMessageDelayed(0, 3000);
}


class Mypageradapter extends PagerAdapter{
        BitmapUtils bitmapUtils;
public Mypageradapter() {
bitmapUtils=new BitmapUtils(MainActivity.this);
}
@Override
public int getCount() {

return url.length;
}


@Override
public boolean isViewFromObject(View view, Object obj) {

return view==obj;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {


        ImageView imageView=new ImageView(MainActivity.this);
        container.addView(imageView);
        bitmapUtils.display(imageView, url[position]);
return imageView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {

container.removeView((View) object);
}

}

}

代码下载地址:https://github.com/kelvinnint/Asyncviewpager

 

你可能感兴趣的:(技术文档)