Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

安卓翻書特效實作:FrameLayout 分層搭配手勢自繪

編輯:Android 開發實例

做電子書、雜誌這類專案時,翻頁動畫幾乎是必備。要做出自然的翻書效果,核心思路其實只有一句話:把「書頁內容」與「翻頁特效」分成兩層,各自獨立繪製。

為什麼選 FrameLayout 當根容器?因為 FrameLayout 本身就是圖層堆疊結構,後加入的子 View 會蓋在前面的子 View 之上。於是 mView 負責放真正的頁面內容,mBookView 疊在最上層負責畫翻頁的陰影與摺角,兩者互不干擾。

初始化結構

做法是先宣告一個 LinearLayout 成員 mView,把 page.xml inflate 成 View 後分別以 leftPage、rightPage 引用並填入資料,用 addView 加入 mView,再把 mView 加到 eBook 上。接著在 eBook 內部定義私有類別 BookView extends View,宣告成員 bookView mBookView 並同樣加入 eBook。這樣一來,mView 顯示的是書面內容,mBookView 顯示的是特效;後續各種手勢的動畫都畫在 mBookView 的畫布上即可。

Activity 程式碼

package eoe.book;

import Android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.ImageView;

public class Book extends Activity {

eBook mBook;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mBook = (eBook)findViewById(R.id.my_book);

mBook.addLayoutRecForPage(R.layout.page,21);

mBook.setListener(new eBook.Listener() {

public void onPrevPage() {

updateContent();

}

public void onNextPage() {

updateContent();

}

public void onInit() {

updateContent();

}

});

}

private void updateContent(){

int index = mBook.getIndexForLeftPage();

View left = mBook.getLeftPage(),right = mBook.getRightPage();

View next1 = mBook.getNextPage1(),next2 = mBook.getNextPage2();

View prev1 = mBook.getPrevPage1(),prev2 = mBook.getPrevPage2();

if(left != null)setImg(left,index);

if(right != null)setImg(right,index+1);

if(next1 != null)setImg(next1,index+2);

if(next2 != null)setImg(next2,index+3);

if(prev1 != null)setImg(prev1,index-1);

if(prev2 != null)setImg(prev2,index-2);

mBook.invalidate();

}

private void setImg(View v , int index){

if(index >= 0 && index < 20){

ImageView img = (ImageView)v.findViewById(R.id.book_img);

if(img == null)return;

Log.d("eBook","set Img");

switch(index%6){

case 0:

img.setImageResource(R.drawable.p1);

break;

case 1:

img.setImageResource(R.drawable.p2);

break;

case 2:

img.setImageResource(R.drawable.p3);

break;

case 3:

img.setImageResource(R.drawable.p4);

break;

case 4:

img.setImageResource(R.drawable.p5);

break;

case 5:

img.setImageResource(R.drawable.p6);

break;

default:

break;

}

}

}

值得注意的地方是 updateContent() 一次準備了六個頁面:當前左右頁、後面兩頁、前面兩頁。翻頁動畫期間,使用者會同時看到即將離開與即將進場的頁面,少了預先載入的前後頁,翻動瞬間就會出現空白。

版面配置

main.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<com.book.eBook android:id="@+id/my_book"

android:layout_width="fill_parent"

android:layout_height="fill_parent"/>

</LinearLayout>

page.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:padding="20dip"

android:background="#FFFFDD">

<ImageView android:layout_width="fill_parent" android:id="@+id/book_img"

android:layout_height="fill_parent" android:layout_weight="1"

android:scaleType="fitXY" android:src="@drawable/p1"/>

<com.book.TelEdit

android:id="@+id/book_text"

android:layout_width="fill_parent"

android:background="#ffffdd"

android:gravity="top"

android:typeface="sans"

android:capitalize="sentences"

android:lineSpacingExtra="5dip"

android:textSize="15dip"

android:textColor="#000000"

android:layout_height="fill_parent"

android:paddingTop="30dip"

android:layout_weight="1"/>

</LinearLayout>

自訂橫線文字方塊 TelEdit

電子書的質感有一半來自橫線紙。做法是自己寫一個 EditText 子類,在 onDraw 裡先用 Canvas 畫滿橫線,再呼叫 super.onDraw() 讓文字壓在線上:

