标签:问题 bsp for content div cti ref idf cond
SimpAndroidFarme是近期脑子突然发热想做的android快速开发的框架,目标是模块化
常用的控件,方便新手学习和使用。也欢迎老鸟来一起充实项目:项目地址
引导页是我们开发app很常用的功能,但是一般都是第一此运行才会执行,每写个项目
都要为这种一次性的东西写那么多重复代码太讨厌啦~
现在再也不用担心这个问题了!
一般引导有两种图片源 , 本地图片/ 网络图片
目标使用方法:
使用本地提供的图片 id
参数一: viewpager
参数二: 图片url集合(使用本地则传null)
参数三: 图片id 集合(使用url则传null)
参数四: 引导完成后 跳转到的页面
new LeaderPager(Second.this).LeaderPagerAdapter(viewPager , null , imgid , MainActivity.class);
是不是看上去使用很方便啊~!!
上代码:
LeaderPager:
package com.wbnq.simpleandroidframe.plugs; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import java.util.ArrayList; import java.util.List; /** * Created by guwei on 16-11-4. */ public class LeaderPager { Context mContext; List<ImageView> mImageViews = new ArrayList<>(); private static final String TAG = "LeaderPager"; public LeaderPager(Context context) { this.mContext = context; } /** * 参数二 三 可选填入 * <p> * 参数二 不为空时 使用bitmap图片 否则使用 mipmap内图片 **/ public void LeaderPagerAdapter(ViewPager viewPager, final List<String> imgs_url, final List<Integer> imgs_id , final Class nextpage) { viewPager.setAdapter(new PagerAdapter() { @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(mContext); if (imgs_url != null) { imageView.setTag(imgs_url.get(position)); new ImageLoader().showImageByThread(imageView , imgs_url.get(position)); //imageView.setImageBitmap(); } else if (imgs_id != null) { imageView.setImageResource(imgs_id.get(position)); } else { Log.e(TAG, "banner方法: 未传入图片参数 , 请检查参数是否填写正确!"); } imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); container.addView(imageView); mImageViews.add(imageView); return imageView; } //移除imageview @Override public void destroyItem(ViewGroup container, int position, Object object) { Log.e(TAG, "destroyItem: position = " + position ); container.removeView(mImageViews.get(position)); if(position == 0){ Intent intent = new Intent(mContext , nextpage); mContext.startActivity(intent); } } @Override public int getCount() { int count; if(imgs_url != null){ count = imgs_url.size(); }else{ count = imgs_id.size(); } return count; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } }); } }
就是一般viewpager的使用方法不多说
如果想要使用网络图片功能先需要配合imageLoader一起使用哦!
ImageLoader:
package com.wbnq.simpleandroidframe.plugs; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Handler; import android.os.Message; import android.util.Log; import android.util.LruCache; import android.widget.ImageView; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; /** * Created by guwei on 16-4-13. */ public class ImageLoader { private ImageView mImageView; private String mUrl; //创建cache private LruCache<String, Bitmap> mCaches; private static final String TAG = "ImageLoader"; public ImageLoader() { //获得最大可用内存 int maxMemory = (int) Runtime.getRuntime().maxMemory(); int cacheSize = maxMemory / 3; mCaches = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap value) { //在每次存入缓存的时候调用,返回的是bitmap的大小 return value.getByteCount(); } }; } //添加图片到缓存中 (cache类似map) public void addBitmapToCache(String url, Bitmap bitmap) { //如果缓存没有这张图片就将图片存到缓存中 if (getBitmapFormCache(url) == null) { mCaches.put(url, bitmap); } } //从缓存中取出图片 public Bitmap getBitmapFormCache(String url) { return mCaches.get(url); } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (mImageView.getTag().equals(mUrl)) { mImageView.setImageBitmap((Bitmap) msg.obj); } } }; //在线程中通过url 加载图片 public void showImageByThread(final ImageView image, final String url) { mImageView = image; mUrl = url; Bitmap bitmap = getBitmapFormCache(url); if (bitmap == null) { new Thread() { @Override public void run() { super.run(); Bitmap bitmap = getBitmapFormURL(url); Message msg = Message.obtain(); msg.obj = bitmap; handler.sendMessage(msg); //Log.i("xyz", "1"); } }.start(); } else { image.setImageBitmap(bitmap); } } public Bitmap getBitmapFormURL(String urlString) { Bitmap bitmap = null; InputStream is = null; try { URL url = new URL(urlString); try { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); is = new BufferedInputStream(connection.getInputStream()); bitmap = BitmapFactory.decodeStream(is); connection.disconnect(); addBitmapToCache(urlString, bitmap); Log.e(TAG, "getBitmapFormURL: 网络方法内部 获取图片 并将图片放入缓存 "+bitmap); } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return bitmap; } }
注释的也比较到位,还有缓存功能哦!图片加载方法来自慕课大神--徐大神
就这样啦~~
都来看看女帝和露琪亚吧~还有一张是雏田哦!
标签:问题 bsp for content div cti ref idf cond
原文地址:http://www.cnblogs.com/wobeinianqing/p/6031197.html