Android中的绘画动画 SurfaceView

来源:互联网 发布:电子口岸数据退税 编辑:程序博客网 时间:2024/05/04 03:26

转自: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;}}


2、布局文件 main.xml 的内容如下:


<?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;@Overrideprotected 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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString text = Morse.morseEncoder(et01.getText().toString());et02.setText(text);}});//单击发送信号按钮,把莫尔斯码用灯信号的方式发送出去bt02.setOnClickListener(new OnClickListener() {@Overridepublic 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);//创建SurfaceViewLightView 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{@Overridepublic void run() {// TODO Auto-generated method stubwhile(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}}}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {// TODO Auto-generated method stubloop = false;}@Overridepublic void surfaceCreated(SurfaceHolder holder) {// TODO Auto-generated method stub//Surface被创建的时候执行new Thread(new LightThread()).start();}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {// TODO Auto-generated method stub}});loop = true;}}}


4、运行结果




原创粉丝点击