源碼裏面有3套輸入法,位置:Z:\myandroid\packages\inputmethods
openwnn是一家日本公司開發的開源輸入法框架,涉及中文、日文、韓文。目前已經加入到了Android源碼之中。因此你打開一個模擬器時,會發現其中有一個japanese ime的輸入法,其服務名爲openwnn,這個就是openwnn的日文輸入法
latin 虛擬即盤
google是PinyinIME ,後續我們加入了手寫,爲第三方庫支持,13年10月份合併手寫和拼音輸入法!
現在合併後的爲PateoIME
上面截圖原因,還有個XmlKeyboardLoader.Java
一、ComposingView
- /**
- * View used to show composing string (The Pinyin string for the unselected
- * syllables and the Chinese string for the selected syllables.)
- * 拼音字符串View,用於顯示輸入的拼音
- */
- public class ComposingView extends View {
其需要設置的字符通過下面方法傳入PateoIME.DecodingInfo decInfo,decInfo中包含了輸入的字符,同時進行刷新view
- /**
- * Set the composing string to show. If the IME status is
- * {@link PateoIME.ImeState#STATE_INPUT}, the composing view's status will
- * be set to {@link ComposingStatus#SHOW_PINYIN}, otherwise the composing
- * view will set its status to {@link ComposingStatus#SHOW_STRING_LOWERCASE}
- * or {@link ComposingStatus#EDIT_PINYIN} automatically.
- * 設置 解碼操作對象,然後刷新View
- */
- public void setDecodingInfo(PateoIME.DecodingInfo decInfo,
- PateoIME.ImeState imeStatus) {
其主要繪製在onDraw的drawForPinyin(canvas);方法中,通過canvas.drawText寫入
而上面的setDecodingInfo方法主要在InputMethodService的繼承的子類中被調用如下:
- private void updateComposingText(boolean visible) {
- if (!visible) {
- mComposingView.setVisibility(View.INVISIBLE);
- } else {
- mComposingView.setDecodingInfo(mDecInfo, mImeState);
- mComposingView.setVisibility(View.VISIBLE);
- }
- mComposingView.invalidate();
- }
二、SoundManager
- /**
- * Class used to manage related sound resources.
- * 按鍵聲音管理類、單例
- */
- public class SoundManager {
播放聲音前需要註冊相應的音頻策略,下面爲播放聲音代碼:
- public void playKeyDown() {
- if (mAudioManager == null) {
- updateRingerMode();
- }
- if (!mSilentMode) {
- int sound = AudioManager.FX_KEYPRESS_STANDARD;
- mAudioManager.playSoundEffect(sound, FX_VOLUME);
- }
- }
相應的上面響應按鍵的方法被調用在SoftKeyboardView類的下面方法中
- // If movePress is true, means that this function is called because user
- // moves his finger to this button. If movePress is false, means that this
- // function is called when user just presses this key.
- public SoftKey onKeyPress(int x, int y,
- SkbContainer.LongPressTimer longPressTimer, boolean movePress) {
- if (!movePress) {
- tryPlayKeyDown();
- tryVibrate();
- }
我們項目中是另外的處理方式,其中特別要注意,在應用監聽案件事件的時候需要返回true,不然可能按下鍵盤鍵聽到兩次按鍵音
- /**
- * Class for soft keys which defined in the keyboard xml file. A soft key can be
- * a basic key or a toggling key.
- *
- * @see com.android.inputmethod.pateoime.SoftKey
- */
- public class SoftKeyToggle extends SoftKey {
上面主要定義的爲切換鍵,相應的配置<toggle_state>
四、class SoftKey
上面主要爲相應的按鍵
五、SoftKeyboard
- /**
- * Class used to represent a soft keyboard definition, including the height, the
- * background image, the image for high light, the keys, etc.
- */
- public class SoftKeyboard {
此類中有個很關鍵的函數,就是通過xy座標值找到相應的臨近按鍵
- public SoftKey mapToKey(int x, int y) {
六、SkbTemplate
- /**
- * Soft keyboard template used by soft keyboards to share common resources. In
- * this way, memory cost is reduced.
- */
- public class SkbTemplate {
上面主要爲:共享公共資源軟鍵盤模板,對應xml資源包下的skb_template.xml、可以根據不同的設備來加載不同的公共資源,其他自由也類似
七、SkbPool
- /**
- * Class used to cache previously loaded soft keyboard layouts.
- */
- public class SkbPool {
上面說明:用來緩存以前加載的軟鍵盤佈局,是一個軟鍵盤緩存池,該類有如下兩個變量
- private Vector<SkbTemplate> mSkbTemplates = new Vector<SkbTemplate>();
- private Vector<SoftKeyboard> mSoftKeyboards = new Vector<SoftKeyboard>();
即上面看出緩存了佈局模板和軟鍵盤兩個列表
八、SkbContainer
- /**
- * The top container to host soft keyboard view(s).
- */
- public class SkbContainer extends RelativeLayout implements OnTouchListener {
上面所說:頂層容器【集裝箱】來承載軟鍵盤視圖
- /**
- * 更新軟鍵盤佈局
- */
- private void updateSkbLayout() {
- int screenWidth = mEnvironment.getScreenWidth();
- int keyHeight = mEnvironment.getKeyHeight();
- int skbHeight = mEnvironment.getSkbHeight();
- Resources r = mContext.getResources();
- if (null == mSkbFlipper) {
- mSkbFlipper = (ViewFlipper) findViewById(R.id.alpha_floatable);
- }
- mMajorView = (SoftKeyboardView) mSkbFlipper.getChildAt(0);
- SoftKeyboard majorSkb = null;
- SkbPool skbPool = SkbPool.getInstance();
- switch (mSkbLayout) {
- case R.xml.skb_qwerty:
- majorSkb = skbPool.getSoftKeyboard(R.xml.skb_qwerty,
- R.xml.skb_qwerty, screenWidth, skbHeight, mContext);
- break;
- case R.xml.skb_sym1:
- majorSkb = skbPool.getSoftKeyboard(R.xml.skb_sym1, R.xml.skb_sym1,
- screenWidth, skbHeight, mContext);
- break;
- case R.xml.skb_sym2:
- majorSkb = skbPool.getSoftKeyboard(R.xml.skb_sym2, R.xml.skb_sym2,
- screenWidth, skbHeight, mContext);
- break;
- case R.xml.skb_smiley:
- majorSkb = skbPool.getSoftKeyboard(R.xml.skb_smiley,
- R.xml.skb_smiley, screenWidth, skbHeight, mContext);
- break;
- case R.xml.skb_phone:
- majorSkb = skbPool.getSoftKeyboard(R.xml.skb_phone,
- R.xml.skb_phone, screenWidth, skbHeight, mContext);
- break;
- default:
- }
- if (null == majorSkb || !mMajorView.setSoftKeyboard(majorSkb)) {
- return;
- }
- mMajorView.setBalloonHint(mBalloonOnKey, mBalloonPopup, false);
- mMajorView.invalidate();
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- super.onTouchEvent(event);
- if (mSkbFlipper.isFlipping()) {
- resetKeyPress(0);
- return true;
- }
- int x = (int) event.getX();
- int y = (int) event.getY();
- // Bias correction
- y = y + mYBiasCorrection;
- // Ignore short-distance movement event to get better performance.
- if (event.getAction() == MotionEvent.ACTION_MOVE) {
- if (Math.abs(x - mXLast) <= MOVE_TOLERANCE
- && Math.abs(y - mYLast) <= MOVE_TOLERANCE) {
- return true;
- }
- }
- mXLast = x;
- mYLast = y;
- if (!mPopupSkbShow) {
- // mGestureDetector的監聽器在輸入法服務PinyinIME中。
- if (mGestureDetector.onTouchEvent(event)) {
- resetKeyPress(0);
- mDiscardEvent = true;
- return true;
- }
- }
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- resetKeyPress(0);
- mWaitForTouchUp = true;
- mDiscardEvent = false;
- mSkv = null;
- mSoftKeyDown = null;
- mSkv = inKeyboardView(x, y, mSkvPosInContainer);
- if (null != mSkv) {
- mSoftKeyDown = mSkv.onKeyPress(x - mSkvPosInContainer[0], y
- - mSkvPosInContainer[1], mLongPressTimer, false);
- }
- break;
- case MotionEvent.ACTION_MOVE:
- if (x < 0 || x >= getWidth() || y < 0 || y >= getHeight()) {
- break;
- }
- if (mDiscardEvent) {
- resetKeyPress(0);
- break;
- }
- if (mPopupSkbShow && mPopupSkbNoResponse) {
- break;
- }
- SoftKeyboardView skv = inKeyboardView(x, y, mSkvPosInContainer);
- if (null != skv) {
- if (skv != mSkv) {
- mSkv = skv;
- mSoftKeyDown = mSkv.onKeyPress(x - mSkvPosInContainer[0], y
- - mSkvPosInContainer[1], mLongPressTimer, true);
- } else if (null != skv) {
- if (null != mSkv) {
- mSoftKeyDown = mSkv.onKeyMove(
- x - mSkvPosInContainer[0], y
- - mSkvPosInContainer[1]);
- if (null == mSoftKeyDown) {
- mDiscardEvent = true;
- }
- }
- }
- }
- break;
- case MotionEvent.ACTION_UP:
- if (mDiscardEvent) {
- resetKeyPress(0);
- break;
- }
- mWaitForTouchUp = false;
- // The view which got the {@link MotionEvent#ACTION_DOWN} event is
- // always used to handle this event.
- if (null != mSkv) {
- mSkv.onKeyRelease(x - mSkvPosInContainer[0], y
- - mSkvPosInContainer[1]);
- }
- if (!mPopupSkbShow || !mPopupSkbNoResponse) {
- responseKeyEvent(mSoftKeyDown);
- }
- if (mSkv == mPopupSkbView && !mPopupSkbNoResponse) {
- dismissPopupSkb();
- }
- mPopupSkbNoResponse = false;
- break;
- case MotionEvent.ACTION_CANCEL:
- break;
- }
- if (null == mSkv) {
- return false;
- }
- return true;
- }
九、Settings
- /**
- * Class used to maintain settings.
- */
- public class Settings {
上面所說,爲設置類,單例、主要設置如下:震動、聲音、預報
- public static void writeBack() {
- Editor editor = mSharedPref.edit();
- editor.putBoolean(ANDPY_CONFS_VIBRATE_KEY, mVibrate);
- editor.putBoolean(ANDPY_CONFS_KEYSOUND_KEY, mKeySound);
- editor.putBoolean(ANDPY_CONFS_PREDICTION_KEY, mPrediction);
- editor.commit();
- }
十、SettingsActivity
- /**
- * Setting activity of Pinyin IME.
- */
- public class SettingsActivity
上面意思設置的Acitivty、一般項目中會有改動,可以放入專門的設置應用中,這個設置信息量大,可以不用單獨爲輸入法搞個設置Activity
十一、BalloonHint
- /**
- * Subclass of PopupWindow used as the feedback when user presses on a soft key
- * or a candidate.
- */
- public class BalloonHint extends PopupWindow {
從上面註釋來看主要是:用戶按下一個軟鍵或候選人時冒出的氣泡
十二、HandWriteView
- public void TouchEvent(MotionEvent event) {
- float x = event.getX(0);
- float y = event.getY(0);
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- touch_start(x, y);
- break;
- case MotionEvent.ACTION_MOVE:
- touch_move(x, y);
- break;
- case MotionEvent.ACTION_UP:
- mhandler.postDelayed(Recognition, timer);
- break;
- }
- }
- mCanvas.drawLine(mX, mY, x, y, mPaint);
十三、HandWriteDecoder
- public char[] RecognitionResult(short p[],int len){
- native char[] getResult(short point[],int len)
- }
輸入法手寫識別的結果返回處理類,主要調用了返回結果的native方法,傳進去xy的座標值數組,返回字符串的char數組
十四、KeyMapDream
- /**
- * Class used to map the symbols on Dream's hardware keyboard to corresponding
- * Chinese full-width symbols.
- */
- public class KeyMapDream {
上面類的說明:硬件鍵盤上的符號映射到相應的中國全角符號
十五、Environment
- /**
- * Global environment configurations for showing soft keyboard and candidate
- * view. All original dimension values are defined in float, and the real size
- * is calculated from the float values of and screen size. In this way, this
- * input method can work even when screen size is changed.
- * 該類保存佈局的一些尺寸。比如:屏幕的寬度、屏幕的高度
- * 、按鍵的高度、候選詞區域的高度、按鍵氣泡寬度比按鍵寬度大的差值、按鍵氣泡高度比按鍵高度大的差值、正常按鍵中文本的大小
- * 、功能按鍵中文本的大小、正常按鍵氣泡中文本的大小、功能按鍵氣泡中文本的大小。
- */
- public class Environment {
- /**
- * The key height for portrait mode. It is relative to the screen height.
- * 豎屏按鍵高度,值是相對於屏幕高度。
- */
- private static final float KEY_HEIGHT_RATIO_PORTRAIT = 0.105f;
- /**
- * The key height for landscape mode. It is relative to the screen height.
- * 橫屏按鍵高度,值是相對於屏幕高度。
- */
- private static final float KEY_HEIGHT_RATIO_LANDSCAPE = 0.147f;
- /**
- * The height of the candidates area for portrait mode. It is relative to
- * screen height. 豎屏候選詞區域的高度,值是相對於屏幕高度。
- */
- private static final float CANDIDATES_AREA_HEIGHT_RATIO_PORTRAIT = 0.084f;
- /**
- * The height of the candidates area for portrait mode. It is relative to
- * screen height. 橫屏候選詞區域高度,值是相對於屏幕高度。
- */
- private static final float CANDIDATES_AREA_HEIGHT_RATIO_LANDSCAPE = 0.125f;
- /**
- * How much should the balloon width be larger than width of the real key.
- * It is relative to the smaller one of screen width and height.
- * 猜測:點擊軟鍵盤按鈕時彈出來的氣泡大於按鍵的寬度的差值,值是相對於屏幕高度和寬度較小的那一個。
- */
- private static final float KEY_BALLOON_WIDTH_PLUS_RATIO = 0.08f;
- /**
- * How much should the balloon height be larger than that of the real key.
- * It is relative to the smaller one of screen width and height.
- * 猜測:點擊軟鍵盤按鈕時彈出來的氣泡大於按鍵的高度的差值,值是相對於屏幕高度和寬度較小的那一個。
- */
- private static final float KEY_BALLOON_HEIGHT_PLUS_RATIO = 0.07f;
- /**
- * The text size for normal keys. It is relative to the smaller one of
- * screen width and height. 正常按鍵的文本的大小,值是相對於屏幕高度和寬度較小的那一個。
- */
- private static final float NORMAL_KEY_TEXT_SIZE_RATIO = 0.075f;
- /**
- * The text size for function keys. It is relative to the smaller one of
- * screen width and height. 功能按鍵的文本的大小,值是相對於屏幕高度和寬度較小的那一個。
- */
- private static final float FUNCTION_KEY_TEXT_SIZE_RATIO = 0.055f;
- /**
- * The text size balloons of normal keys. It is relative to the smaller one
- * of screen width and height. 正常按鍵彈出的氣泡的文本的大小,值是相對於屏幕高度和寬度較小的那一個。
- */
- private static final float NORMAL_BALLOON_TEXT_SIZE_RATIO = 0.14f;
- /**
- * The text size balloons of function keys. It is relative to the smaller
- * one of screen width and height. 功能按鍵彈出的氣泡的文本的大小,值是相對於屏幕高度和寬度較小的那一個。
- */
- private static final float FUNCTION_BALLOON_TEXT_SIZE_RATIO = 0.085f;
- /**
- * The configurations are managed in a singleton. 該類的實例,該類採用設計模式的單例模式。
- */
- private static Environment mInstance;
- private int mScreenWidth; // 屏幕的寬度
- private int mScreenHeight; // 屏幕的高度
- private int mKeyHeight; // 按鍵的高度
- private int mCandidatesAreaHeight; // 候選詞區域的高度
- private int mKeyBalloonWidthPlus; // 按鍵氣泡寬度比按鍵寬度大的差值
- private int mKeyBalloonHeightPlus; // 按鍵氣泡高度比按鍵高度大的差值
- private int mNormalKeyTextSize; // 正常按鍵中文本的大小
- private int mFunctionKeyTextSize; // 功能按鍵中文本的大小
- private int mNormalBalloonTextSize; // 正常按鍵氣泡中文本的大小
- private int mFunctionBalloonTextSize; // 功能按鍵氣泡中文本的大小
環境設置
十六、EnglishInputProcessor
- /**
- * Class to handle English input.
- */
- public class EnglishInputProcessor {
英文輸入發處理器
十七、HandWriteDecoder
- public class HandWriteDecoder {
- final static String TAG = "HandWriteDecoder";
- private int mMode = 0;
- public void setMode(int mode){
- mMode = mode;
- }
- public char[] RecognitionResult(short p[],int len,AssetManager ass){
- char[] a = null;
- a = getResult(p,len,mMode,ass,"aiwrite.dat");
- return a;
- }
- public char[] associate(char p[],int len){
- char[] a = null;
- a = getAssociate(p,len);
- return a;
- }
- static {
- System.loadLibrary("jni_pateoHandWrite");
- }
- native char[] getResult(short point[],int len,int mode,AssetManager ass,String path);
- native char[] getAssociate(char point[],int len);
- }
手寫結果管理類,主要把手寫的xy座標點放入p數組,然後返回相應的識別字結果
十八、HandWriteView
- public class HandWriteView extends View{
- private final static String TAG = "HandwriteView";
- private final int WIDTH = 800;
- private final int CANVAS_HEIGHT = 440;
- private final int POINT_NUMBER = 500;
- private Bitmap mBitmap;
- private Canvas mCanvas;
- private Paint mBitmapPaint;
- private Rect mDirtyRect;
- private Paint mPaint;
- private short[] mpoint = new short[502];
- private int length = 0;
- private boolean isMove = false;
- private long timer = 500;
- private int move_count = 0;
- private PateoIME mService;
- private Handler mhandler = new Handler();
- private Runnable Recognition = new Runnable(){
- public void run() {
- // TODO Auto-generated method stub
- if(length == 0){
- mService.setPoints(mpoint,length);
- }else{
- mpoint[length]=-1;
- mpoint[length+1]=-1;
- length=length+2;
- mService.setPoints(mpoint,length);
- length = 0;
- isMove = false;
- }
- clear();
- }
- };
- public HandWriteView(Context c) {
- super(c);
- mService = (PateoIME)c;
- mBitmap = Bitmap.createBitmap(WIDTH, CANVAS_HEIGHT, Bitmap.Config.ARGB_8888);
- mCanvas = new Canvas(mBitmap);
- mBitmapPaint = new Paint(Paint.DITHER_FLAG);
- mDirtyRect = new Rect(0,0,WIDTH,POINT_NUMBER);
- mPaint = new Paint();
- mPaint.setAntiAlias(true);
- mPaint.setDither(true);
- mPaint.setColor(0xFFFF0000);
- mPaint.setStyle(Paint.Style.STROKE);
- mPaint.setStrokeJoin(Paint.Join.ROUND);
- mPaint.setStrokeCap(Paint.Cap.ROUND);
- mPaint.setStrokeWidth(6);
- isMove = false;
- }
- public void setPaintColor(int color){
- if(color == 0){
- color = 0xFFFF0000;
- }
- mPaint.setColor(color);
- }
- public void setStrokeWidth(int size){
- if(size == 0){
- mPaint.setStrokeWidth(8);
- }else if(size == 2){
- mPaint.setStrokeWidth(4);
- }else if(size == 3){
- mPaint.setStrokeWidth(2);
- }else{
- mPaint.setStrokeWidth(6);
- }
- }
- public void setTimer(long time){
- if(time == 0){
- time = 500;
- }
- timer = time;
- }
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- super.onSizeChanged(w, h, oldw, oldh);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- canvas.drawColor(0x00FFFFFF);
- canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
- }
- private float mX, mY;
- private void touch_start(float x, float y) {
- mX = x;
- mY = y;
- }
- private void touch_move(float x, float y) {
- mCanvas.drawLine(mX, mY, x, y, mPaint);
- mX = x;
- mY = y;
- if(move_count%2 == 0)
- {
- invalidate(mDirtyRect);
- }
- }
- public void clear(){
- mBitmap = Bitmap.createBitmap(WIDTH, CANVAS_HEIGHT, Bitmap.Config.ARGB_8888);
- mCanvas = new Canvas(mBitmap);
- invalidate();
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- return false;
- }
- public void TouchEvent(MotionEvent event) {
- float x = event.getX(0);
- float y = event.getY(0);
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- move_count = move_count + 1;
- mhandler.removeCallbacks(Recognition);
- addPoints((short)x,(short)y);
- touch_start(x, y);
- break;
- case MotionEvent.ACTION_MOVE:
- move_count = move_count + 1;
- isMove = true;
- mhandler.removeCallbacks(Recognition);
- addPoints((short)x,(short)y);
- touch_move(x, y);
- break;
- case MotionEvent.ACTION_UP:
- move_count = 0;
- invalidate(mDirtyRect);
- if(isMove == true){
- addPoints((short)x,(short)y);
- }else{
- length = 0;
- }
- mhandler.postDelayed(Recognition, timer);
- break;
- }
- }
- public void TouchEvent(MotionEvent event,float x,float y) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- move_count = move_count + 1;
- mhandler.removeCallbacks(Recognition);
- addPoints((short)x,(short)y);
- touch_start(x, y);
- break;
- case MotionEvent.ACTION_MOVE:
- move_count = move_count + 1;
- isMove = true;
- mhandler.removeCallbacks(Recognition);
- addPoints((short)x,(short)y);
- touch_move(x, y);
- break;
- case MotionEvent.ACTION_UP:
- move_count = 0;
- invalidate(mDirtyRect);
- if(isMove == true){
- addPoints((short)x,(short)y);
- }else{
- length = 0;
- }
- mhandler.postDelayed(Recognition, timer);
- break;
- }
- }
- private void addPoints(short x, short y){
- if(x<0 || y<0){
- return;
- }
- if(length < POINT_NUMBER - 2){
- mpoint[length]=x;
- mpoint[length+1]=y;
- length=length+2;
- }
- }
- }
手寫view,主要通過TouchEvent來獲取xy座標值,來畫筆畫到畫布,其次把xy座標值輸入到Service.setPoints進行最終調用HandWriteDecoder的char[] RecognitionResult(short p[],int len返回識別結果
十九、Log
- public class Log {
- public static boolean mDebug = true;
- public static void d(String tag,String str)
- {
- if(mDebug)
- {
- android.util.Log.d("PateoInputMethod", "[ " + tag + " ] : " + str);
- }
- }
- public static void i(String tag,String str)
- {
- if(mDebug)
- {
- android.util.Log.i("PateoInputMethod", "[ " + tag + " ] : " + str);
- }
- }
- public static void e(String tag,String str)
- {
- if(mDebug)
- {
- android.util.Log.e("PateoInputMethod", "[ " + tag + " ] : " + str);
- }
- }
- public static void w(String tag,String str,Exception e)
- {
- if(mDebug)
- {
- android.util.Log.w("PateoInputMethod", "[ " + tag + " ] : " + str,e);
- }
- }
- }
二十、CandidateViewListener
- /**
- * Interface to notify the input method when the user clicks a candidate or
- * makes a direction-gesture on candidate view.
- */
- /**
- * 候選詞視圖監聽器接口
- *
- * @ClassName CandidateViewListener
- * @author keanbin
- */
- public interface CandidateViewListener {
- /**
- * 選擇了候選詞的處理函數
- *
- * @param choiceId
- */
- public void onClickChoice(int choiceId);
- /**
- * 向左滑動的手勢處理函數
- */
- public void onToLeftGesture();
- /**
- * 向右滑動的手勢處理函數
- */
- public void onToRightGesture();
- /**
- * 向上滑動的手勢處理函數
- */
- public void onToTopGesture();
- /**
- * 向下滑動的手勢處理函數
- */
- public void onToBottomGesture();
- }
二十一、CandidateView
- /**
- * View to show candidate list. There two candidate view instances which are
- * used to show animation when user navigates between pages.
- */
- public class CandidateView extends View {
主要爲候選詞界面
二十二、CandidatesContainer
- public class CandidatesContainer extends RelativeLayout implements
- OnTouchListener, AnimationListener, ArrowUpdater {
候選詞集裝箱,候選詞的管理,管理翻頁動畫、及其箭頭灰畫等。
二十三、InputModeSwitcher
- /**
- * Switcher used to switching input mode between Chinese, English, symbol,etc.
- */
- public class InputModeSwitcher {
切換輸入法界面,同時提供當前是什麼輸入法的判斷接口,其中該類中涉及到一個有關type的處理方法如下:
- public int requestInputWithSkb(EditorInfo editorInfo) {
比如定製的收音機輸入法界面,應用可以通過type來設置,就可以直接指定顯示收音機收入法
- public class PateoIME extends InputMethodService {
輸入法服務,涉及到如下一些信息:
- // 綁定詞庫解碼遠程服務PinyinDecoderService
- startPinyinDecoderService();
- /**
- * 按鍵處理函數
- *
- */
- private boolean processKey(KeyEvent event, boolean realAction) {
- // keyCode can be from both hard key or soft key.
- /**
- * 功能鍵處理函數
- */
- private boolean processFunctionKeys(int keyCode, boolean realAction) {
等等view控制等等,比如控制候選view,下面爲手寫的初始化,大致的看下:
- mHandWriteView = new HandWriteView(this);
- mHandWriteWinow = new PopupWindow(mHandWriteView, 800,440);
- mHandWriteWinow.setBackgroundDrawable(new ColorDrawable(0));
- mHandWriteWinow.setTouchable(true);
- mHandWriteWinow.setOutsideTouchable(false);
- mHandWriteWinow.setClippingEnabled(false);
- mHandWriteWinow.setTouchInterceptor(new OnTouchListener() {
- ublic boolean onTouch(View v, MotionEvent event) {
- boolean result = false;
- int num = event.getPointerCount();
- for(int i=0;i<num;i++){
- if(i == 0)
- {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- mHandWriteView.TouchEvent(event);
- break;
- case MotionEvent.ACTION_MOVE:
- mHandWriteView.TouchEvent(event);
- break;
- case MotionEvent.ACTION_UP:
- mHandWriteView.TouchEvent(event);
- break;
- }
- }
- }
- return result;
- });
- setCandidatesViewShown(true);
符號界面xml,如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <keyboard
- skb_template="@xml/skb_template1"
- skb_cache_flag="true"
- width="10%p"
- height="23.5%p"
- key_type="0"
- repeat="false"
- balloon="true">
- <row start_pos_x="1.25%p" start_pos_y="2%p" width="9.75%p">
- <keys splitter="|" labels="<|>|#|_|£|$|€|¥|§|¤"/>
- </row>
- <row start_pos_x="1.25%p" width="9.75%p">
- <keys splitter=" " labels="¿ ¡ | [ ] { } ^ ~ `"/>
- </row>
- <row start_pos_x="1.25%p" width="9.75%p">
- <key code="-5" width="9.75%p" label="2/2" key_type="7"/>
- <keys splitter="|" labels="±|×|÷|·|%|°|©"/>
- <key id="3"/>
- </row>
- <row start_pos_x="1.25%p" key_type="1">
- <key id="10"/>
- <key code="-2" width="9.75%p" repeat="true" key_type="7" icon="@drawable/icon_chinese"
- icon_popup="@drawable/icon_chinese">
- <toggle_state state_id="@string/toggle_en_sym" code="-2" icon="@drawable/icon_english"
- icon_popup="@drawable/icon_english"/>
- <toggle_state state_id="@string/toggle_hw_sym" code="-2" icon="@drawable/icon_hw"
- icon_popup="@drawable/icon_hw"/>
- </key>
- <key label="," width="9.75%p" icon="@drawable/comma_full_icon"
- icon_popup="@drawable/comma_full_icon" key_type="0">
- </key>
- <key code="62" key_type="5" width="29.25%p"/>
- <key label="." width="9.75%p" key_type="0" icon="@drawable/period_icon"
- icon_popup="@drawable/period_icon">
- </key>
- <key id="8"/>
- <key id="1"/>
- </row>
- </keyboard>
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Copyright (C) 2009 The Android Open Source Project
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- -->
- <skb_template
- skb_bg="@drawable/skb_bg1"
- key_xmargin="0.125%p"
- key_ymargin="1.2%p"
- balloon_bg="@drawable/key_balloon_bg"
- popup_bg="@drawable/miniskb_bg"
- color="@color/balloon_color"
- color_highlight="@color/label_color"
- color_balloon="@color/balloon_color">
- <!-- Normal key -->
- <key_type
- id="0"
- bg="@drawable/normal_key_bg1"
- hlbg="@drawable/normal_key_hl_bg"
- color_highlight="@color/label_color_hl0"/>
- <!-- Function key -->
- <key_type
- id="1"
- bg="@drawable/normal_key_bg"
- hlbg="@drawable/normal_key_hl_bg"/>
- <!-- Light key, light is off -->
- <key_type
- id="2"
- bg="@drawable/light_key_bg"
- hlbg="@drawable/light_key_hl_bg"/>
- <!-- Light key, light is on -->
- <key_type
- id="3"
- bg="@drawable/light_key_up_bg"
- hlbg="@drawable/light_key_up_hl_bg"/>
- <!-- key without background-->
- <key_type
- id="4"/>
- <!-- Key with normal background but on-key high-light-->
- <key_type
- id="5"
- bg="@drawable/normal_key_bg1"
- hlbg="@drawable/normal_key_hl_bg"
- color_highlight="@color/label_color_hl0"/>
- <key_type
- id="6"
- bg="@drawable/handinput_bg"
- hlbg="@drawable/handinput_hl_bg"
- color_highlight="@color/label_color_hl0"/>
- <!-- Function key -->
- <key_type
- id="7"
- bg="@drawable/normal_key_bg"
- hlbg="@drawable/normal_key_hl_bg"/>
- <!-- Function key -->
- <key_type
- id="8"
- bg="@drawable/normal_key_bg1"
- hlbg="@drawable/normal_key_hl_bg"/>
- <!-- Normal key -->
- <key_type
- id="9"
- color="@color/label_color"
- bg="@drawable/normal_key_bg1"
- hlbg="@drawable/normal_key_hl_bg"
- color_highlight="@color/label_color_hl0"/>
- <key_type
- id="10"
- bg="@drawable/normal_key_bg1"
- hlbg="@drawable/normal_key_bg1"/>
- <!-- Default icons for enter key -->
- <key_icon code="66" icon="@drawable/enter_icon"
- icon_popup="@drawable/enter_popup_icon"/>
- <!-- Default icons for space key -->
- <key_icon code="62" icon="@drawable/space_icon"
- icon_popup="@drawable/space_icon"/>
- <!-- Default icons for delete key -->
- <key_icon code="67" icon="@drawable/delete_icon"
- icon_popup="@drawable/delete_icon"/>
- <!-- Default key definition -->
- <!-- Enter key for QWERTY-like keyboards.-->
- <key id="1" start_pos_x="79.125%p" start_pos_y="72.5%p"
- width="19.60%p" height="23.5%p" code="66" key_type="7" balloon="false">
- <toggle_state state_id="@string/toggle_enter_go" label="@string/go" code="66"/>
- <toggle_state state_id="@string/toggle_enter_search" label="@string/search" code="66"/>
- <toggle_state state_id="@string/toggle_enter_send" label="@string/sent" code="66"/>
- <toggle_state state_id="@string/toggle_enter_next" label="@string/next" code="66"/>
- <toggle_state state_id="@string/toggle_enter_done" label="@string/completion" code="66"/>
- </key>
- <!-- Enter key for phone keyboard.-->
- <key id="2" start_pos_x="73.125%p" start_pos_y="75%p"
- width="19.60%p" height="22%p" code="66" key_type="0" balloon="false">
- <toggle_state state_id="@string/toggle_enter_go" label="@string/go" code="66"/>
- <toggle_state state_id="@string/toggle_enter_search" label="@string/search" code="66"/>
- <toggle_state state_id="@string/toggle_enter_send" label="@string/sent" code="66"/>
- <toggle_state state_id="@string/toggle_enter_next" label="@string/next" code="66"/>
- <toggle_state state_id="@string/toggle_enter_done" label="@string/completion" code="66"/>
- </key>
- <!-- Delete key.-->
- <key id="3" start_pos_x="79.125%p" start_pos_y="49%p" balloon="false"
- width="19.60%p" height="23.5%p" code="67" key_type="7"
- repeat="true"/>
- <key id="4" code="-3" start_pos_x="11.00%p" start_pos_y="72.5%p" balloon="false"
- width="9.8%p" height="23.5%p" key_type="7" label="\?123" icon="@drawable/symbol_icon"/>
- <!-- Language-switching key. -->
- <key id="6" start_pos_x="20.75%p" start_pos_y="72.5%p" balloon="false"
- width="9.75%p" height="23.5%p" code="-2" key_type="7" repeat="true"
- icon="@drawable/chinese_input_icon" icon_popup="@drawable/chinese_input_icon">
- <toggle_state state_id="@string/toggle_en_lower" code="-2" icon="@drawable/english_input_icon" icon_popup="@drawable/english_input_icon"/>
- <toggle_state state_id="@string/toggle_en_upper" code="-2" icon="@drawable/english_input_icon" icon_popup="@drawable/english_input_icon"/>
- <toggle_state state_id="@string/toggle_en_first" code="-2" icon="@drawable/english_input_icon" icon_popup="@drawable/english_input_icon"/>
- </key>
- <!-- Period key(English mode). -->
- <key id="7" start_pos_x="50.50%p" start_pos_y="72.5%p"
- width="9.75%p" height="23.5%p" label="." key_type="0"
- icon="@drawable/period_icon" icon_popup="@drawable/period_icon"/>
- <!-- clear input switch. -->
- <key id="8" start_pos_x="69.375%p" start_pos_y="72.5%p" balloon="false"
- width="9.75%p" height="23.5%p" label="" code="-7" key_type="7"
- icon="@drawable/clearbg" />
- <!-- hand input switch. -->
- <key id="9" start_pos_x="60.125%p" start_pos_y="72.5%p" balloon="false"
- width="9.75%p" height="23.5%p" label="@string/hand_write" code="-8" key_type="7"/>
- <!-- voice input switch. -->
- <key id="10" start_pos_x="1.25%p" start_pos_y="72.5%p" balloon="false"
- width="9.75%p" height="23.5%p" code="-10" label="" key_type="7"
- repeat="true" icon="@drawable/icon_speech"/>
- <key id="11" start_pos_x="77.475%p" start_pos_y="34.33%p" balloon="false"
- width="10.5%p" height="32.33%p" code="-10" label="" key_type="7"
- repeat="true" icon="@drawable/icon_speech"/>
- <key id="12" start_pos_x="87.9%p" start_pos_y="34.33%p" balloon="false"
- width="10.5%p" height="32.33%p" label="" code="-7" key_type="7"
- icon="@drawable/clearbg" />
- <key id="13" start_pos_x="77.475%p" start_pos_y="66.66%p" balloon="false"
- width="21.0%p" height="32.33%p" label="@string/completion" code="66" key_type="7"/>
- <key id="14" start_pos_x="58.475%p" start_pos_y="50%p" balloon="false"
- width="18.975%p" height="49%p" label="," icon="@drawable/comma_full_icon" icon_popup="@drawable/comma_full_icon"/>
- <key id="15" start_pos_x="58.975%p" start_pos_y="50%p"
- width="18.975%p" height="49%p" label="." icon="@drawable/period_icon" icon_popup="@drawable/period_icon"/>
- <key id="16" start_pos_x="77.475%p" start_pos_y="66.66%p" width="21.0%p" height="32.33%p" label="@string/completion" code="66" balloon="false" keytype="7" >
- <toggle_state state_id="@string/disable_done" code="-100"
- label="@string/completion" key_type="7" balloon="false"/>
- </key>
- <key id="17" start_pos_x="87.9%p" start_pos_y="34.33%p" balloon="false"
- width="10.50%p" height="32.33%p" label="" code="-7" key_type="7" icon="@drawable/clearbg1"/>
- <key id="18" start_pos_x="77.475%p" start_pos_y="34.33%p" balloon="false"
- width="10.50%p" height="32.33%p" label="" code="-11" key_type="7" icon="@drawable/icon_speech"/>
- <!--handwrite input switch-->
- <key id="20" start_pos_x="79.20%p" start_pos_y="2%p" width="19.5%p" height="48%p" code="67" key_type="7" balloon="false" repeat="true"/>
- <key id="21" start_pos_x="11.00%p" start_pos_y="52%p" width="9.8%p" height="48%p" code="-3" key_type="7" label="\?123" icon="@drawable/symbol_icon" balloon="false"/>
- <key id="22" start_pos_x="69.375%p" start_pos_y="52%p" width="9.75%p" height="48%p" code="-7" key_type="7" icon="@drawable/clearbg" balloon="false"/>
- <key id="23" start_pos_x="79.20%p" start_pos_y="52%p" width="19.5%p" height="48%p" code="66" key_type="7" balloon="false">
- <toggle_state state_id="@string/toggle_enter_go" label="@string/go" code="66"/>
- <toggle_state state_id="@string/toggle_enter_search" label="@string/search" code="66"/>
- <toggle_state state_id="@string/toggle_enter_send" label="@string/sent" code="66"/>
- <toggle_state state_id="@string/toggle_enter_next" label="@string/next" code="66"/>
- <toggle_state state_id="@string/toggle_enter_done" label="@string/completion" code="66"/>
- </key>
- <key id="24" start_pos_x="1.25%p" start_pos_y="52%p" width="9.75%p" height="48%p" code="-10" key_type="7" label="" icon="@drawable/icon_speech" balloon="false"/>
- <key id="30" start_pos_x="62.15%p" start_pos_y="34.6%p" balloon="false"
- width="15.245%p" height="32.33%p" icon="@drawable/numstar" label="*" key_type="9"/>
- <key id="31" start_pos_x="62.15%p" start_pos_y="66.6%p" balloon="false"
- width="15.245%p" height="32.33%p" icon="@drawable/numpound" label="\#" key_type="9"/>
- </skb_template>
相應的顯示效果,下面是UE設計圖,不爲實際顯示效果
上面爲基本的概率說明