我想爲Android製作弧形屏幕鍵盤。我已經檢查了softkeyboard和其他各種屏幕鍵盤上的google代碼。我發現沒有任何東西涉及的鍵盤形狀不是矩形。理想情況下,我想創建一個鍵盤,其中包含分佈在屏幕兩側的兩個半圓上的按鍵(即,想象拿着平板電腦的兩側並能夠用拇指按下按鍵)。如何製作適用於Android的弧形屏幕鍵盤
在我所研究的代碼中,屏幕鍵盤被創建爲視圖(通常延伸KeyboardView)並在屏幕底部顯示爲連續的橫條。作爲一個近似我的目標,我試圖修改我在google代碼(dotdash-keyboard-android)上找到的代碼,只在左下角繪製它的按鍵,並保持右下角透明。我已經能夠覆蓋onMeasure來影響視圖的尺寸(見下文),但這似乎只是改變了鍵的位置而不是容器的位置。換句話說,屏幕底部仍然有一個黑條。
//Located within KeyboardView
@Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
this.setMeasuredDimension(200, 200);
}
是我想要做甚至可能嗎?有更正確的術語嗎?我可以使用哪些項目作爲示例?
我也嘗試使用this.setLayoutParams設置視圖的尺寸 - 但這些調用似乎沒有效果。我也嘗試使用this.getParent來訪問父視圖(如果有的話),並改變它的尺寸,但這種方法不起作用(或者,我只是做錯了)。任何幫助將非常感激。
謝謝
更新:2012年12月21日 - 我想我需要重寫父類的OnDraw方法。展望here,它看起來像KeyboardView的OnDraw方法繪製在畫布等於用下面的代碼屏幕尺寸:
final int width = Math.max(1, getWidth());
final int height = Math.max(1, getHeight());
mBuffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBuffer);
我想我可以重寫的onDraw並繪製任何我想要的畫布上。
更新:2012年12月21日 - 我已覆蓋onDraw,它現在清楚,keyboardView是我設置它的尺寸(200x200)。使用Hierarchyview,我可以看到keyboardview在ID爲InputArea的框架佈局中。所以,填充整個寬度的水平條就是這個framelayout。但是,我沒有創造它 - 它從哪裏來,我如何改變它的尺寸?
更新:12/22/2012 - 經過更多測試後,鍵盤視圖的行爲(維度)似乎由調用它的活動部分決定。在瀏覽器中,我得到了我所描述的行爲:即使鍵盤的寬度小於寬度,瀏覽器窗口的高度也會縮小以適應容納鍵盤屏幕底部的橫條。屏幕。在日曆應用程序中,鍵盤大小顯示爲我設置的日曆(作爲左下角的正方形),日曆在其下方保持不變。所以,大多數使用這種方法的應用程序似乎無法達到我的目標。另一種方法可能是讓IME服務創建一個彈出式窗口或對話框。一個問題是popupwindows需要一個父視圖或錨點來連接,我不認爲有可能從IME服務中找到最頂層的視圖。也許我可以在當前的活動上創建一個透明的視圖,並將彈出窗口放在上面?
更新:12/23/2012 - 進展。我已經想出瞭如何從鍵盤輸入法顯示彈出窗口。下一步就是弄清楚如何讓彈出窗口有點圓。以下是我完成的源代碼的截圖。
來源。以下方法位於服務IME類中,並由服務的子視圖的onMeasure方法調用,以便在繪製鍵盤的同時打開彈出窗口。我已將鍵盤的尺寸設置爲1x1,因此不可見。日誌語句可以幫助我找出如何定位彈出窗口。
public void initiatePopupWindow()
{
try {
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
//display.getSize(p);
Log.i("dotdashkeyboard","initiatePopupWindow (from IME service)");
LayoutInflater inflater = (LayoutInflater) this.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layoutLeft = inflater.inflate(R.layout.popup_layout_left,null);
View layoutRight = inflater.inflate(R.layout.popup_layout_right, null);
// create a 300px width and 470px height PopupWindow
int popupHeight = 300;
int popupWidth = 200;
if (popUpLeft == null) popUpLeft = new PopupWindow(layoutLeft, popupWidth, popupHeight, false);
if (popUpRight == null) popUpRight = new PopupWindow(layoutRight, popupWidth, popupHeight, false);
int ypos = 0;
int xposRight = 0;
if (display.getRotation() == Surface.ROTATION_0) {
ypos = -(dm.heightPixels/2 + popupHeight/2);
xposRight = (dm.widthPixels - popupWidth);
Log.i("dotdashkeyboard","test rotation=normal");
} else if (display.getRotation() == Surface.ROTATION_90) {
ypos = -(dm.heightPixels/2 + popupHeight/2)/2;
xposRight = (dm.widthPixels - popupWidth)*2-popupWidth;
Log.i("dotdashkeyboard","test rotation=90-degrees");
} else {
Log.i("dotdashkeyboard","test rotation=unknown=" + display.getRotation());
}
popUpLeft.showAtLocation(inputView, Gravity.NO_GRAVITY, 0, ypos);
popUpRight.showAtLocation(inputView, Gravity.NO_GRAVITY, xposRight, ypos);
Log.i("dotdashkeyboard","test created popup at ypos="+ypos + " xposRight=" + xposRight);
Log.i("dotdashkeyboard","test screenWidth=" + dm.widthPixels + " screenHeight=" + dm.heightPixels);
Button cancelButton = (Button) layoutLeft.findViewById(R.id.popup_cancel_button);
//cancelButton.setOnClickListener(inputView.cancel_button_click_listener);
cancelButton.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
看看這個鏈接。 [使用曲線形狀](http://stackoverflow.com/a/10140513/793943)。 – Sush
有趣的問題 - 將遵循它的可能的答案....遺憾的是我沒有什麼可以添加來幫助你。 –