Android 自定义车牌键盘

简介: Android 自定义车牌键盘

image.png

最近在做物流的项目,需要输入车牌号,传统的输入体验并不好,来回切换还要手打,便有了自定义键盘的想法。

其实这个车牌需求并不多见,物流、租车、停车相关的会常见一些,好在有前辈已经铺好路了

这个比较详细:https://blog.csdn.net/dgs960825/article/details/50344743


1.在res文件夹下新建xml文件夹,新建两个文件:number_or_letters.xml(数字和大写字母)、province_abbreviation.xml(省会、直辖市、特别行政区简称)。

number_or_letters.xml


<?xml version="1.0" encoding="UTF-8"?>
<Keyboard android:keyWidth="10%p" android:keyHeight="8%"
          android:horizontalGap="0.0px" android:verticalGap="0.0px"
          xmlns:android="http://schemas.android.com/apk/res/android">
    <Row android:verticalGap="1%p">
        <Key android:codes="49" android:keyLabel="1"
             android:horizontalGap="1%p" android:keyWidth="8%p"
             android:keyEdgeFlags="left"  />
        <Key android:codes="50" android:keyLabel="2"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="51" android:keyLabel="3"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="52" android:keyLabel="4"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="53" android:keyLabel="5"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="54" android:keyLabel="6"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="55" android:keyLabel="7"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="56" android:keyLabel="8"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="57" android:keyLabel="9"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="48" android:keyLabel="0"
             android:horizontalGap="2%p" android:keyWidth="8%p"
             android:keyEdgeFlags="right"/>
    </Row>
    <Row android:verticalGap="1%p">
        <Key android:codes="81" android:keyLabel="Q"
             android:horizontalGap="1%p" android:keyWidth="8%p"
             android:keyEdgeFlags="left" />
        <Key android:codes="87" android:keyLabel="W"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="69" android:keyLabel="E"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="82" android:keyLabel="R"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="84" android:keyLabel="T"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="89" android:keyLabel="Y"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="85" android:keyLabel="U"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="73" android:keyLabel="I"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="79" android:keyLabel="O"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="80" android:keyLabel="P"
             android:horizontalGap="2%p" android:keyWidth="8%p"
             android:keyEdgeFlags="right" />
    </Row>
    <Row android:verticalGap="1%p">
        <Key android:codes="65" android:keyLabel="A"
             android:horizontalGap="6%p" android:keyWidth="8%p"
             android:keyEdgeFlags="left"/>
        <Key android:codes="83" android:keyLabel="S"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="68" android:keyLabel="D"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="70"  android:keyLabel="F"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="71" android:keyLabel="G"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="72"  android:keyLabel="H"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="74" android:keyLabel="J"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="75" android:keyLabel="K"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="76"  android:keyLabel="L"
             android:horizontalGap="2%p" android:keyWidth="8%p"
             android:keyEdgeFlags="right" />
    </Row>
    <Row >
        <Key android:codes="-1"
             android:keyEdgeFlags="left" android:keyLabel="省份"
             android:horizontalGap="1%p" android:keyWidth="13%p"
             android:isModifier="true" android:isSticky="true"  />
        <Key android:codes="90" android:keyLabel="Z"
             android:horizontalGap="2%p" android:keyWidth="8%p"/>
        <Key android:codes="88" android:keyLabel="X"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="67" android:keyLabel="C"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="86" android:keyLabel="V"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="66" android:keyLabel="B"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="78" android:keyLabel="N"
             android:horizontalGap="2%p" android:keyWidth="8%p"/>
        <Key android:codes="77" android:keyLabel="M"
             android:horizontalGap="2%p" android:keyWidth="8%p"/>
        <Key  android:codes="-3"
              android:keyEdgeFlags="right" android:keyLabel="删除"
              android:horizontalGap="2%p" android:keyWidth="13%p"
              android:isRepeatable="false" />
    </Row>
