手写板原理

本文探讨了手写板的实现,包括笔锋处理的两种方案、消隐策略、手写板大小计算以及PopupWindow的使用。在触摸事件处理中,通过dispatchTouchEvent和onTouchEvent与手写SDK交互,并在onDraw中进行Canvas绘制。GestureController、GestureCanvas、BrushesPlotter和SpotFilter等组件协同工作,完成手写轨迹的记录和绘制。
摘要由CSDN通过智能技术生成

笔锋有两种方案:自己画(计算复杂度高且实现效果差)、笔锋图片(存在重合区域)

消隐有两种方案:笔画结束时整体重画(过度绘制)、笔画结束时增加PorterDuff蒙层

计算手写板大小(全屏和半屏)、构造视图层次结构(Background、Margin)、扩展手写板大小:

/*--------------------------------SogouIME.java----------------------------------*/

Rect frame = new Rect();
mCandidateViewContainer.getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top; //Rect(0, 50, 720, 1280)

//left : 0.005, top : 0.0125, height : 0.83, width : 0.845
HandWritingRect hwRect = mKeyboardView.getKeyboard().getHandWritingRect();

int[] candidateViewContainerInWindow = new int[2]; //(0, 0)
mCandidateViewContainer.getLocationInWindow(candidateViewContainerInWindow);

Rect rect = new Rect();
if (isFullScreen) {
    rect.left = 0;
    rect.top = getHWWindowTop();
    rect.right = mKeyboardView.getWidth();
    rect.bottom = candidateViewContainerInWindow[1];
} else {
    rect.left = (int)(mKeyboardView.getWidth() * hwRect.left);
    rect.top = (int)(mKeyboardView.getKeyboard().getKeyboardRealHeight() * hwRect.top);
    rect.right = (rect.left + (int)(mKeyboardView.getKeyboard().getMinWidth() * hwRect.width));
    rect.bottom = rect.top + (int)(mKeyboardView.getKeyboard().getKeyboardRealHeight() * hwRect.height);
}

mHWGestureWindow = new HWGestureWindow(getApplicationContext(), mKeyboardView, !isFullScreen, rect, mode);
mHWGestureWindow.setGestureActionListener(mHWGestureListener);
mHWGestureWindow.showGestureWindow();

public int getHWWindowTop() {
    int keyboardHeight = mKeyboardView.getHeight(); //230
    int candidateHeight = mCandidateViewContainer.getHeight(); //88
    Rect frame = calculateTitleBarHeight(); //Rect(0, 50, 720, 1280)
    int totalHeight = keyboardHeight + candidateHeight - frame.height(); // totalHeight : -912
    return totalHeight;
}

/*--------------------------------HWGestureWindow.java----------------------------------*/

public HWGestureWindow(Context context, View parent, boolean withInKeyboard, Rect viewRect, int mode) {
    super(context);
    mContext = context;
    mParent = parent;
    mWithinKeyboard = withInKeyboard;
    mViewRect = viewRect;
    mMode = mode;
    setBackgroundDrawable(null);
    setClippingEnabled(false);
    mGesturePoints = new short[GESTURE_POINTS_NUM];
    initGestureView(mWithinKeyboard);
}

//View hierarchy for fullScreen :
//PopupWindow -- mRootView -- mHwGestureView
//View hierarchy for halfScreen :
//mKeyboardView -- mHandwritingView -- mHwBgLayout -- mModeTextView(下) & mHwGestureView(上)

public void initGestureView(boolean withInKeyboard) {
    mDensity = mContext.getResources().getDisplayMetrics().density;
    mMinPadding = (int)(2 * mDensity);
    mRootView = new RelativeLayout(mContext);
    mHWGestureView = new HandWriteView(mContext, withInKeyboard, mViewRect);
    if (!withInKeyboard) {
        mRootView.addView(mHWGestureView);
        setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.bg_color)));
    } else {
        LayoutInflater inflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mHandWritingView = inflater.inflate(R.layout.hw_half_layout, null);

        mModeTextView = (TextView) mHandWritingView.findViewById(R.id.hw_mode_tip);
        mTextStyle = KeyboardManager.getInstance(mContext).getTextStyle();
        mModeTextView.setTextColor(mTextStyle.color);
        String contentText = mContext.getString(R.string.hw_mode_overlap);
        mModeTextView.setText(contentText);

        mHWBgLayout = mHandWritingView.findViewById(R.id.hw_bg_layout);
        // 将mHWGestureView添加到mHWBgLayout中,并设置mHWBgLayout的背景
        mHWBg = mKeyboardView.getKeyboard().getHandWritingBG();
        mHWBgLayout.addView(mHWGestureView);
        mHWBgLayout.setBackgroundDrawable(mHWBg);
        // mHWGestureView相对mHWBgLayout的布局信息
        mBGPaddingRect = mKeyboardView.getKeyboard().getHandWr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

little-sparrow

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值