转自:http://android.yaohuiji.com/archives/818
SurfaceView是View的一个子类,它提供了一种比普通View组件绘制速度更快的绘图方式,在游戏、视频等要求高帧速和高流畅度的场合,使用SurfaceView成了一种很好的选择。
1、创建一个莫尔斯码的工具类 Morse.java
public class Morse {
public static Map<String , String> morseMap = new HashMap<String, String>();
static{
morseMap.put("a", ".-");
morseMap.put("b", "-...");
morseMap.put("c", "-.-.");
morseMap.put("d", "-..");
morseMap.put("e", ".");
morseMap.put("f", "..-.");
morseMap.put("g", "--.");
morseMap.put("h", "....");
morseMap.put("i", "..");
morseMap.put("j", ".---");
morseMap.put("k", "-.-");
morseMap.put("l", ".-..");
morseMap.put("m", "--");
morseMap.put("n", "-.");
morseMap.put("o", "---");
morseMap.put("p", ".--.");
morseMap.put("q", "--.-");
morseMap.put("r", ".-.");
morseMap.put("s", "...");
morseMap.put("t", "-");
morseMap.put("u", "..-");
morseMap.put("v", "...-");
morseMap.put("w", ".--");
morseMap.put("x", "-..-");
morseMap.put("y", "-.--");
morseMap.put("z", "--..");
morseMap.put("0", "-----");
morseMap.put("1", ".----");
morseMap.put("2", "..---");
morseMap.put("3", "...--");
morseMap.put("4", "....-");
morseMap.put("5", ".....");
morseMap.put("6", "-....");
morseMap.put("7", "--...");
morseMap.put("8", "---..");
morseMap.put("9", "----.");
morseMap.put(".", ".-.-.-");
morseMap.put("-", "-....-");
morseMap.put(",", "--..--");
morseMap.put("?", "..--..");
morseMap.put("/", "-..-.");
morseMap.put(";", "-.-.-.");
morseMap.put("(", "-.--.");
morseMap.put(")", "-.--.-");
morseMap.put("@", ".--.-.");
morseMap.put("*", "...-.-");
morseMap.put("+", ".-.-.");
morseMap.put("%", ".-...");
morseMap.put("\\", "---...");
morseMap.put("\"", ".-..-.");
morseMap.put("'", ".----.");
morseMap.put("!", "-----.");
morseMap.put("$", "...-..-");
morseMap.put(" ", "/");
}
public static String morseEncoder(String input){
String output = "";
for(char c : input.toCharArray()){
output += morseMap.get(String.valueOf(c))+"";
}
return output;
}
}
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent" android:layout_width="match_parent"
android:orientation="vertical" android:id="@+id/layout01">
<TextView android:layout_height="wrap_content" android:layout_width="match_parent"
android:text="输入:"
/>
<EditText android:id="@+id/edittext01" android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<Button android:id="@+id/button01" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="转换"
/>
<TextView android:layout_height="wrap_content" android:layout_width="match_parent"
android:text="输出:"
/>
<EditText android:id="@+id/edittext02" android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<Button android:id="@+id/button02" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="发送信号"
/>
</LinearLayout>
3、MainActivity.java的内容如下
public class MainActivity extends Activity {
private LinearLayout layout;
//莫尔斯码数组变量
char[] chars;
//莫尔斯码数组计数变量
int count = 0;
//开关标志
boolean flag = false;
//循环标志
boolean loop = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义UI组件
final Button bt01 = (Button)findViewById(R.id.button01);
final Button bt02 = (Button)findViewById(R.id.button02);
final EditText et01 = (EditText)findViewById(R.id.edittext01);
final EditText et02 = (EditText)findViewById(R.id.edittext02);
layout = (LinearLayout)findViewById(R.id.layout01);
//单击转换为莫尔斯码
bt01.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String text = Morse.morseEncoder(et01.getText().toString());
et02.setText(text);
}
});
//单击发送信号按钮,把莫尔斯码用灯信号的方式发送出去
bt02.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//莫尔斯码文本框有内容就发送灯信号
if(et02.getText()!=null&&et02.getText().toString().length()>0){
//把莫尔斯码拆解为字符
chars = et02.getText().toString().toCharArray();
//计数
count = chars.length;
Log.d("debug", ""+count);
//创建SurfaceView
LightView light = new LightView(MainActivity.this);
layout.addView(light);
}
}
});
}
//信号灯
class LightView extends SurfaceView{
//声明SurfaceHolder对象
SurfaceHolder holder;
//构造方法
public LightView(Context context){
super(context);
//从SurfaceView获取Holder对象
holder = this.getHolder();
//addCallback对象
holder.addCallback(new SurfaceHolder.Callback() {
//创建SurfaceHolder.Callback匿名内部类
//在内部类的内部创建它所需要的线程内部类
class LightThread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
while(loop){
if(count>0){
String s = String.valueOf(chars[chars.length-count]);
//锁定canvas开始绘图
Canvas canvas = holder.lockCanvas(null);
Paint paint = new Paint();
paint.setAntiAlias(true);
//清屏幕
paint.setColor(Color.BLACK);
canvas.drawRect(0, 0, 240, 240, paint);
//标志位是真时关一下灯
if(flag){
sleep(2);
paint.setColor(Color.BLACK);
}
else{
//为假就亮灯
if(s.equalsIgnoreCase(".")){
sleep(2);
paint.setColor(Color.YELLOW);
}
else if(s.equalsIgnoreCase("-")){
sleep(8);
paint.setColor(Color.YELLOW);
}
else if(s.equalsIgnoreCase(" ")){
sleep(2);
paint.setColor(Color.BLACK);
}
else if(s.equalsIgnoreCase("/")){
sleep(2);
paint.setColor(Color.BLUE);
}
else {
//出问题就亮红灯
sleep(2);
paint.setColor(Color.RED);
}
count --;
}
//绘制灯光
canvas.drawCircle(125.0f, 100.0f, 80, paint);
//标志位开关
flag = !flag;
//释放canvas,绘图完毕
holder.unlockCanvasAndPost(canvas);
}
}
}
//休眠函数
public void sleep(int time){
try {
Thread.sleep(time*500);
} catch (Exception e) {
// TODO: handle exception
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
loop = false;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
//Surface被创建的时候执行
new Thread(new LightThread()).start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
});
loop = true;
}
}
}