</Keyboard>

province_abbreviation.xml


<?xml version="1.0" encoding="UTF-8"?>
<Keyboard
    android:keyWidth="10%p" android:keyHeight="8%p"
    android:horizontalGap="0.0px" android:verticalGap="0.0px"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Row android:verticalGap="1%p">
        <Key android:codes="20140" android:keyLabel="京"
             android:horizontalGap="1%p" android:keyWidth="8%p"
             android:keyEdgeFlags="left"  />
        <Key android:codes="27941" android:keyLabel="津"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="20864" android:keyLabel="冀"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="40065" android:keyLabel="鲁"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="26187" android:keyLabel="晋"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="33945" android:keyLabel="蒙"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="36797" android:keyLabel="辽"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="21513" android:keyLabel="吉"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="40657" android:keyLabel="黑"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="27818" android:keyLabel="沪"
             android:horizontalGap="2%p" android:keyWidth="8%p"
             android:keyEdgeFlags="right"/>
    </Row>
    <Row android:verticalGap="1%p">
        <Key android:codes="33487" android:keyLabel="苏"
             android:horizontalGap="1%p" android:keyWidth="8%p"
             android:keyEdgeFlags="left" />
        <Key android:codes="27993" android:keyLabel="浙"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="30358" android:keyLabel="皖"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="38397" android:keyLabel="闽"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="36195" android:keyLabel="赣"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="35947" android:keyLabel="豫"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="37122" android:keyLabel="鄂"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="28248" android:keyLabel="湘"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="31908" android:keyLabel="粤"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="26690" android:keyLabel="桂"
             android:horizontalGap="2%p" android:keyWidth="8%p"
             android:keyEdgeFlags="right" />
    </Row>
    <Row android:verticalGap="1%p">
        <Key android:codes="28189" android:keyLabel="渝"
             android:horizontalGap="11%p" android:keyWidth="8%p"
             android:keyEdgeFlags="left"/>
        <Key android:codes="24029" android:keyLabel="川"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="36149" android:keyLabel="贵"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="20113"  android:keyLabel="云"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="34255" android:keyLabel="藏"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="38485"  android:keyLabel="陕"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="29976" android:keyLabel="甘"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="38738"  android:keyLabel="青"
             android:horizontalGap="2%p" android:keyWidth="8%p"
             android:keyEdgeFlags="right" />
    </Row>
    <Row >
        <Key android:codes="-1"
             android:keyEdgeFlags="left" android:keyLabel="ABC"
             android:horizontalGap="4%p" android:keyWidth="15%p"
             android:isModifier="true" android:isSticky="true"  />
        <Key android:codes="29756" android:keyLabel="琼"
             android:horizontalGap="2%p" android:keyWidth="8%p"/>
        <Key android:codes="26032" android:keyLabel="新"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="23425" android:keyLabel="宁"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="28207" android:keyLabel="港"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="28595" android:keyLabel="澳"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="21488" android:keyLabel="台"
             android:horizontalGap="2%p" android:keyWidth="8%p"/>
        <Key  android:codes="-3"
              android:keyEdgeFlags="right" android:keyLabel="删除"
              android:horizontalGap="2%p" android:keyWidth="15%p"
              android:isRepeatable="false" />
    </Row>
</Keyboard>

2.新建键盘工具类KeyboardUtil(初始化、交互、显示和隐藏)

package yechaoa.com.platenumberkeyboard;


import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.text.InputType;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
 * Created by yechao on 2018/5/15/015.
 * Describe :
 */
public class KeyboardUtil {
    private Activity mActivity;
    private KeyboardView mKeyboardView;
    private EditText mEdit;
    /**
     * 省份简称键盘
     */
    private Keyboard provinceKeyboard;
    /**
     * 数字与大写字母键盘
     */
    private Keyboard numberKeyboard;
    KeyboardUtil(Activity activity, EditText edit) {
        mActivity = activity;
        mEdit = edit;
        provinceKeyboard = new Keyboard(activity, R.xml.province_abbreviation);
        numberKeyboard = new Keyboard(activity, R.xml.number_or_letters);
        mKeyboardView = (KeyboardView) activity.findViewById(R.id.keyboard_view);
        mKeyboardView.setKeyboard(provinceKeyboard);
        mKeyboardView.setEnabled(true);
        mKeyboardView.setPreviewEnabled(false);
        mKeyboardView.setOnKeyboardActionListener(listener);
    }
    private KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {
        @Override
        public void swipeUp() {
        }
        @Override
        public void swipeRight() {
        }
        @Override
        public void swipeLeft() {
        }
        @Override
        public void swipeDown() {
        }
        @Override
        public void onText(CharSequence text) {
        }
        @Override
        public void onRelease(int primaryCode) {
        }
        @Override
        public void onPress(int primaryCode) {
        }
        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            Editable editable = mEdit.getText();
            int start = mEdit.getSelectionStart();
            //判定是否是中文的正则表达式 [\\u4e00-\\u9fa5]判断一个中文 [\\u4e00-\\u9fa5]+多个中文
            String reg = "[\\u4e00-\\u9fa5]";
            if (primaryCode == -1) {// 省份简称与数字键盘切换
                if (mEdit.getText().toString().matches(reg)) {
                    changeKeyboard(true);
                }
            } else if (primaryCode == -3) {
                if (editable != null && editable.length() > 0) {
                    //没有输入内容时软键盘重置为省份简称软键盘
                    if (editable.length() == 1) {
                        changeKeyboard(false);
                    }
                    if (start > 0) {
                        editable.delete(start - 1, start);
                    }
                }
            } else {
                editable.insert(start, Character.toString((char) primaryCode));
                // 判断第一个字符是否是中文,是,则自动切换到数字软键盘
                if (mEdit.getText().toString().matches(reg)) {
                    changeKeyboard(true);
                }
            }
        }
    };
    /**
     * 指定切换软键盘 isNumber false表示要切换为省份简称软键盘 true表示要切换为数字软键盘
     */
    private void changeKeyboard(boolean isNumber) {
        if (isNumber) {
            mKeyboardView.setKeyboard(numberKeyboard);
        } else {
            mKeyboardView.setKeyboard(provinceKeyboard);
        }
    }
    /**
     * 软键盘展示状态
     */
    public boolean isShow() {
        return mKeyboardView.getVisibility() == View.VISIBLE;
    }
    /**
     * 软键盘展示
     */
    public void showKeyboard() {
        int visibility = mKeyboardView.getVisibility();
        if (visibility == View.GONE || visibility == View.INVISIBLE) {
            mKeyboardView.setVisibility(View.VISIBLE);
        }
    }
    /**
     * 软键盘隐藏
     */
    public void hideKeyboard() {
        int visibility = mKeyboardView.getVisibility();
        if (visibility == View.VISIBLE) {
            mKeyboardView.setVisibility(View.INVISIBLE);
        }
    }
    /**
     * 禁掉系统软键盘
     */
    public void hideSoftInputMethod() {
        mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        int currentVersion = android.os.Build.VERSION.SDK_INT;
        String methodName = null;
        if (currentVersion >= 16) {
            // 4.2
            methodName = "setShowSoftInputOnFocus";
        } else if (currentVersion >= 14) {
            // 4.0
            methodName = "setSoftInputShownOnFocus";
        }
        if (methodName == null) {
            mEdit.setInputType(InputType.TYPE_NULL);
        } else {
            Class<EditText> cls = EditText.class;
            Method setShowSoftInputOnFocus;
            try {
                setShowSoftInputOnFocus = cls.getMethod(methodName, boolean.class);
                setShowSoftInputOnFocus.setAccessible(true);
                setShowSoftInputOnFocus.invoke(mEdit, false);
            } catch (NoSuchMethodException e) {
                mEdit.setInputType(InputType.TYPE_NULL);
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
}

3.Activity引用

package yechaoa.com.platenumberkeyboard;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
    private KeyboardUtil keyboardUtil;
    private EditText mEditText;
    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEditText = findViewById(R.id.edit_text);
        mEditText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                if (keyboardUtil == null) {
                    keyboardUtil = new KeyboardUtil(MainActivity.this, mEditText);
                    keyboardUtil.hideSoftInputMethod();
                    keyboardUtil.showKeyboard();
                } else {
                    keyboardUtil.showKeyboard();
                }
                return false;
            }
        });
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                Log.i("字符变换后", "afterTextChanged");
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.i("字符变换前", s + "-" + start + "-" + count + "-" + after);
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i("字符变换中", s + "-" + "-" + start + "-" + before + "-" + count);
            }
        });
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (keyboardUtil.isShow()) {
                keyboardUtil.hideKeyboard();
            } else {
                finish();
            }
        }
        return false;
    }
}

4.相关属性

android:keyBackground=”@drawable/selector_key”

* 按键的背景颜色


android:shadowColor=”#FFFFFF”

android:shadowRadius=”0.0”

* 不加这两个属性 文字会出现模糊


android:keyTextColor=”#000”

* 字体颜色


android:keyTextSize=”18sp”

* 字体大小


android:keyIcon=”@drawable/ic_delete”

* 按键上的图标


android:codes=”20140”

* 输出的内容 对照ASCII表


android:keyLabel=”京”

* 按键上显示的内容


android:horizontalGap=”0px”

* 水平方向的间隙


android:verticalGap=”0px”

* 垂直反向的间隙


android:keyEdgeFlags=”right”

* 按键的对齐方式




github:https://github.com/yechaoa/PlateNumberKeyBoard

目录
相关文章
|
10月前
|
Android开发 UED 计算机视觉
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
本文介绍了一款受游戏“金铲铲之战”启发的Android自定义View——线条等待动画的实现过程。通过将布局分为10份,利用`onSizeChanged`测量最小长度,并借助画笔绘制动态线条,实现渐变伸缩效果。动画逻辑通过四个变量控制线条的增长与回退,最终形成流畅的等待动画。代码中详细展示了画笔初始化、线条绘制及动画更新的核心步骤,并提供完整源码供参考。此动画适用于加载场景,提升用户体验。
662 5
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
|
10月前
|
Android开发
Android自定义view之利用PathEffect实现动态效果
本文介绍如何在Android自定义View中利用`PathEffect`实现动态效果。通过改变偏移量,结合`PathEffect`的子类(如`CornerPathEffect`、`DashPathEffect`、`PathDashPathEffect`等)实现路径绘制的动态变化。文章详细解析了各子类的功能与参数,并通过案例代码展示了如何使用`ComposePathEffect`组合效果,以及通过修改偏移量实现动画。最终效果为一个菱形图案沿路径运动,源码附于文末供参考。
201 0
|
10月前
|
Android开发 开发者
Android自定义view之利用drawArc方法实现动态效果
本文介绍了如何通过Android自定义View实现动态效果,重点使用`drawArc`方法完成圆弧动画。首先通过`onSizeChanged`进行测量,初始化画笔属性,设置圆弧相关参数。核心思路是不断改变圆弧扫过角度`sweepAngle`,并调用`invalidate()`刷新View以实现动态旋转效果。最后附上完整代码与效果图,帮助开发者快速理解并实践这一动画实现方式。
245 0
|
10月前
|
Android开发 数据安全/隐私保护 开发者
Android自定义view之模仿登录界面文本输入框(华为云APP)
本文介绍了一款自定义输入框的实现,包含静态效果、hint值浮动动画及功能扩展。通过组合多个控件完成界面布局,使用TranslateAnimation与AlphaAnimation实现hint文字上下浮动效果,支持密码加密解密显示、去除键盘回车空格输入、光标定位等功能。代码基于Android平台,提供完整源码与attrs配置,方便复用与定制。希望对开发者有所帮助。
205 0
|
10月前
|
XML Java Android开发
Android自定义view之网易云推荐歌单界面
本文详细介绍了如何通过自定义View实现网易云音乐推荐歌单界面的效果。首先,作者自定义了一个圆角图片控件`MellowImageView`,用于绘制圆角矩形图片。接着,通过将布局放入`HorizontalScrollView`中,实现了左右滑动功能,并使用`ViewFlipper`添加图片切换动画效果。文章提供了完整的代码示例,包括XML布局、动画文件和Java代码,最终展示了实现效果。此教程适合想了解自定义View和动画效果的开发者。
443 65
Android自定义view之网易云推荐歌单界面
|
10月前
|
XML 前端开发 Android开发
一篇文章带你走近Android自定义view
这是一篇关于Android自定义View的全面教程,涵盖从基础到进阶的知识点。文章首先讲解了自定义View的必要性及简单实现(如通过三个构造函数解决焦点问题),接着深入探讨Canvas绘图、自定义属性设置、动画实现等内容。还提供了具体案例,如跑马灯、折线图、太极图等。此外,文章详细解析了View绘制流程(measure、layout、draw)和事件分发机制。最后延伸至SurfaceView、GLSurfaceView、SVG动画等高级主题,并附带GitHub案例供实践。适合希望深入理解Android自定义View的开发者学习参考。
822 84
|
10月前
|
前端开发 Android开发 UED
讲讲Android为自定义view提供的SurfaceView
本文详细介绍了Android中自定义View时使用SurfaceView的必要性和实现方式。首先分析了在复杂绘制逻辑和高频界面更新场景下,传统View可能引发卡顿的问题,进而引出SurfaceView作为解决方案。文章通过Android官方Demo展示了SurfaceView的基本用法,包括实现`SurfaceHolder.Callback2`接口、与Activity生命周期绑定、子线程中使用`lockCanvas()`和`unlockCanvasAndPost()`方法完成绘图操作。
295 3
|
10月前
|
Android开发 开发者
Android自定义view之围棋动画(化繁为简)
本文介绍了Android自定义View的动画实现,通过两个案例拓展动态效果。第一个案例基于`drawArc`方法实现单次动画,借助布尔值控制动画流程。第二个案例以围棋动画为例,从简单的小球直线运动到双向变速运动,最终实现循环动画效果。代码结构清晰,逻辑简明,展示了如何化繁为简实现复杂动画,帮助读者拓展动态效果设计思路。文末提供完整源码,适合初学者和进阶开发者学习参考。
186 0
Android自定义view之围棋动画(化繁为简)
|
10月前
|
Java Android开发 开发者
Android自定义view之围棋动画
本文详细介绍了在Android中自定义View实现围棋动画的过程。从测量宽高、绘制棋盘背景,到创建固定棋子及动态棋子,最后通过属性动画实现棋子的移动效果。文章还讲解了如何通过自定义属性调整棋子和棋盘的颜色及动画时长,并优化视觉效果,如添加渐变色让白子更明显。最终效果既可作为围棋动画展示,也可用作加载等待动画。代码完整,适合进阶开发者学习参考。
223 0
|
10月前
|
传感器 Android开发 开发者
Android自定义view之3D正方体
本文介绍了如何通过手势滑动操作实现3D正方体的旋转效果,基于Android自定义View中的GLSurfaceView。相较于使用传感器控制,本文改用事件分发机制(onTouchEvent)处理用户手势输入,调整3D正方体的角度。代码中详细展示了TouchSurfaceView的实现,包括触控逻辑、OpenGL ES绘制3D正方体的核心过程,以及生命周期管理。适合对Android 3D图形开发感兴趣的开发者学习参考。
196 0