MasterofProject

Android study notes: the use of Andorid network request framework (Volley) - Web image loading

Label Android DevelopmentVolley frameworkimage processing
315 people read comment(2) Collection report
Classification:

On a note "Android study notes: the use of Andorid network request framework Volley (on)"A small supplement: we request data with Volley, do not forget to add network access in AndroidManifest.xml. Learning goal is to achieve the goal of the network to achieve the image of the load, Vollery to achieve the image of the load there are three ways, we can choose according to the actual situation of the loading mode, loaded directly on the code:

Class MyApplication extends Application public {
Static RequestQueue requestQueues private;

@Override
Void onCreate public () {
Auto-generated method stub / / TODO
Super.onCreate ();
RequestQueues = Volley.newRequestQueue (getApplicationContext ());
}

Static RequestQueue getRequestQueues public () {
RequestQueues return;
}
}

Remember to register the above AndroidManifest.xml to the Myapplication list file, the following is the main interface layout file:

Xmlns:android= http://schemas.android.com/apk/res/android "<LinearLayout"
Http://schemas.android.com/tools "xmlns:tools="
Match_parent "android:layout_width="
Match_parent "android:layout_height="
Vertical "android:orientation=" >

<ImageView
@+id/test_iv "android:id="
Match_parent "android:layout_width="
200dp "android:layout_height="
@null "android:contentDescription="
10dp "android:layout_marginTop="
Android:scaleType= "fitXY" / >

<com.android.volley.toolbox.NetworkImageView
@+id/net_work_iv "android:id="
Match_parent "android:layout_width="
20dp "android:layout_marginTop="
200dp "android:layout_height="
@null "android:contentDescription="
FitXY "android:scaleType=" >
</com.android.volley.toolbox.NetworkImageView>

</LinearLayout>

Next up is the code:

Class MainActivity extends Activity public {
ImageView mImageView private;
NetworkImageView mNetworkImageView private;
Static final String IMAGE_URL private = "http://pic.qiantucdn.com/58pic/16/85/34/72K58PICjMU_1024.jpg";


@Override
Void onCreate protected (savedInstanceState Bundle) {
Super.onCreate (savedInstanceState);
SetContentView (R.layout.activity_main);
FindViewsById ();
(); / / getImgRequest / / the first picture of the loading way
GetImgByImageLoader (second); / / picture of the loading way
GetNetworkImageView (third); / / picture of the loading way
}


Void findViewsById private () {
This.mImageView = (ImageView) findViewById (R.id.test_iv);
This.mNetworkImageView = (NetworkImageView) findViewById (R.id.net_work_iv);
}


/ * *
* the first way to load the network image: through the ImageRequest to get the network picture
* ImageRequest (URL String, listener Listener<Bitmap>, maxWidth int, maxHeight int, decodeConfig Config, ErrorListener, errorListener)
* the corresponding parameters are:
* URL pictures URL
* listener download successful listener
* maxWidth: the width of the picture
* maxHeight: the height of the picture
* decodeConfig is a picture of the color properties
* errorListener download failed when listening
* 2015-8-26 1:24:04 PM @date
* /
Void getImgRequest private () {
MRequest ImageRequest = ImageRequest IMAGE_URL (new, Listener<Bitmap> new) {


@Override
Void onResponse public (arg0 Bitmap) {
MImageView.setImageBitmap (arg0); / / show success when loading the images
}
}, 0, 300, Config.RGB_565, ErrorListener new () {


@Override
Void onErrorResponse public (arg0 VolleyError) {
MImageView.setImageResource (R.drawable.ic_launcher); / / if the request fails to set a default.
}
});
MRequest.setTag ("imgTag");
MyApplication.getRequestQueues ().Add (mRequest);
}


/ * *
* second kinds of test, through the ImageLoader and ImageListener in Volley to realize the network picture
* ImageLoader.getImageListener (view ImageView, defaultImageResId int, errorImageResId int)
* parameter introduction:
* view to load images ImageView
* defaultImageResId is the default picture that is loading the image display
* errorImageResId is the default picture that is displayed when the failed load is loaded.
* 2015-8-26 1:56:14 PM @date
* /
Void getImgByImageLoader private () {
Loader ImageLoader = ImageLoader new (MyApplication.getRequestQueues) (), ImageCache new () {


@Override
Void putBitmap public (paramString String, paramBitmap Bitmap) {


}


@Override
Bitmap getBitmap public (paramString String) {
Null return;
}
});
Listener ImageListener = ImageLoader.getImageListener (mImageView, R.drawable.ic_launcher, R.drawable.ic_launcher);
Loader.get (IMAGE_URL, listener);
}


/ * *
* third kinds of loading mode, directly with the picture to load the NetworkImageView to load the control Volley
* use the method and ImageView almost the same
* 2015-8-26 2:02:33 PM @date
* /
Void getNetworkImageView private () {
Loader ImageLoader = ImageLoader new (MyApplication.getRequestQueues) (), ImageCache new () {


@Override
Void putBitmap public (paramString String, paramBitmap Bitmap) {


}


@Override
Bitmap getBitmap public (paramString String) {
Null return;
}
});
MNetworkImageView.setDefaultImageResId (R.drawable.ic_launcher); / / set the default image loading in
MNetworkImageView.setErrorImageResId (R.drawable.ic_launcher); / / set the default image loading failure
MNetworkImageView.setImageUrl (IMAGE_URL, loader); / / transmission parameter is URL and ImageLoader.
}
}

Design sketch



top
Zero
step on
Zero
Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • Visit46364 times
    • Integral:Two thousand one hundred and ninety-four
    • Grade
    • Rank:10491st name
    • Original179
    • Reproduced:37
    • Translation:1
    • Comments:12
    Latest comments