編輯:關於 Android 程式設計


用過多米音樂的朋友都知道,它的某個 UI 可以上下滑動——作用嘛,無聊時可以劃劃解解悶。這種細節往往被形容成一種「情懷」,不過叫「情味」或許更貼切。如今行動網際網路發展得如此迅速,市場上早已不是早期隨便敲個 App 上架就能輕鬆擁有幾十萬用戶的階段了。至於行動發展趨勢,我想搞行動 IT 的人心裡都比擬清楚,這裡就不扯遠了。
其實應用 UI 特效是應用中很重要的一部分。如果同樣功能的兩款軟體,一個體驗好點、另一個略微差點,身為用戶的你毫無疑問會選擇體驗更好的那個。總結就是一句話:「操作性」對於產品起著至關重要的作用。
接下來我們看看如何實現。首先聲明,這個實現方式並不是最好的,我在這裡只是提出一個解決方案,大家可以根據自己的想法進行創新。
道理很簡單:RelativeLayout + 自訂 ScrollView。
我們大致看一下版面配置結構:


其實也沒什麼技術含量,我簡單介紹一下:紅色代表的是背景照片,綠色代表自訂 ScrollView,粉色代表你要編輯的透明區域。想必大家都明確,我們還是直接看程式碼吧。
由於屬於情懷特效(沒有具體的回調事件要求),所以就沒有必要自訂監聽、回調處理,我直接把要處理的 UI 注入到自訂元件中,這樣她方便我也方便。
首先我們把背景圖片和頂部線條注入到該元件中,接著看 onTouchEvent 事件,因為自始至终都是它在起作用。
/**
* 觸摸事件
* @param ev
*/
public void commOnTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
initTouchY = ev.getY();
current_Top = initTop = imageView.getTop();
current_Bottom = initBottom = imageView.getBottom();
lineUp_current_Top = line_up_top = line_up.getTop();
lineUp_current_Bottom = line_up_bottom = line_up.getBottom();
break;
case MotionEvent.ACTION_UP:
/** 回縮動畫 **/
if (isNeedAnimation()) {
animation();
}
isMoveing = false;
touchY = 0;// 手指松開要歸0.
break;
/**
* 消除出第一次挪動計算,因為第一次無法得知deltaY的高度,
* 然而我們也要進行初始化,就是第一次挪動的時候讓滑動距離歸0.
*/
case MotionEvent.ACTION_MOVE:
touchY = ev.getY();
float deltaY = touchY - initTouchY;// 滑動距離
/** 過濾: **/
if (deltaY < 0 && inner.getTop() <= 0) {
return;
}
// 當滾動到最上或者最下時就不會再滾動,這時挪動布局
isNeedMove();
if (isMoveing) {
// 初始化頭部矩形
if (normal.isEmpty()) {
// 保存正常的布局位置
normal.set(inner.getLeft(), inner.getTop(),
inner.getRight(), inner.getBottom());
}
// 挪動布局(手勢挪動的1/3)
float inner_move_H = deltaY / 5;
inner.layout(normal.left, (int) (normal.top + inner_move_H),
normal.right, (int) (normal.bottom + inner_move_H));
/** image_bg **/
float image_move_H = deltaY / 10;
current_Top = (int) (initTop + image_move_H);
current_Bottom = (int) (initBottom + image_move_H);
imageView.layout(imageView.getLeft(), current_Top,
imageView.getRight(), current_Bottom);
/** line_up **/
float line_up_H = inner_move_H;
lineUp_current_Top = (int) (line_up_top + inner_move_H);
lineUp_current_Bottom = (int) (line_up_bottom + inner_move_H);
line_up.layout(line_up.getLeft(), lineUp_current_Top,
line_up.getRight(), lineUp_current_Bottom);
}
break;
default:
break;
}
}
簡單說明:
MotionEvent.ACTION_DOWN:觸摸摁下獲得相應的坐標。
MotionEvent.ACTION_MOVE:裡面有個方法 isNeedMove,作用是判斷我們滑動的是 ScrollView 自身,還是我們自己模擬的那種滑動。
/**
* 是不是須要挪動布局
* inner.getMeasuredHeight():獲得的是控件的總高度
* getHeight():獲得的是屏幕的高度
*/
public void isNeedMove() {
int offset = inner.getMeasuredHeight() - getHeight();
int scrollY = getScrollY();
// 如果ScrollView的子View們沒有超越一屏幕則scrollY == 0,直接返回true,
// 如果ScrollView的子View們超越了一屏幕則 getScrollY()==offset說明滑到了ScrollView的低端.這時候才返回true.
if (scrollY == 0 || scrollY == offset) {
isMoveing = true;
}
}
這裡面用到最多的就是 view.layout(l, t, r, b),作用很簡單,詳情請參看源碼。
MotionEvent.ACTION_UP:就是做些善後操作,主要看 animation 方法。
/**
* 回縮動畫
*/
public void animation() {
TranslateAnimation image_Anim = new TranslateAnimation(0, 0,
Math.abs(initTop - current_Top), 0);
image_Anim.setDuration(200);
imageView.startAnimation(image_Anim);
imageView.layout(imageView.getLeft(), (int) initTop,
imageView.getRight(), (int) initBottom);
// 開啟挪動動畫
TranslateAnimation inner_Anim = new TranslateAnimation(0, 0,
inner.getTop(), normal.top);
inner_Anim.setDuration(200);
inner.startAnimation(inner_Anim);
inner.layout(normal.left, normal.top, normal.right, normal.bottom);
/** line_up **/
TranslateAnimation line_up_Anim = new TranslateAnimation(0, 0,
Math.abs(line_up_top - lineUp_current_Top), 0);
line_up_Anim.setDuration(200);
line_up.startAnimation(line_up_Anim);
line_up.layout(line_up.getLeft(), line_up_top, line_up.getRight(),
line_up_bottom);
normal.setEmpty();
/** 動畫執行 **/
if (current_Top > initTop + 50 && turnListener != null)
turnListener.onTurn();
}
這裡我要簡單說明一下,因為我在這裡栽了些時光。
比如:我們的背景圖片本來坐標為(0,-190,800,300),隨著手勢挪動到(0,-100,800,390),挪動了 90 像素,那麼我們的 TranslateAnimation 應當如何寫呢?我之前總以為不就是末尾坐標指向初始坐標不就完了,結果你會發現動畫基本不起作用而是一閃而過。原因呢,動畫參數不可為負數(或許因為動畫是以(0,0)為參照物吧)。因此要把動畫寫成 new TranslateAnimation(0, 0, Math.abs(-190 - (-100)), 0),這樣我們所需要的動畫效果就實現了。
但是新的問題又出現了:當你下拉到一定狀態後然後漸漸向上挪動,會發現挪動得很快(沒有回縮的反響),而挪動到最頂部的時候突然又出現反彈效果。這個效果固然不是我們所需要的那種。我們所需要的效果是:下拉到一定程度,然後反過來上拉的時候要漸漸地挪動回到原點(中央位置)停止;如果是上拉的話,不要出現反彈效果;如果是下拉松開的話,才出現反彈效果。
首先,比如我們手勢下拉了 50 像素,其實是使得自訂 ScrollView 的孩子也就是 LinearLayout 這個元件的 top 為 50,而這個時候的 getScrollY() 的值仍為 0。但是如果此時你停止下拉反而向上拉取的話,那麼此時的 getScrollY() 會從 0 開始逐步增大,當我們挪動到頂部也就是將 ScrollView 挪動到最底部,此時的 isMoveing 為 true,所以你繼續上拉的話會出現反彈效果。
這個問題要如何解決呢?其實也不難。比如我們手勢下拉了 50 像素,那麼此時 touch 的距離也就是 50 像素,如果此時我們反向上拉的話,同樣是須要 50 像素回到最初的位置。也就是說,我們要將操作離開,分為 UP、DOWN:如果是 DOWN 的話,那麼在下拉後執行上拉的時候我們禁用掉自訂元件的滑動,而是通過手勢執行 layout 執行這 50 像素。
以下是部分程式碼:
/** 對於初次Touch操作要判斷方位:UP OR DOWN **/
if (deltaY < 0 && state == state.NOMAL) {
state = State.UP;
} else if (deltaY > 0 && state == state.NOMAL) {
state = State.DOWN;
}
if (state == State.UP) {
deltaY = deltaY < 0 ? deltaY : 0;
isMoveing = false;
shutTouch = false;
} else if (state == state.DOWN) {
if (getScrollY() <= deltaY) {
shutTouch = true;
isMoveing = true;
}
deltaY = deltaY < 0 ? 0 : deltaY;
}
touch 事件處理:
/** touch 事件處理 **/
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (inner != null) {
commOnTouchEvent(ev);
}
// ture:禁止控件本身的滑動.
if (shutTouch)
return true;
else
return super.onTouchEvent(ev);
}
說明:如果返回值為 true,作用是禁止 ScrollView 的滑動,此時的 Touch 事件還在哦!如果對 Touch 事件比較熟悉的同學,相信明白我的意思。
package com.example.scrollviewdemo;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.ScrollView;
/**
* 自定義ScrollView
* @author jia
*/
public class PersonalScrollView extends ScrollView {
private final String TAG = PersonalScrollView.class.getSimpleName();
private View inner;// 孩子View
private float touchY;// 點擊時Y坐標
private float deltaY;// Y軸滑動的距離
private float initTouchY;// 初次點擊的Y坐標
private boolean shutTouch = false;// 是不是關閉ScrollView的滑動.
private Rect normal = new Rect();// 矩形
private boolean isMoveing = false;// 是不是開始挪動.
private ImageView imageView;// 背景圖控件.
private View line_up;// 上線
private int line_up_top;// 上線的top
private int line_up_bottom;// 上線的bottom
private int initTop, initBottom;// 初始高度
private int current_Top, current_Bottom;// 拖動時時高度。
private int lineUp_current_Top, lineUp_current_Bottom;// 上線
private onTurnListener turnListener;
private ImageView imageHeader;
public void setImageHeader(ImageView imageHeader) {
this.imageHeader = imageHeader;
}
// 狀態:上部,下部,默認
private enum State {
UP, DOWN, NOMAL
};
// 默認狀態
private State state = State.NOMAL;
public void setTurnListener(onTurnListener turnListener) {
this.turnListener = turnListener;
}
public void setLine_up(View line_up) {
this.line_up = line_up;
}
// 注入背景圖
public void setImageView(ImageView imageView) {
this.imageView = imageView;
}
public PersonalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 根據 XML 生成視圖工作實現.該函數在生成視圖的最後調用,在所有子視圖添加完以後.
*/
@Override
protected void onFinishInflate() {
if (getChildCount() > 0) {
inner = getChildAt(0);
}
}
/** touch 事件處理 **/
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (inner != null) {
commOnTouchEvent(ev);
}
if (shutTouch)
return true;
else
return super.onTouchEvent(ev);
}
/**
* 觸摸事件
* @param ev
*/
public void commOnTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
initTouchY = ev.getY();
current_Top = initTop = imageView.getTop();
current_Bottom = initBottom = imageView.getBottom();
if (line_up_top == 0) {
lineUp_current_Top = line_up_top = line_up.getTop();
lineUp_current_Bottom = line_up_bottom = line_up.getBottom();
}
break;
case MotionEvent.ACTION_UP:
if (isNeedAnimation()) {
animation();
}
if (getScrollY() == 0) {
state = State.NOMAL;
}
isMoveing = false;
touchY = 0;
shutTouch = false;
break;
case MotionEvent.ACTION_MOVE:
touchY = ev.getY();
deltaY = touchY - initTouchY;// 滑動距離
/** 對於初次Touch操作要判斷方位:UP OR DOWN **/
if (deltaY < 0 && state == state.NOMAL) {
state = State.UP;
} else if (deltaY > 0 && state == state.NOMAL) {
state = State.DOWN;
}
if (state == State.UP) {
deltaY = deltaY < 0 ? deltaY : 0;
isMoveing = false;
shutTouch = false;
/** line_up **/
lineUp_current_Top = (int) (line_up_top - getScrollY());
lineUp_current_Bottom = (int) (line_up_bottom - getScrollY());
line_up.layout(line_up.getLeft(), lineUp_current_Top,
line_up.getRight(), lineUp_current_Bottom);
} else if (state == state.DOWN) {
if (getScrollY() <= deltaY) {
shutTouch = true;
isMoveing = true;
}
deltaY = deltaY < 0 ? 0 : deltaY;
}
if (isMoveing) {
if (normal.isEmpty()) {
normal.set(inner.getLeft(), inner.getTop(),
inner.getRight(), inner.getBottom());
}
float inner_move_H = deltaY / 5;
inner.layout(normal.left, (int) (normal.top + inner_move_H),
normal.right, (int) (normal.bottom + inner_move_H));
/** image_bg **/
float image_move_H = deltaY / 10;
current_Top = (int) (initTop + image_move_H);
current_Bottom = (int) (initBottom + image_move_H);
imageView.layout(imageView.getLeft(), current_Top,
imageView.getRight(), current_Bottom);
/** line_up **/
lineUp_current_Top = (int) (line_up_top + inner_move_H);
lineUp_current_Bottom = (int) (line_up_bottom + inner_move_H);
line_up.layout(line_up.getLeft(), lineUp_current_Top,
line_up.getRight(), lineUp_current_Bottom);
}
break;
default:
break;
}
}
/**
* 回縮動畫
*/
public void animation() {
TranslateAnimation image_Anim = new TranslateAnimation(0, 0,
Math.abs(initTop - current_Top), 0);
image_Anim.setDuration(200);
imageView.startAnimation(image_Anim);
imageView.layout(imageView.getLeft(), (int) initTop,
imageView.getRight(), (int) initBottom);
// 開啟挪動動畫
TranslateAnimation inner_Anim = new TranslateAnimation(0, 0,
inner.getTop(), normal.top);
inner_Anim.setDuration(200);
inner.startAnimation(inner_Anim);
inner.layout(normal.left, normal.top, normal.right, normal.bottom);
/** line_up **/
TranslateAnimation line_up_Anim = new TranslateAnimation(0, 0,
Math.abs(line_up_top - lineUp_current_Top), 0);
line_up_Anim.setDuration(200);
line_up.startAnimation(line_up_Anim);
line_up.layout(line_up.getLeft(), line_up_top, line_up.getRight(),
line_up_bottom);
normal.setEmpty();
/** 動畫執行 **/
if (current_Top > initTop + 50 && turnListener != null)
turnListener.onTurn();
}
/** 是不是須要開啟動畫 **/
public boolean isNeedAnimation() {
return !normal.isEmpty();
}
/**
* 執行翻轉
* @author jia
*/
public interface onTurnListener {
/** 必須到達一定水平才執行 **/
void onTurn();
}
}
最後還有一個小 BUG,也就是那個頂部拉線:如果你讓 ScrollView 慣性滑動的話,那麼你會發現頂部線條沒有跟隨挪動,其實就是因為慣性滑動的時候我們是得不到 getScrollY() 的值造成的,這個問題就臨時留在這裡,有時間了再續。
效果圖:



界面有點丑陋,不過 UI 可以自己根據需求進行調整。
自定義布局實現雙向側滑菜單(下篇)
上一節我們已經說明了側滑菜單的實現原理,並完成了單側菜單。這一節我們來完善專案,實現雙向側滑菜單。原理若不清楚可回看上節,這裡直接上程式碼。 /** * 這個類和 S
Android Fragment 使用建議與常見問題
Fragment 基本使用 Activity 透過 FragmentManager 管理 Fragment。新增 Fragment 時,通常需先判斷是否已存在,避免 Ac
Android Tab 實作:ViewPager 與 PagerTitleStrip/PagerTabStrip
ViewPager 搭配 PagerTitleStrip 或 PagerTabStrip,可以快速建立帶有標題與切換指示效果的頁面。使用者可以透過滑動或點擊標籤切換內容,
Android 建立與解析 XML(二)——Dom 方式詳解
1. Dom 概述以 Dom 方式建立 XML,用的是標準的 xml 建構器 javax.xml.parsers.DocumentBuilder,需要導入以下內容: ja