項目實戰①—高仿知乎日報(1)逼真的開場動畫,高仿知

來源:互聯網
上載者:User

項目實戰①—高仿知乎日報(1)逼真的開場動畫,高仿知

在這一篇開頭我要感謝我的老師李衛民同志,沒有他這個東西做不出來,有了他這個Demo逼近真正的XX日報


〇連網工具類的爭論

在連網工具類中,有人說,Xutils 比volly牛逼,又有人說其實volly效能很差,不過我這一篇文章用的就是volly,我感覺不到差距在哪,反而覺得用起來很爽。其實我自己用的就是Xutils不過後面demo寫下不下去了,經過李同志的代碼重構,得以完全。



① 如何使用Volley1.什麼是Volley我們在程式中需要和網路通訊的時候,大體使用的東西莫過於AsyncTaskLoader,HttpURLConnection,AsyncTask,HTTPClient(Apache)等
Google I/O 2013上,Volley發布了。Volley是Android平台上的網路通訊庫,能使網路通訊更快,更簡單,更健壯。
在Google IO的演講上,其配圖是一幅發射火弓箭的圖,有點類似流星。
 
2.為什麼要使用Volley1.傳統的網路通訊方式從網上下載圖片的步驟可能是這樣的流程

1.在ListAdapter#getView()裡開始映像的讀取。

2.通過AsyncTask等機制使用HttpURLConnection從伺服器去的圖片資源

3.在AsyncTask#onPostExecute()裡設定相應ImageView的屬性。


有些會導致重複載入的情況

1.旋轉螢幕的時候

2.使用ListView快速滾動的時候


2.Volley提供的功能1.JSON,映像等的非同步下載
2.網路請求的排序(scheduling)
3.網路請求的優先順序處理
4.緩衝
5.多層級取消請求
6.Activity和生命週期的聯動(Activity結束時同時取消所有網路請求)


好啦 感覺越來越像教學文章了,回到項目貼了。我在Demo裡面用到也就是圖片載入,和網路載入
圖片載入 NetworkImageView一、緩衝在記憶體中
二、緩衝在cache檔案夾
三、直接可以通過Url載入圖片 以上自己總結,不全.......僅供參考json載入 RequestQueue 
OK ,XX日報的首頁效果,是通過動畫效果實現的,還記得我寫過一篇貼子,通過動畫實現 電池的變化,也是動畫的一種實現 練手小項目(5)安全衛士_電源管理

好啦 我還是按照我的教學步驟 先看布局 其實Volly的圖片載入適合SmartImageview是一樣的  布局如下
<?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>
②代碼編寫其實NetworkImageView裡面是需要靠連網擷取的。再看java代碼1.初始化組件 
/** * 初始化介面 */private void initView() {imgStart = (NetworkImageView) findViewById(R.id.img_start);}

2.初始化資料使用volly架構
/** * 初始化資料 */private void initData() {mQueue = Volley.newRequestQueue(getApplicationContext());//第一個參數 請求方式,第二個參數是URL,第三個參數 返回資料設為空白我不是用post,第四個參數 監聽器接收JSON響應mQueue.add(new JsonObjectRequest(Method.GET, API.getStartImageUrl(), null, new Listener<JSONObject>() {@Overridepublic void onResponse(JSONObject response) {try {//使用volly架構直接加在圖片String imgUrl = response.getString("img");imgStart.setImageUrl(imgUrl, new ImageLoader(mQueue, new BitmapCache()));// 圖片動畫Animation animation = new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); // 將圖片放大1.2倍,從中心開始縮放animation.setDuration(2000); // 動畫期間animation.setFillAfter(true); // 動畫結束後停留在結束的位置animation.setAnimationListener(WelcomeActivity.this); // 添加動畫監聽imgStart.startAnimation(animation); // 啟動動畫} catch (JSONException e) {e.printStackTrace();}}}, null));}
3.設定動畫監聽動畫結束後跳轉
// 動畫監聽@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {// 動畫結束時跳轉至首頁startActivity(new Intent(this, MainActivity.class));finish();}@Overridepublic void onAnimationRepeat(Animation animation) {}


全部代碼 
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();}/** * 初始化介面 */private void initView() {imgStart = (NetworkImageView) findViewById(R.id.img_start);}/** * 初始化資料 */private void initData() {mQueue = Volley.newRequestQueue(getApplicationContext());//第一個參數 請求方式,第二個參數是URL,第三個參數 返回資料設為空白我不是用post,第四個參數 監聽器接收JSON響應mQueue.add(new JsonObjectRequest(Method.GET, API.getStartImageUrl(), null, new Listener<JSONObject>() {@Overridepublic void onResponse(JSONObject response) {try {//使用volly架構直接加在圖片String imgUrl = response.getString("img");imgStart.setImageUrl(imgUrl, new ImageLoader(mQueue, new BitmapCache()));// 圖片動畫Animation animation = new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); // 將圖片放大1.2倍,從中心開始縮放animation.setDuration(2000); // 動畫期間animation.setFillAfter(true); // 動畫結束後停留在結束的位置animation.setAnimationListener(WelcomeActivity.this); // 添加動畫監聽imgStart.startAnimation(animation); // 啟動動畫} catch (JSONException e) {e.printStackTrace();}}}, null));}// 動畫監聽@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {// 動畫結束時跳轉至首頁startActivity(new Intent(this, MainActivity.class));finish();}@Overridepublic void onAnimationRepeat(Animation animation) {}}








聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.