Android仿人人網(wǎng)滑動側(cè)邊欄效果
很多應(yīng)用為了節(jié)省空間而又使界面能夠充足的顯示信息,大多數(shù)應(yīng)用都采用了側(cè)邊欄的方式,如下圖:
來說說它的思路,底下是兩個或多個視圖,分別通過控制它們的寬度、左邊距來控制它們的顯示,來看看代碼
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context=".MainActivity" > <LinearLayout android:id="@+id/menu" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/menu" > </LinearLayout> <LinearLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/content" > </LinearLayout> </LinearLayout>
MainActivity.java
public class MainActivity extends Activity implements OnTouchListener { private LinearLayout menu; private LinearLayout content; private LayoutParams menuParams; private LayoutParams contentParams; // menu完全顯示時,留給content的寬度值。 private static final int menuPadding = 80; // 分辨率 private int disPlayWidth; private float xDown; private float xMove; private boolean mIsShow = false; private static final int speed = 50; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); disPlayWidth = getWindowManager().getDefaultDisplay().getWidth(); menu = (LinearLayout) findViewById(R.id.menu); content = (LinearLayout) findViewById(R.id.content); menuParams = (LayoutParams) menu.getLayoutParams(); contentParams = (LayoutParams) content.getLayoutParams(); findViewById(R.id.layout).setOnTouchListener(this); menuParams.width = disPlayWidth - menuPadding; contentParams.width = disPlayWidth; showMenu(mIsShow); } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: showMenu(!mIsShow); break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: break; } return true; } private void showMenu(boolean isShow) { if (isShow) { mIsShow = true; menuParams.leftMargin = 0; } else { mIsShow = false; menuParams.leftMargin = 0 - menuParams.width; } menu.setLayoutParams(menuParams); } }
上述代碼只是用兩張圖片代替了兩個復(fù)雜的view(layout),你會發(fā)現(xiàn),兩個視圖雖然可以切換,但沒有動畫的感覺,再加上要有拖動效果,所以,我們再給它加個平移時間段,看起來有動畫的效果
package com.example.test; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.Window; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; public class MainActivity extends Activity implements OnTouchListener, OnClickListener { private LinearLayout menu; private LinearLayout content; private LayoutParams menuParams; private LayoutParams contentParams; // menu完全顯示時,留給content的寬度值。 private static final int menuPadding = 80; // 分辨率 private int disPlayWidth; private float xDown; private float xMove; private boolean mIsShow = false; private static final int speed = 50; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); disPlayWidth = getWindowManager().getDefaultDisplay().getWidth(); menu = (LinearLayout) findViewById(R.id.menu); menu.setOnClickListener(this); content = (LinearLayout) findViewById(R.id.content); content.setOnClickListener(this); menuParams = (LayoutParams) menu.getLayoutParams(); contentParams = (LayoutParams) content.getLayoutParams(); //findViewById(R.id.layout).setOnTouchListener(this); menuParams.width = disPlayWidth - menuPadding; contentParams.width = disPlayWidth; showMenu(mIsShow); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.menu: new showMenuAsyncTask().execute(-50); break; case R.id.content: new showMenuAsyncTask().execute(50); break; } } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: showMenu(!mIsShow); break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: break; } return true; } private void showMenu(boolean isShow) { if (isShow) { mIsShow = true; menuParams.leftMargin = 0; } else { mIsShow = false; menuParams.leftMargin = 0 - menuParams.width; } menu.setLayoutParams(menuParams); } /** * *這是主要代碼:模擬動畫過程,也讓我更熟悉了AsyncTask這玩意兒 * */ class showMenuAsyncTask extends AsyncTask<Integer, Integer, Integer> { @Override protected Integer doInBackground(Integer... params) { int leftMargin = menuParams.leftMargin; //這里也是值得學(xué)習(xí)的地方,如果在平常,自己肯定又是這樣寫: // if(){ // while() // } // else if(){ // while() // } while (true) { leftMargin += params[0]; if (params[0] > 0 && leftMargin >= 0) { break; } else if (params[0] < 0 && leftMargin <= -menuParams.width) { break; } publishProgress(leftMargin); try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } } return leftMargin; } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); menuParams.leftMargin = values[0]; menu.setLayoutParams(menuParams); } @Override protected void onPostExecute(Integer result) { super.onPostExecute(result); menuParams.leftMargin = result; menu.setLayoutParams(menuParams); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實現(xiàn)屏幕旋轉(zhuǎn)方法總結(jié)
這篇文章主要介紹了Android實現(xiàn)屏幕旋轉(zhuǎn)方法,實例總結(jié)了屏幕旋轉(zhuǎn)的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04android指定DatePickerDialog樣式并不顯示年的實現(xiàn)代碼
下面小編就為大家?guī)硪黄猘ndroid指定DatePickerDialog樣式并不顯示年的實現(xiàn)代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,祝大家游戲愉快哦2016-08-08Android開發(fā)中TextView文本過長滾動顯示實現(xiàn)方法分析
這篇文章主要介紹了Android開發(fā)中TextView文本過長滾動顯示實現(xiàn)方法,結(jié)合實例形式分析了Android項目開發(fā)中TextView顯示超長文本的具體操作技巧與注意事項,需要的朋友可以參考下2018-02-02Android開發(fā)之獲取LayoutInflater對象的方法總結(jié)
這篇文章主要介紹了Android開發(fā)之獲取LayoutInflater對象的方法,結(jié)合實例形式總結(jié)分析了Android獲取LayoutInflater對象的常用技巧,需要的朋友可以參考下2016-02-02android判斷手機(jī)是否安裝地圖應(yīng)用實現(xiàn)跳轉(zhuǎn)到該地圖應(yīng)用
這篇文章主要給大家介紹了android如何判斷手機(jī)是否安裝地圖應(yīng)用,并實現(xiàn)跳轉(zhuǎn)到該地圖應(yīng)用的方法,需要的朋友可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-01-01