編輯:關於Android編程
package cc.y; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.os.Bundle; import android.util.LruCache; import android.widget.ImageView; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.ImageLoader; import com.android.volley.toolbox.ImageLoader.ImageCache; import com.android.volley.toolbox.ImageLoader.ImageListener; import com.android.volley.toolbox.ImageRequest; import com.android.volley.toolbox.NetworkImageView; import com.android.volley.toolbox.Volley; /** * Demo描述: * 去年一個項目中簡單的使用了Volley,參見: * http://blog.csdn.net/lfdfhl/article/details/12223345 * 最近guolin寫了幾篇文章詳細介紹Volley,所以在此比較系統地學習一下. * 在本示例中,主要包括: * 1 利用ImageRequest為ImageView加載網絡圖片 * 2 利用ImageLoader為ImageView加載網絡圖片 * 3 利用NetworkImageView為ImageView加載網絡圖片 * * 注意事項: * 1 引入jar包volley.jar * 2 網絡權限* 3 Volley源碼分析,請參見: * http://blog.csdn.net/guolin_blog/article/details/17656437 * 4 自定義的XMLRequest和GsonRequest,請參見: * http://blog.csdn.net/guolin_blog/article/details/17612763 * * 學習資料: * http://blog.csdn.net/guolin_blog/article/details/17482165 * Thank you very much * */ public class MainActivity extends Activity { private Context mContext; private RequestQueue mRequestQueue; private String mUrl="http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg"; private ImageView mImageView; private ImageRequest mImageRequest; private ImageLoader mImageLoader; private ImageListener mImageListener; private NetworkImageView mNetworkImageView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); //volley_ImageRequest(); //volley_ImageLoader(); volley_NetworkImageView(); } private void init(){ mContext=this; mImageView=(ImageView) findViewById(R.id.imageView); mNetworkImageView=(NetworkImageView) findViewById(R.id.networkImageView); } //利用ImageRequest為ImageView加載網絡圖片 private void volley_ImageRequest() { mRequestQueue = Volley.newRequestQueue(mContext); mImageRequest = new ImageRequest( mUrl, new Response.Listener () { @Override public void onResponse(Bitmap response) { mImageView.setImageBitmap(response); }}, 0, 0, Config.ARGB_8888, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { System.out.println("獲取圖片失敗,所以加載一個默認圖片"); mImageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_launcher)); } }); mRequestQueue.add(mImageRequest); } //利用ImageLoader為ImageView加載網絡圖片 private void volley_ImageLoader(){ mRequestQueue = Volley.newRequestQueue(mContext); mImageLoader=new ImageLoader(mRequestQueue, new ImageCacheImpl()); mImageListener=ImageLoader.getImageListener(mImageView, R.drawable.ic_launcher, R.drawable.ic_launcher); mImageLoader.get(mUrl, mImageListener); //mImageLoader.get(mUrl, mImageListener,250,250); } //利用LruCache實現圖片緩存機制 //請參見: //http://blog.csdn.net/lfdfhl/article/details/18350601 private class ImageCacheImpl implements ImageCache { private LruCache mLruCache; public ImageCacheImpl() { int maxSize = 15 * 1024 * 1024; mLruCache = new LruCache (maxSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getRowBytes() * bitmap.getHeight(); } }; } @Override public Bitmap getBitmap(String url) { return mLruCache.get(url); } @Override public void putBitmap(String url, Bitmap bitmap) { mLruCache.put(url, bitmap); } } //利用NetworkImageView為ImageView加載網絡圖片 private void volley_NetworkImageView(){ mRequestQueue = Volley.newRequestQueue(mContext); mImageLoader=new ImageLoader(mRequestQueue, new ImageCacheImpl()); mNetworkImageView.setDefaultImageResId(R.drawable.ic_launcher); mNetworkImageView.setErrorImageResId(R.drawable.ic_launcher); mNetworkImageView.setImageUrl(mUrl, mImageLoader); } }
main.xml如下:
一、概述:在日常的app使用中,我們會在android 的app中看見 熱門標簽等自動換行的流式布局,今天,我們就來看看如何自定義一個類似熱門標簽那樣的流式布局吧(源碼下
運行時權限(Runtime Permission)是Android 6.0( 代號為 Marshmallow,API版本為 23)及以上版本新增的功能,相比於以往版本,這
我們都知道,手機時代的來臨的主要標志是啥?能夠方便的接入互聯網!互聯網展現給我們的方式一般都是網頁,網頁中又必不可少的擁有javascript,所以說,android提供
1、申請注冊你的appid2、下載sdkQQ: http://wiki.open.qq.com/wiki/mobile/SDK%E4%B8%8B%E8%BD%BD微信:h