Project Practice ①-high imitation zhihu daily (1) realistic opening animation, high imitation zhihu

Source: Internet
Author: User

Project Practice ①-high imitation zhihu daily (1) realistic opening animation, high imitation zhihu

At the beginning of this article, I would like to thank my teacher, Comrade Li Weimin, for failing to do this thing without him. With his Demo approaching the real XX daily report


Debate on networking tools

Some people say that Xutils is better than volly, and some people say that volly has poor performance, but I use volly in this article. I don't feel the gap, on the contrary, I think it is nice to use it. In fact, I used Xutils myself, but I couldn't write down the demo later. It was completely completed after Comrade Li's code reconstruction.



① How to use Volley1. what is Volley when we need to communicate with the network in the program is generally used in AsyncTaskLoader, HttpURLConnection, AsyncTask, HTTPClient (Apache), etc.
Volley was released on Google I/O 2013. Volley is a network communication library on the Android platform, which enables faster, simpler, and more robust network communication.
In a speech by Google IO, the image is like a meteor.
 
2. Why do I need to use Volley1. the traditional network communication method to download images from the Internet may be like this process?

1. Read the image in ListAdapter # getView.

2. Use AsyncTask and other mechanisms to use HttpURLConnection to remove image resources from the server

3. Set the attributes of the corresponding ImageView in AsyncTask # onPostExecute.


Some situations may cause repeated loading.

1. When the screen rotates

2. When using ListView for quick scrolling


2. functions provided by Volley 1. asynchronous download of JSON and Images
2. Sort network requests (scheduling)
3. priority processing of network requests
4. Cache
5. Multi-Level cancellation request
6. Interaction between Activity and lifecycle (canceling all network requests at the end of Activity)


It feels more and more like a teaching post. I have returned to the project post. I used image loading and network loading in the Demo.
Picture loading NetworkImageView 1. cache in memory
2. cache in the cache folder
3. You can directly load the image through the Url and summarize the above. Incomplete ...... loading RequestQueue in json for reference only
OK. The homepage Effect of XX daily is achieved through animation. I still remember that I wrote a post about how to change the battery through animation, it is also a kind of animation implementation of a small program (5) security guard _ Power Management

Well, I still follow my teaching steps to see the layout first. In fact, the layout of Volly images is the same for SmartImageview. The layout is as follows:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <com.android.volley.toolbox.NetworkImageView        android:id="@+id/img_start"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scaleType="centerCrop"        tools:ignore="ContentDescription" />    <ImageView        android:id="@+id/img_logo"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:layout_marginBottom="20dp"        android:src="@drawable/splash_logo"        tools:ignore="ContentDescription" /></RelativeLayout>
② In fact, NetworkImageView needs to be obtained through the Internet. Let's look at the java code. 1. initialize the component.
/*** Initialization interface */private void initView () {imgStart = (NetworkImageView) findViewById (R. id. img_start );}

2. initialize data using the volly framework
/*** Initialize data */private void initData () {mQueue = Volley. newRequestQueue (getApplicationContext (); // Request Method of the first parameter, URL of the second parameter, and null of the returned data of the third parameter. I do not use post, the fourth parameter listener receives a JSON response to mQueue. add (new JsonObjectRequest (Method. GET, API. getStartImageUrl (), null, new Listener <JSONObject> () {@ Overridepublic void onResponse (JSONObject response) {try {// use the volly framework to add it directly to the image String imgUrl = response. getString ("img"); imgStart. setImageUrl (imgUrl, new ImageLoader (mQueue, new BitmapCache (); // Animation animation Animation = new ScaleAnimation (1.0f, 1.2f, 1.0f, 1.2f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f); // enlarge the image by 1.2 times and zoom in and out the animation from the center. setDuration (2000); // animation duration animation. setFillAfter (true); // animation stops at the animation end. setAnimationListener (WelcomeActivity. this); // Add an animation listener imgStart. startAnimation (animation); // start the animation} catch (JSONException e) {e. printStackTrace () ;}}, null ));}
3. Set an animation to listen to the jump after the animation ends
// Animation listener @ Overridepublic void onAnimationStart (animation Animation) {}@ Overridepublic void onAnimationEnd (animation Animation) {// jump to the homepage startActivity (new Intent (this, MainActivity. class); finish () ;}@ Overridepublic void onAnimationRepeat (Animation animation ){}


All code
Import org. json. JSONException; import org. json. JSONObject; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. animation. animation; import android. view. animation. animation. animationListener; import android. view. animation. scaleAnimation; import com. android. volley. request. method; import com. android. volley. requestQueue; import com. android. volley. response. listener; import com. android. volley. toolbox. imageLoader; import com. android. volley. toolbox. jsonObjectRequest; import com. android. volley. toolbox. networkImageView; import com. android. volley. toolbox. volley; import com. qf. teach. project. zhihudaily. r; import com. qf. teach. project. zhihudaily. c. API; import com. qf. teach. project. zhihudaily. cache. bitmapCache; public class WelcomeActivity extends Activity implements AnimationListener {private NetworkImageView imgStart; private RequestQueue mQueue; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_welcome); initView (); initData ();}/*** initialization interface */private void initView () {imgStart = (NetworkImageView) findViewById (R. id. img_start);}/*** initialization data */private void initData () {mQueue = Volley. newRequestQueue (getApplicationContext (); // Request Method of the first parameter, URL of the second parameter, and null of the returned data of the third parameter. I do not use post, the fourth parameter listener receives a JSON response to mQueue. add (new JsonObjectRequest (Method. GET, API. getStartImageUrl (), null, new Listener <JSONObject> () {@ Overridepublic void onResponse (JSONObject response) {try {// use the volly framework to add it directly to the image String imgUrl = response. getString ("img"); imgStart. setImageUrl (imgUrl, new ImageLoader (mQueue, new BitmapCache (); // Animation animation Animation = new ScaleAnimation (1.0f, 1.2f, 1.0f, 1.2f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f); // enlarge the image by 1.2 times and zoom in and out the animation from the center. setDuration (2000); // animation duration animation. setFillAfter (true); // animation stops at the animation end. setAnimationListener (WelcomeActivity. this); // Add an animation listener imgStart. startAnimation (animation); // start the animation} catch (JSONException e) {e. printStackTrace () ;}}}, null);} // Animation listener @ Overridepublic void onAnimationStart (animation Animation) {}@ Overridepublic void onAnimationEnd (animation Animation) {// jump to startActivity (new Intent (this, MainActivity. class); finish () ;}@ Overridepublic void onAnimationRepeat (Animation animation ){}}








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.