轮播图的实现有很多种方式,早先我在网上看了下别人写的轮播图,感觉都比较的墨守成规,有的还有可能加载不了网络图片。所以我在这里自己重新写了下轮播图 ,方便日后的项目使用.
在下面的代码中,我也用volley封装了网络请求框架,异步加载网络图片,也可以给大家参考,非常实用。
效果图:这只是其中的一种效果
底层封装的我在下面会一一展示,先看下在MainActivity中怎样调取这个轮播控件
package com.wujie.advertisment.activity; import android.view.View; import com.wujie.advertisment.R; import com.wujie.advertisment.flashview.EffectConstants; import com.wujie.advertisment.flashview.FlashView; import java.util.ArrayList; public class MainActivity extends BaseActivity { private FlashView flashView; private ArrayListimageUrls=new ArrayList (); @Override protected void initView() { setContentView(R.layout.activity_main); flashView=(FlashView)findViewById(R.id.flash_view); } @Override protected void initListener() { } @Override protected void initData() { /** * 获取接口返回的轮播图地址,获取的部分我已经注释掉了,可以使用。我在这里就直接弄了4张图片 */ imageUrls.add("1303680113,133301350&fm=21&gp=0.jpg"); imageUrls.add("1350614941,725003865&fm=21&gp=0.jpg"); imageUrls.add("3293842903,1040789920&fm=21&gp=0.jpg"); imageUrls.add(",1368131788&fm=21&gp=0.jpg"); flashView.setImageUris(imageUrls); /** (wujie)增加代码 usage:需要什么类型falshView直接去找这个类就行在flashView框架中,同时修改View中的flashView注释的那块*/ flashView.setEffect(EffectConstants.CUBE_EFFECT); // VolleyRequest.newInstance(IpAddressConstants.getAdnertisment("","")).newGsonRequest2(Request.Method.POST,IpAddressConstants.ADVERTISMENT_URL, Advertisment.class, // new Response.Listener () { // @Override // public void onResponse(Advertisment dvertisment) { // /**在此处将服务器返回的轮播图地址一个一个放入imageUrls中*/ // flashView.setImageUris(imageUrls); // /** (wujie)增加代码 usage:需要什么类型falshView直接去找这个类就行在flashView框架中,同时修改View中的flashView注释的那块*/ // flashView.setEffect(EffectConstants.CUBE_EFFECT); // } // }, new Response.ErrorListener() { // @Override // public void onErrorResponse(VolleyError volleyError) { // // } // }); } @Override public void onClick(View view) { } }
MainActivity继承的BaseActivity:
package com.wujie.advertisment.activity; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.Window; import android.widget.Toast; /** ** Company: * Author : wujie * Time : 2016/11 * Usage : 所有自定义activity的父类 * desc : * other : **/ public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener{ @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); supportRequestWindowFeature(Window.FEATURE_NO_TITLE); initView(); initData(); initListener(); } /** * 初始化控件 */ protected abstract void initView(); /** * 设置监听 */ protected abstract void initListener(); /** * 初始化数据 */ protected abstract void initData(); /** * 吐司 */ public void showShortToast(String toast) { Toast.makeText(this, toast, Toast.LENGTH_SHORT).show(); } }
那么下面我就来封装Volley请求网络数据框架,封装好了之后,调用在MainActivity中有体现。
首先,VolleyRequest类如下:
package com.wujie.advertisment.volley; import android.content.Context; import android.util.Log; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.toolbox.Volley; import java.util.HashMap; import java.util.Map; /** * 请求类 */ public class VolleyRequest { private static RequestQueue mRequestQueue; private MapmMap=new HashMap<>(); private VolleyRequest() { } private VolleyRequest(Map mMap) { this.mMap=mMap; } /** * @param context ApplicationContext */ public static void buildRequestQueue(Context context) { mRequestQueue = Volley.newRequestQueue(context); //... do something } /** (WangShuJie)增加代码 usage:非带参数*/ public static VolleyRequest newInstance() { if (mRequestQueue == null) { throw new NullPointerException("Call buildRequestQueue method first."); } //... return new VolleyRequest(); } /** (WangShuJie)增加代码 usage:带参数*/ public static VolleyRequest newInstance(Map mMap) { if (mRequestQueue == null) { throw new NullPointerException("Call buildRequestQueue method first."); } //... Log.i("mtag","newInstance==========="+mMap.get("goodsname")); return new VolleyRequest(mMap); } /** * @param url * @param clazz * @param listener * @param errorListener *带参 * @return */ public GsonRequest newGsonRequest2(int method, String url, Class clazz, Response.Listener listener, Response.ErrorListener errorListener) { GsonRequest request = new GsonRequest(method,url, clazz, listener, errorListener){ @Override protected Map getParams() { Log.i("mtag","getParams==========="+mMap.get("goodsname")); return mMap; } }; mRequestQueue.add(request); return request; } /** (wujie)增加代码 usage:不带参的*/ public GsonRequest newGsonRequest(String url, Class clazz, Response.Listener listener, Response.ErrorListener errorListener) { GsonRequest request = new GsonRequest(url, clazz, listener, errorListener); mRequestQueue.add(request); return request; } }
GsonRequest类:
package com.wujie.advertisment.volley; import android.util.Log; import com.android.volley.NetworkResponse; import com.android.volley.ParseError; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.Response.ErrorListener; import com.android.volley.Response.Listener; import com.android.volley.toolbox.HttpHeaderParser; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.io.UnsupportedEncodingException; public class GsonRequestextends Request { private final Listener mListener; private static Gson mGson = new Gson(); private Class mClass; private TypeToken mTypeToken; public GsonRequest(int method, String url, Class clazz, Listener listener, ErrorListener errorListener) { super(method, url, errorListener); mClass = clazz; mListener = listener; } public GsonRequest(int method, String url, TypeToken typeToken, Listener listener, ErrorListener errorListener) { super(method, url, errorListener); mTypeToken = typeToken; mListener = listener; } public GsonRequest(String url, Class clazz, Listener listener, ErrorListener errorListener) { this(Method.GET, url, clazz, listener, errorListener); } public GsonRequest(String url, TypeToken typeToken, Listener listener, ErrorListener errorListener) { super(Method.GET, url, errorListener); mTypeToken = typeToken; mListener = listener; } @Override protected Response parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data,"UTF-8"); Log.i("mtag","响应====="+response.data); if (mTypeToken == null) return Response.success(mGson.fromJson(jsonString, mClass), HttpHeaderParser.parseCacheHeaders(response)); else return (Response ) Response.success(mGson.fromJson(jsonString, mTypeToken.getType()), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } } @Override protected void deliverResponse(T response) { mListener.onResponse(response); } }
管理接口的类:
package com.wujie.advertisment.volley; import java.util.HashMap; import java.util.Map; /** * author:wujie 2016/11 * usage: */ public class IpAddressConstants { public static String MYIP="http://xxxxx:8080/"; /** ======轮播图====== */ public static final String ADVERTISMENT_URL=MYIP+"AppInterface/advert/indexAdvert"; /** (wujie)增加代码 usage:获取轮播图需要的Map数据*/ public static MapgetAdnertisment(String city,String token){ Map mMap=new HashMap<>(); mMap.put("city_name",city);//这个key是你要上传参数的的key,服务器通过这个key来获取客户端穿的参数。 mMap.put("token",token); return mMap; } }
封装好后,我们要将Volley请求加入到队列中(初始化):
package com.wujie.advertisment.application; import android.app.Application; import android.content.Context; import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import com.wujie.advertisment.volley.VolleyRequest; /** ** Company: xxx * Author : wujie * Time : 2016/11 * Usage : * desc : * other : **/ public class MyApplication extends Application { public static Context mContext; @Override public void onCreate() { super.onCreate(); mContext = getApplicationContext(); VolleyRequest.buildRequestQueue(this); initImageLoader(); } private void initImageLoader() { /** * ImageLoader的全局的配置信息 */ ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this) .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) .diskCacheFileCount(100) .memoryCacheSize(2 * 1024 * 1024) //设置内存的缓存 .diskCacheSize(50 * 1024 * 1024) //设置磁盘缓存的大小 .threadPoolSize(4) //设置线程池的大小是多少 .build(); //设置配置信息this // ImageLoader.getInstance().init(new ImageLoaderConfiguration.Builder(this).build()); ImageLoader.getInstance().init(configuration); // ImageLoaderConfiguration aDefault = ImageLoaderConfiguration.createDefault(this); } }
到此为止,网络请求框架就封装好了,下面就是轮播图的实现,因为代码文件比较多,我就不一一列举出来了,如果大家有需要可以找我要整个代码。
注意点:
1. 轮播图图片描述字体资源文件的导入
2. 网络请求框架的初始化
下载demo