package eoe.book;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.util.AttributeSet;

import android.view.WindowManager;

import android.widget.EditText;

public class TelEdit extends EditText {

Context mContext;

public TelEdit(Context context) {

super(context);

mContext = context;

}

public TelEdit(Context context, AttributeSet attrs) {

super(context, attrs);

mContext = context;

}

public TelEdit(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

mContext = context;

}

protected void onDraw(Canvas canvas) {

WindowManager wm = (WindowManager) mContext.getSystemService("window");

int windowWidth = wm.getDefaultDisplay().getWidth();

int windowHeight = wm.getDefaultDisplay().getHeight();

Paint paint = new Paint();

paint.setStyle(Paint.Style.FILL);

paint.setColor(Color.BLACK);

int paddingTop = getPaddingTop();

int paddingBottom = getPaddingBottom();

int scrollY = getScrollY();

int scrollX = getScrollX() + windowWidth;

int innerHeight = scrollY + getHeight() - paddingBottom;

int lineHeight = getLineHeight();

int baseLine = scrollY+ (lineHeight - ((scrollY - paddingTop) % lineHeight));

int x = 8;

while (baseLine < innerHeight) {

canvas.drawLine(x, baseLine, scrollX - x, baseLine, paint);

baseLine += lineHeight;

}

super.onDraw(canvas);

}

}

這裡最關鍵的設計是「線距等於行高」:baseLine 每次遞增 lineHeight,橫線才會剛好落在每行文字的基線上。同時把 scrollY 與 padding 都納入計算,捲動時線條才不會跑掉。

eBook 容器骨架

package eoe.book;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.LinearGradient;

import android.graphics.Paint;

import android.graphics.Path;

import android.graphics.Point;

import android.graphics.PorterDuffXfermode;

import android.graphics.Shader;

import android.graphics.PorterDuff.Mode;

import android.util.AttributeSet;

import android.util.Log;

import android.view.GestureDetector;

import android.view.LayoutInflater;

import android.view.MotionEvent;

import android.view.View;

import android.view.GestureDetector.OnGestureListener;

import android.widget.FrameLayout;

import android.widget.LinearLayout;

public class eBook extends FrameLayout{

public static final String LOG_TAG = "eBook";

List<Integer> myRecPages;

int totalPageNum;

Context mContext;

boolean hasInit = false;

final int defaultWidth = 600 , defaultHeight = 400;

int contentWidth = 0;

int contentHeight = 0;

View leftPage,rightPage,llPage,lrPage,rrPage,rlPage;

LinearLayout mView;

bookView mBookView;

boolean closeBook = false;

private enum Corner {

LeftTop,RightTop,LeftBottom,RightBottom,None

};

private Corner mSelectCorner;

final int clickCornerLen = 250*250; //50dip

float scrollX = 0,scrollY = 0;

int indexPage = 0;

private enum State {

ABOUT_TO_ANIMATE,ANIMATING,ANIMATE_END,READY,TRACKING

};

private State mState;

private Point aniStartPos;

private Point aniStopPos;

private Date aniStartTime;

private long aniTime = 2000;

private long timeOffset = 900;

Listener mListener;

private GestureDetector mGestureDetector;

private BookOnGestureListener mGestureListener;

public eBook(Context context) {

super(context);

Init(context);

}

public eBook(Context context, AttributeSet attrs) {

super(context, attrs);

Init(context);

}

}

從成員宣告就能看出整個特效的骨架:Corner 列舉用來記錄使用者摜的是哪個角,State 列舉則把翻頁拆成「即將動畫、動畫中、動畫結束、待機、拖曳中」五種狀態。GestureDetector 負責把手勢事件翻譯成動作,aniStartTime 搭配 aniTime 控制動畫時間軸,mBookView 則在每一幀把折角與陰影畫出來。

想把這個範例接成真實的電子書,主要工作會落在三處:把 setImg() 裡寫死的六張示意圖換成你自己的頁面資料來源、依實際內容調整 totalPageNum 與邊界判斷,以及在 eBook 的 onDraw 中補上折角路徑與 LinearGradient 陰影,讓翻頁過程有立體的明暗變化。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved