生成虚拟键盘的DIALOG方法:

package org.socketclient.keyboard;

import org.socketclient.R;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class KeyBoardDialog{

    public Context context;

    public VirtualKeyboard keybord;
    
    public MessageHandler messageHandler;
    
    public KeyBoardDialog(Context context,MessageHandler handler){
        this.context = context;
        this.messageHandler = handler;
    }

    public AlertDialog CreateDialog(){
        AlertDialog localAlertDialog = new AlertDialog.Builder(context).create();
        localAlertDialog.show();
        localAlertDialog.setContentView(R.layout.virtual_keyboard_layout);
        localAlertDialog.setCanceledOnTouchOutside(false);
        keybord = (VirtualKeyboard) localAlertDialog.findViewById(R.id.virtualKeyboard1);
        keybord.setOnTouchListener(listener);
        return localAlertDialog;
    }
    //当前坐标点位置
    int cx = 0,cy = 0;
    final OnTouchListener listener = new OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            // TODO Auto-generated method stub
            cx = (int) event.getX();
            cy = (int) event.getY();
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                keybord.postInvalidate(cx, cy, 0, 0);
                if(keybord.currentModel != null){
                    Message message = new Message();
                    message.what = 1001;
                    message.obj = keybord.currentModel.code;
                    messageHandler.handler.sendMessage(message);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                break;
            }
            return false;
        }
    };
}

消息相应类:

package org.socketclient.keyboard;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;

public abstract class MessageHandler {

    public Handler handler;
    
    public MessageHandler(){
        createHandler();
    }
    
    @SuppressLint("HandlerLeak")
    public void createHandler(){
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                // TODO Auto-generated method stub
                if(msg.what == 1001){
                    onTouchMessage(String.valueOf((Integer)msg.obj));
                }
                super.handleMessage(msg);
            }
        };
    }
    
    public abstract void onTouchMessage(String message);
}




自定义虚拟键盘控件

package org.socketclient.keyboard;

import java.util.ArrayList;
import org.socketclient.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

@SuppressLint("DrawAllocation")
public class VirtualKeyboard extends View {

    public int width;

    public int height;

    public int keyWidth;

    public int keyHeight;

    public final int rows = 6;

    public final int cols = 10;

    public final Rect brect = new Rect(0,0,100,100);

    public Paint paint;

    public boolean isfirst = true;
    //代表当前的按钮所代表的按键
    public int[][] keyText = {
            {0x11,0x12,0x20,0x0D,0x28,0x26,0x25,0x27},
            {0x10,0x4C,0x5A,0x58,0x43,0x56,0x42,0x4E,0x4D,0x08},
            {0x1B,0x41,0x53,0x44,0x46,0x47,0x48,0x4A,0x4B,0x2E},
            {0x51,0x57,0x45,0x52,0x54,0x59,0x55,0x49,0x4F,0x50},
            {0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30}};
    //当前按钮所代表的图标
    public int[][] keyBitmapId = {
            {R.drawable.ctrl,R.drawable.alt,R.drawable.space,R.drawable.enter,R.drawable.down,R.drawable.up,R.drawable.left,R.drawable.right},
            {R.drawable.shift,R.drawable.l,R.drawable.z,R.drawable.x,R.drawable.c,R.drawable.v,R.drawable.b,R.drawable.n,R.drawable.m,R.drawable.bspace},
            {R.drawable.esc,R.drawable.a,R.drawable.s,R.drawable.d,R.drawable.f,R.drawable.g,R.drawable.h,R.drawable.j,R.drawable.k,R.drawable.delete},
            {R.drawable.q,R.drawable.w,R.drawable.e,R.drawable.r,R.drawable.t,R.drawable.y,R.drawable.u,R.drawable.i,R.drawable.o,R.drawable.p},
            {R.drawable.n1,R.drawable.n2,R.drawable.n3,R.drawable.n4,R.drawable.n5,R.drawable.n6,R.drawable.n7,R.drawable.n8,R.drawable.n9,R.drawable.n0}};
    //被按下按钮所代表图标
    public int[][] keyBitmapId2 = {
            {R.drawable.ctrl1,R.drawable.alt1,R.drawable.space1,R.drawable.enter1,R.drawable.down1,R.drawable.up1,R.drawable.left1,R.drawable.right1},
            {R.drawable.shift1,R.drawable.l1,R.drawable.z1,R.drawable.x1,R.drawable.c1,R.drawable.v1,R.drawable.b1,R.drawable.nn1,R.drawable.m1,R.drawable.bspace1},
            {R.drawable.esc1,R.drawable.a1,R.drawable.s1,R.drawable.d1,R.drawable.f1,R.drawable.g1,R.drawable.h1,R.drawable.j1,R.drawable.k1,R.drawable.delete1},
            {R.drawable.q1,R.drawable.w1,R.drawable.e1,R.drawable.r1,R.drawable.t1,R.drawable.y1,R.drawable.u1,R.drawable.i1,R.drawable.o1,R.drawable.p1},
            {R.drawable.n11,R.drawable.n22,R.drawable.n33,R.drawable.n44,R.drawable.n55,R.drawable.n66,R.drawable.n77,R.drawable.n88,R.drawable.n99,R.drawable.n00}};

    //按钮集合
    public ArrayList keylist = null;
    //当前被按下按钮
    public KeyModel currentModel = null;

    public VirtualKeyboard(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        init();
    }

    public VirtualKeyboard(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        init();
    }

    public VirtualKeyboard(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        init();
    }
    //初始化按钮
    public void init(){
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE); // 红色圆形

        if(keylist != null)
            keylist.clear();
        keylist = new ArrayList();
        //获取布局大小
        int width = getWidth();
        int height = getHeight();
        //计算单个按钮大小
        keyWidth = width / rows;
        keyHeight = height / cols;
        for(int i=0; i             for(int j=0; j                 KeyModel model;
                if(i == 0){
                    if(j < 2){
                        model = new KeyModel(i * keyWidth,
                                j * keyHeight,(i + 1) * keyWidth,(j+1) * keyHeight);
                    }else if(j == 2){
                        model = new KeyModel(i * keyWidth,
                                j * keyHeight,(i + 1) * keyWidth,(j + 2) * keyHeight);
                    }else if(j == 3){
                        model = new KeyModel(i * keyWidth,
                                (j + 1) * keyHeight,(i + 1) * keyWidth,(j + 3) * keyHeight);
                    }else{
                        model = new KeyModel(i * keyWidth,
                                (j + 2) * keyHeight,(i + 1) * keyWidth,(j + 3) * keyHeight);
                    }
                }else{
                    model = new KeyModel(i * keyWidth,
                            j * keyHeight,(i + 1) * keyWidth,(j+1) * keyHeight);
                }
                //显示编号
                model.index = i;
                model.jndex = j;
                model.bitmap = BitmapFactory.decodeResource(getResources(), keyBitmapId[i][j]);
                model.code = keyText[i][j];
                keylist.add(model);
            }
        }
    }
    //绘制键盘
    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        //绘制  
        if(isfirst == true){
            init();
        }
        isfirst = false;
        //  设置背景色
        paint.setColor(Color.BLACK);
        for(int i=0; i             KeyModel model = keylist.get(i);
            RectF rectf = model.textf;
            Matrix matrix = new Matrix();  
            // 缩放原图  
            matrix.postScale(1f, 1f);  
            // 向左旋转45度,参数为正则向右旋转  
            matrix.postRotate(90);  
            //bmp.getWidth(), 500分别表示重绘后的位图宽高  
            Bitmap dstbmp = Bitmap.createBitmap(model.bitmap, 0, 0, 100, 100,  
                    matrix, true);
            canvas.drawBitmap(dstbmp, brect, rectf, paint);
        }

        if(currentModel != null){
            Matrix matrix = new Matrix();  
            // 缩放原图  
            matrix.postScale(1f, 1f);  
            // 向左旋转45度,参数为正则向右旋转  
            matrix.postRotate(90);  
            //bmp.getWidth(), 500分别表示重绘后的位图宽高  
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), keyBitmapId2[currentModel.index][currentModel.jndex]);
            Bitmap dstbmp = Bitmap.createBitmap(bitmap, 0, 0, 100, 100,  
                    matrix, true);
            canvas.drawBitmap(dstbmp, brect, currentModel.textf, paint);
        }
        System.out.println("绘制图像了吗?");
    }
    //重新绘制键盘
    @Override
    public void postInvalidate(int left, int top, int right, int bottom) {
        // TODO Auto-generated method stub
        currentModel = null;
        GetClickKey(left,top);
        if(currentModel != null){
            super.postInvalidate((int)currentModel.textf.left,(int)currentModel.textf.top,
                    (int)currentModel.textf.right,(int)currentModel.textf.bottom);
        }
        else{
            super.postInvalidate(0,0,0,0);
        }
    }
    //计算当前本按下键盘位置
    public void GetClickKey(int left, int top){
        int index = left / keyWidth;
        int jndex = top / keyHeight;
        //第一行特殊处理数据
        if(index == 0){
            if(jndex == 3)
                jndex = 2;
            if(jndex == 4 || jndex == 5)
                jndex = 3;
            if(jndex > 5)
                jndex = jndex - 2;
        }
        for(int i=0; i             KeyModel model = keylist.get(i);
            if(model.index == index && model.jndex == jndex){
                currentModel = model;
            }
        }
    }
    //按键类
    class KeyModel{
        public KeyModel(int x,int y,int x1,int y1){
            textf = new RectF(x,y,x1,y1);
        }
        //左下角位置
        public RectF textf;
        //显示图片
        public Bitmap bitmap;
        //代表的字符串
        public int code;
        //显示编号
        public int index;
        public int jndex;
    }

}

调用方法:

KeyBoardDialog dialog = new KeyBoardDialog(BaseActivity1.this , new MessageHandler() {
            @Override
            public void onTouchMessage(String message) {
                // TODO Auto-generated method stub
                sendMessage("b,"+message,"Contrl",-1);
            }
        });
        dialog.CreateDialog();





更多相关文章

  1. AndroidStudio 3.4.1 DrawerLayout 侧滑
  2. Android使用SurfaceView画图
  3. Android(安卓)自定义确认提示框,选择确认
  4. TextView源码解析-----绘制过程
  5. android 强制关闭软键盘
  6. Android悬浮贴边按钮实现(含动画效果)
  7. 自定义开关控件(ToggleView)继承View实现
  8. miui卸载爆炸效果
  9. 隐藏软键盘方法

随机推荐

  1. Android 中文api,Debug签名证书过期(Expiry
  2. Service与Android系统实现(1)-- 应用程序里
  3. Ogre3d 之使用 Android(安卓)NativeActiv
  4. Android命令行截屏screencap
  5. Android设置中“强行停止”详解
  6. 《Android/OPhone开发完全讲义》连载(7):使
  7. 第一个Android工程HelloAndroid
  8. Android NDK 开发(笔记一)
  9. Android:自定义控件你应该知道的这些事_Ty
  10. android chrome iframe设置src属性无法启