程序员表白程序,开放源码,不断更新

这是我为自己写的一款表白的程序,几经努力把它完成为一个完整的程序,这是我准备完成的程序之爱系列第一辑,希望能多做几辑吧。

源码地址:https://github.com/wuxia2001/mylove.git

APK地址:http://apk.hiapk.com/html/2014/01/2260344.html        

先上图吧

                            



一共三个主界面和一个设置界面,主界面都是用surfaceview画出来的。三个界面都是自己设计的,作为一个程序员,非美工,就不要对我的美术素养太苛刻了哈!

每个界面都有底层背景,加背景的方法:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1.  //外面加背景   
  2. l1.setBackgroundResource(R.drawable.q2);  
  3.   
  4. //Surfaceview做设置  
  5. //透明  
  6. setZOrderOnTop(true);  
  7. holder.setFormat(PixelFormat.TRANSPARENT);   

第一个界面,楚楚这两个字是用爱心一点点渐现出来的,在这里,画出字有两种方法,第一种,是我用awt得到的字的坐标,然后保存在文件里,再从文件里读出坐标,从而画出字。

生成坐标的代码如下,这部分代码只能在电脑上运行,不能移植到手机端,所以这种方法只能用来显示事先设计好的字,但这种方法写出来的字效果去很好。代码如下

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 要写的字,我这里只试过单字,要保存的文件名,字体,调整X和Y位置,宽和高 
  3.      * @param zi 
  4.      * @param filename 
  5.      * @param font 
  6.      * @param outx 
  7.      * @param outy 
  8.      * @param w 
  9.      * @param h 
  10.      */  
  11.     public static void zi(String zi, String filename, Font font,  
  12.             int outx,int outy,int w,int h){  
  13.         try  
  14.         {  
  15.             //张 黑体,30号字。w40 h 29 array_zhang  
  16.             //晚 黑体 30   
  17.             //I 宋体 30号。 w12 h24 array_I  
  18.             //U 宋体 30 号粗 w17 h24 array_U  
  19.             //Font font = new Font("黑体", Font.PLAIN, 30);  
  20.               
  21.             AffineTransform at = new AffineTransform();  
  22.             FontRenderContext frc = new FontRenderContext(at, truetrue);  
  23.             GlyphVector gv = font.createGlyphVector(frc, zi); // 要显示的文字  
  24.             Shape shape = gv.getOutline(outx,outy);  
  25.             int weith = w;  
  26.             int height = h;  
  27.             boolean[][] view = new boolean[weith][height];  
  28.             for (int i = 0; i < weith; i++)  
  29.             {  
  30.                 for (int j = 0; j < height; j++)  
  31.                 {  
  32.                     if (shape.contains(i, j))  
  33.                     {  
  34.                         view[i][j] = true;  
  35.                     } else  
  36.                     {  
  37.                         view[i][j] = false;  
  38.                     }  
  39.                     //if(i == 7 && j>=3 && j<=6) view[i][j] = true;  
  40.                     //if(i == 6 && j == 8) view[i][j] = true;  
  41.                 }  
  42.             }  
  43.             for (int j = 0; j < height; j++)  
  44.             {  
  45.                 for (int i = 0; i < weith; i++)  
  46.                 {  
  47.                     if (view[i][j])  
  48.                     {  
  49.                         System.out.print("@");// 替换成你喜欢的图案  
  50.                     } else  
  51.                     {  
  52.                         System.out.print(" ");  
  53.                     }  
  54.                 }  
  55.                 System.out.println();  
  56.             }  
  57.               
  58.             File file = new File(filename);  //存放数组数据的文件               
  59.               FileWriter out = new FileWriter(file);  //文件写入流              
  60.               //将数组中的数据写入到文件中。每行各数据之间TAB间隔  
  61.               for(int j=0;j<height;j++){  
  62.                for(int i=0;i<weith;i++){  
  63.                    if(view[i][j])     
  64.                        out.write(1+"b");  
  65.                    else out.write(0+"b");  
  66.                }  
  67.                out.write("n");  
  68.               }  
  69.               out.close();  
  70.         } catch (Exception e)  
  71.         {  
  72.             e.printStackTrace();  
  73.         }  
  74.     }  

直接进行调用 zi("楚","array_chu.txt",font,-1,17,20,20);就可以得坐标。

在手机端读出来并写出来的代码如下:


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. // 花 type 1为花,2为爱心  
  2.     /** 
  3.      * 开始x,开始Y坐标,字的宽,高,文件名字,放大倍数,type为图片种类 
  4.      * @param stx 
  5.      * @param sty 
  6.      * @param w 
  7.      * @param h 
  8.      * @param filename 
  9.      * @param beishu 
  10.      * @param type 
  11.      */  
  12.     public void show_I(int stx, int sty, int w, int h, String filename,  
  13.             int beishu, int type) {  
  14.         int startx = stx, starty = sty;  
  15.         try {  
  16.             int weith = w;  
  17.             int height = h;  
  18.             boolean[][] arr = new boolean[weith][height]; // 插入的数组  
  19.             String file = filename;  
  20.             InputStream ins = Util.init().getAssetsInputStream(mContext, file);  
  21.             BufferedReader in = new BufferedReader(new InputStreamReader(ins)); //  
  22.             String line; // 一行数据  
  23.             int row = 0;  
  24.             // 逐行读取,并将每个数组放入到数组中  
  25.             StringBuilder sb = new StringBuilder();  
  26.             while ((line = in.readLine()) != null) {  
  27.                 sb.append(line);  
  28.             }  
  29.             System.out.println(sb.toString());  
  30.             in.close();  
  31.             String all = sb.toString();  
  32.             String[] all_a = all.split("n");  
  33.             //先得到坐标  
  34.             for (int i = 0; i < all_a.length; i++) {  
  35.                 String[] all_b = all_a[i].split("b");  
  36.                 System.out.println();  
  37.                 for (int j = 0; j < all_b.length; j++) {  
  38.                     if (all_b[j].equals("0")) {  
  39.                         arr[j][i] = false;  
  40.                     } else  
  41.                         arr[j][i] = true;  
  42.                 }  
  43.             }  
  44.             int bei = beishu;  
  45.             int dis = 25;  
  46.             int old_num = -1;  
  47.             for (int j = 0; j < height && !isallstop; j++) {  
  48.                 for (int i = 0; i <= weith && !isallstop; i++) {  
  49.                     //一定要sleep,要不然其他线程画不了东西  
  50.                     Thread.sleep(25);  
  51.                     Random rm = new Random();  
  52.                     Bitmap bitmap = null;  
  53.                     int num = 0;  
  54.                     if (type == 1) {  
  55.                         num = rm.nextInt(heart_all.length - 1);  
  56.                         bitmap = bitmapcache  
  57.                                 .getBitmap(heart_all[num], mContext);  
  58.                     } else if (type == 2) {  
  59.                         bitmap = bitmapcache.getBitmap(R.drawable.love,  
  60.                                 mContext);  
  61.                     }  
  62.                     int bw = bitmap.getWidth();  
  63.                     int bh = bitmap.getHeight();  
  64.                     if (i >= weith  && !isallstop) {  
  65.                       
  66.   
  67.                     synchronized (holder) {  
  68.                         Canvas c = null;  
  69.                         try {  
  70.                             float xx = (float) i;  
  71.                             float yy = (float) j;  
  72.                             //不要轻易去锁定整个屏幕  
  73.                             c = holder.lockCanvas(new Rect(startx + (int) xx  
  74.                                     * bei, starty + (int) yy * bei,  
  75.                                     startx + (int) xx * bei + dis, starty  
  76.                                             + (int) yy * bei + dis));  
  77.   
  78.                             // c = holder.lockCanvas();  
  79.                             Paint p = new Paint(); // 创建画笔  
  80.                             p.setColor(Color.RED);  
  81.                             //下面这段是保证双缓冲能都画上东西,从而不会闪烁  
  82.                             if (i > 0 && !isallstop) {  
  83.                                 int xx_b = i - 1;  
  84.                                 int yy_b = j;  
  85.                                 if (arr[xx_b][yy_b]) {  
  86.                                     if (old_num != -1) {  
  87.                                         if (type == 1)  
  88.                                             c.drawBitmap(bitmapcache.getBitmap(  
  89.                                                     heart_all[old_num],  
  90.                                                     mContext), startx + xx_b  
  91.                                                     * bei, starty + yy_b * bei,  
  92.                                                     p);  
  93.                                         else if (type == 2) {  
  94.                                             c.drawBitmap(bitmapcache.getBitmap(  
  95.                                                     R.drawable.love, mContext),  
  96.                                                     startx + xx_b * bei, starty  
  97.                                                             + yy_b * bei, p);  
  98.                                         }  
  99.                                     }  
  100.                                 }  
  101.                             }  
  102.                             if (arr[i][j] && !isallstop) {  
  103.                                 c.drawBitmap(bitmap, startx + xx * bei, starty  
  104.                                         + yy * bei, p);  
  105.                             }  
  106.                             old_num = num;  
  107.                         } catch (Exception e) {  
  108.                             e.printStackTrace();  
  109.                         } finally {  
  110.                             try{  
  111.                                 if (c != null){  
  112.                                 <span style="white-space:pre">  </span>holder.unlockCanvasAndPost(c);// 结束锁定画图,并提交改变。  
  113.                                 }  
  114.                             }catch(Exception e){  
  115.                                 e.printStackTrace();  
  116.                             }  
  117.                         }  
  118.                     }                     
  119.                 }  
  120.             }  
  121.   
  122.         } catch (Exception e) {  
  123.             e.printStackTrace();  
  124.         }  
  125.   
  126.     }  

第二种方法就是点阵字符集,我程序中,如果设置的字不是内定的那寥寥几个,就会用点阵字符,点阵字符分16*1624*2432*3248*48,我代码里有前三种,每种字体又分为黑体宋体楷体等几种,在经过每一种尝试后,我最终选择的24HZKS,代码里每一种都有代码,大家也可以对每种进行测试,如果只要适应一款分辨率的话,会有更好的适配字体,我这是为了适应大多数的分辨率选的这种。24*24的代码如下,

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class Font24 {  
  2.     private Context context;  
  3.     public Font24(Context context){  
  4.         this.context = context;  
  5.     }  
  6.       
  7.     private final static int[] mask = {1286432168421};  
  8.     private final static String ENCODE = "GB2312";  
  9.     private final static String ZK16 = "Hzk24s";  
  10.       
  11.     private boolean[][] arr;  
  12.     int all_16_32 = 24;  
  13.     int all_2_4 = 3;  
  14.     int all_32_128 = 72;  
  15.     public boolean[][] drawString(String str){  
  16.           byte[] data = null;  
  17.           int[] code = null;  
  18.           int byteCount;//到点阵数据的第几个字节了  
  19.           int lCount;//控制列  
  20.             
  21.           arr = new boolean[all_16_32][all_16_32]; // 插入的数组  
  22.           //g.setColor(color);  
  23.           for(int i = 0;i < str.length();i ++){  
  24.               if(str.charAt(i) < 0x80){//非中文  
  25.                   //g.drawString(str.substring(i,i+1),x+(i<<4),y,0);  
  26.                   continue;  
  27.               }  
  28.               code = getByteCode(str.substring(i,i+1));  
  29.               data = read(code[0],code[1]);  
  30.               byteCount = 0;  
  31.               for(int line = 0;line < all_16_32;line ++){  
  32.                   lCount = 0;  
  33.                   for(int k = 0;k < all_2_4;k ++){  
  34.                       for(int j = 0;j < 8;j ++){  
  35.                          // if((data[byteCount]&mask[j])==mask[j]){  
  36.                            if (((data[byteCount] >> (7 - j)) & 0x1) == 1) {  
  37.                                arr[line][lCount] = true;  
  38.                               System.out.print("@");                            
  39.                           }else{  
  40.                               System.out.print(" ");  
  41.                               arr[line][lCount] = false;  
  42.                           }  
  43.                           lCount++;  
  44.                       }  
  45.                       byteCount ++;  
  46.                   }  
  47.                   System.out.println();  
  48.               }  
  49.           }  
  50.           return arr;  
  51.     }  
  52.            
  53.     /** 
  54.      *读取文字信息 
  55.      *@param areaCode 区码 
  56.      *@param posCode 位码 
  57.      *@return 文字数据 
  58.      */  
  59.     protected byte[] read(int areaCode,int posCode){  
  60.         byte[] data = null;  
  61.         try{  
  62.             int area = areaCode-0xa0;//获得真实区码  
  63.             int pos  = posCode-0xa0;//获得真实位码  
  64.               
  65.             //InputStream in = getClass().getResourceAsStream(ZK32);  
  66.             InputStream in = Util.init().getAssetsInputStream(context, ZK16);  
  67.             long offset = all_32_128*((area-1)*94+pos-1);  
  68.             in.skip(offset);  
  69.             data = new byte[all_32_128];  
  70.             in.read(data,0,all_32_128);  
  71.             in.close();  
  72.         }catch(Exception ex){  
  73.         }  
  74.         return data;  
  75.     }  
  76.       
  77.     /** 
  78.      *获得文字的区位码 
  79.      *@param str 
  80.      *@return int[2] 
  81.      */  
  82.     protected int[] getByteCode(String str){  
  83.         int[] byteCode = new int[2];  
  84.         try{  
  85.             byte[] data = str.getBytes(ENCODE);  
  86.             byteCode[0] = data[0] < 0?256+data[0]:data[0];  
  87.             byteCode[1] = data[1] < 0?256+data[1]:data[1];  
  88.         }catch(Exception ex){  
  89.             ex.printStackTrace();  
  90.         }  
  91.         return byteCode;  
  92.     }  
  93.       
  94. }  

这段代码可以得到字体的坐标,1632的就是

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. int all_16_32 = 24;  
  2. int all_2_4 = 3;  
  3. int all_32_128 = 72;  

三个参数不一样而己。

用坐标系画字大体差不多,代码如下

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 16/24/32,要写的字符,开始的x,y,倍数,花或爱心 
  3.      * @param font_kind 
  4.      * @param s 
  5.      * @param stx 
  6.      * @param sty 
  7.      * @param beishu 
  8.      * @param type 
  9.      */  
  10.     public void show_font16_24_32(int font_kind,String s, int stx, int sty,   
  11.             int beishu, int type) {  
  12.         boolean[][] arr = null;  
  13.         int weith = 16;  
  14.         int height = 16;  
  15.         if(font_kind == 16){  
  16.             weith = 16;  
  17.             height = 16;  
  18.             arr = new boolean[weith][height];  
  19.             Font16 font16 = new Font16(mContext);  
  20.             arr = font16.drawString(s);  
  21.         }else if(font_kind == 24){  
  22.             weith = 24;  
  23.             height = 24;  
  24.             arr = new boolean[weith][height];  
  25.             Font24 font24 = new Font24(mContext);  
  26.             arr = font24.drawString(s);  
  27.         }else {  
  28.             weith = 32;  
  29.             height = 32;  
  30.             arr = new boolean[weith][height];  
  31.             Font32 font32 = new Font32(mContext);  
  32.             arr = font32.drawString(s);  
  33.         }  
  34.                   
  35.         int startx = stx, starty = sty;       
  36.         int bei = beishu;  
  37.         int old_num = -1;  
  38.         int lCount;// 控制列  
  39.         for (int i = 0; i < weith && !isallstop; i++) {  
  40.             for (int j = 0; j < height && !isallstop; j++) {  
  41.                 try {  
  42.                     Thread.sleep(25);  
  43.                 } catch (InterruptedException e1) {  
  44.                     // TODO 自动生成的 catch 块  
  45.                     e1.printStackTrace();  
  46.                 }  
  47.                 float xx = (float) j;  
  48.                 float yy = (float) i;  
  49.                 if (arr[i][j] && !isallstop) {  
  50.   
  51.                     Random rm = new Random();  
  52.                     Bitmap bitmap = null;  
  53.                     int num = 0;  
  54.                     if (type == 1) {  
  55.                         num = rm.nextInt(heart_all.length - 1);  
  56.                         bitmap = bitmapcache  
  57.                                 .getBitmap(heart_all[num], mContext);  
  58.                     } else if (type == 2) {  
  59.                         bitmap = bitmapcache.getBitmap(R.drawable.love,  
  60.                                 mContext);  
  61.                     }  
  62.                     int bw = bitmap.getWidth();  
  63.                     int bh = bitmap.getHeight();  
  64.                     synchronized (holder) {  
  65.                         Canvas c = null;  
  66.                         try {  
  67.   
  68.                             // 不要轻易去锁定整个屏幕  
  69.                             c = holder.lockCanvas(new Rect(startx + (int) xx  
  70.                                     * bei, starty + (int) yy * bei, startx  
  71.                                     + (int) xx * bei + bw, starty + (int) yy  
  72.                                     * bei + bh));  
  73.   
  74.                             // c = holder.lockCanvas();  
  75.                             Paint p = new Paint(); // 创建画笔  
  76.                             p.setColor(Color.RED);  
  77.                             // 下面这段是保证双缓冲能都画上东西,从而不会闪烁  
  78.   
  79.                             c.drawBitmap(bitmap, startx + xx * bei, starty + yy  
  80.                                     * bei, p);  
  81.   
  82.                             old_num = num;  
  83.                         } catch (Exception e) {  
  84.                             e.printStackTrace();  
  85.                         } finally {  
  86.                             try{  
  87.                                 if (c != null){  
  88.                                     holder.unlockCanvasAndPost(c);// 结束锁定画图,并提交改变。  
  89.                                 }  
  90.                             }catch(Exception e){  
  91.                                 e.printStackTrace();  
  92.                             }  
  93.                         }  
  94.                     }  
  95.                 }  

画出爱心,最主要得到爱心的坐标就可以了,具体可参照:http://love.hackerzhou.me/ 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. private void run_hua_heart() {  
  2.             // TODO 自动生成的方法存根  
  3.             int startx = sw / 2 - 16, starty = sh / 2 - 68;  
  4.             int maxh = 100;    
  5.             int y_dao = starty;  
  6.             double begin = 10// 起始位置  
  7.             Random rm = new Random();  
  8.             int old_num = -1;  
  9.             float old_xx = 0, old_yy = 0;  
  10.             for (int i = 0; i < maxh  && !isallstop; i++) {  
  11.                 try {  
  12.                     Thread.sleep(80);  
  13.                 } catch (InterruptedException e1) {  
  14.                     // TODO 自动生成的 catch 块  
  15.                     e1.printStackTrace();  
  16.                 }  
  17.   
  18.                 int hua_num = rm.nextInt(18);  
  19.                 Bitmap bit = bitmapcache  
  20.                         .getBitmap(heart_all[hua_num], mContext);  
  21.                 begin = begin + 0.2;  //密度  
  22.                 double b = begin / Math.PI;  
  23.                 double a = 13.5 * (16 * Math.pow(Math.sin(b), 3));  //这里的13.5可以控制大小  
  24.                 double d = -13.5  
  25.                         * (13 * Math.cos(b) - 5 * Math.cos(2 * b) - 2  
  26.                                 * Math.cos(3 * b) - Math.cos(4 * b));  
  27.                 synchronized (holder) {  
  28.                     Canvas c = null;  
  29.                     try {  
  30.                         float xx = (float) a;  
  31.                         float yy = (float) d;  
  32.   
  33.                         c = holder.lockCanvas(new Rect(  
  34.                                 (int) (startx + xx - 40),  
  35.                                 (int) (starty + yy - 40),  
  36.                                 (int) (startx + xx + 40),  
  37.                                 (int) (starty + yy + 40)));  
  38.                         Paint p = new Paint(); // 创建画笔  
  39.                         p.setColor(Color.RED);  
  40.                         //画上一个,要不然会闪烁  
  41.                         if (old_num != -1) {  
  42.                             Bitmap bb = bitmapcache.getBitmap(  
  43.                                     heart_all[old_num], mContext);  
  44.                             c.drawBitmap(bb, startx + old_xx, starty + old_yy,  
  45.                                     p);  
  46.                         }  
  47.                         c.drawBitmap(bit, startx + xx, starty + yy, p);  
  48.                         old_num = hua_num;  
  49.                         old_xx = xx;  
  50.                         old_yy = yy;  
  51.                         // c.drawPoint(startx+xx,starty+yy, p);  
  52.                     } catch (Exception e) {  
  53.                         e.printStackTrace();  
  54.                     } finally {  
  55.                         try{  
  56.                             if (c != null){  
  57.                                 holder.unlockCanvasAndPost(c);// 结束锁定画图,并提交改变。  
  58.                             }  
  59.                         }catch(Exception e){  
  60.                             e.printStackTrace();  
  61.                         }  
  62.                     }  
  63.                 }  
  64.           

第一界面就差不多了,一些花边就没必要说了。

第二个界面,要说的也不多,那个一点点画出来的大的爱心是从PC端上移植过去的,但我感觉那个爱心不太圆满,也没时间去修正了,但要注意的是画这个爱心的时候容易发生闪烁,我还是把代码贴出来吧,

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public void run_heart() {  
  2.             int i, j;  
  3.             double x, y, r;  
  4.             int max = 180;  
  5.             //先计算出所有的位置,再去画图  
  6.             float[][] x_ff = new float[max][max];  
  7.             float[][] y_ff = new float[max][max];  
  8.             for (i = 0; i < max; i++) {  
  9.                 for (j = 0; j < max; j++) {  
  10.                     double pi = Math.PI;  
  11.                     r = (pi / 45 * i * (1 - (Math.sin(pi / 45 * j))) * 18);  
  12.                     x = ((r * (Math.cos(pi / 45 * j)) * (Math.sin(pi / 45 * i)) + w / 2) * 1.01);  
  13.                     y = ((-r * (Math.sin(pi / 45 * j)) + h / 4) * 1.01);  
  14.                     x_ff[i][j] = (float) x;  
  15.                     y_ff[i][j] = (float) y;  
  16.                 }  
  17.             }  
  18.   
  19.             i = 0;  
  20.             j = 0;  
  21.             for (i = 0; i < max && !isallstop; i++) {  
  22.   
  23. //                  //sleep,屏幕  
  24.                     try {  
  25.                         Thread.sleep(10);  
  26. //                      clearAll();  
  27.                     } catch (InterruptedException e) {  
  28. //                      // TODO 自动生成的 catch 块  
  29.                         e.printStackTrace();  
  30.                     }  
  31.                     Canvas c = null;  
  32.                     int numm = 10;  
  33.                       
  34.                     for (j = 0; j < max && !isallstop; j=j+numm) {  
  35.                           
  36.                         synchronized (holder) {  
  37.                         try {  
  38.                             Paint p = new Paint(); // 创建画笔  
  39.                             p.setColor(Color.RED);  
  40.                             //找出最大最小  
  41.                             float xx_min=x_ff[i][j],  
  42.                                     xx_max=x_ff[i][j],  
  43.                                     yy_min=y_ff[i][j],  
  44.                                     yy_max=y_ff[i][j];  
  45.                             for(int k =0;k<numm;k++){  
  46.                                 float xx_n = x_ff[i][j+k];  
  47.                                 float yy_n = y_ff[i][j+k];  
  48.                                 if(xx_n >= xx_max) xx_max = xx_n;  
  49.                                 if(xx_n <= xx_min) xx_min = xx_n;  
  50.                                 if(yy_n >= yy_max) yy_max = yy_n;  
  51.                                 if(yy_n <= yy_min) yy_min = yy_n;  
  52.                                           
  53.                             }  
  54.                             int xmin,xmax,ymin,ymax;  
  55.                             if(xx_min == 0) xmin = 0;  
  56.                             else xmin = (int) (xx_min-5>0?xx_min-5:0);  
  57.                             if(yy_min == 0) ymin = 0;  
  58.                             else ymin = (int) (yy_min-5>0?yy_min-5:0);  
  59.                             xmax = (int) (xx_max+5);  
  60.                             ymax = (int) (yy_max+5);  
  61.                               
  62.                           
  63.                             //c = holder.lockCanvas(new Rect(xi,yi,xa,ya));  
  64.                             c = holder.lockCanvas(new Rect(xmin,ymin,xmax,ymax));  
  65.                               
  66.                             if(j!=0){  
  67.                                 int m = j-numm;  
  68.                                 for(int k =0;k<numm;k++){  
  69.                                     float xx_n = x_ff[i][m+k];  
  70.                                     float yy_n = y_ff[i][m+k];  
  71.                                     c.drawPoint(xx_n, yy_n, p);  
  72.                                 }  
  73.                             }  
  74.                             for(int k =0;k<numm;k++){  
  75.                                 float xx_n = x_ff[i][j+k];  
  76.                                 float yy_n = y_ff[i][j+k];  
  77.                                 c.drawPoint(xx_n, yy_n, p);  
  78.                             }  
  79.   
  80.                         } catch (Exception e) {  
  81.                             e.printStackTrace();  
  82.                         } finally {  
  83.                             try{  
  84.                                 if (c != null){  
  85.                                     holder.unlockCanvasAndPost(c);// 结束锁定画图,并提交改变。  
  86.                                 }  
  87.                             }catch(Exception e){  
  88.                                 e.printStackTrace();  
  89.                             }  
  90.                         }  
  91.                         }  
  92.                 }  
  93.             }  
  94.         }  

第三个界面,如果有几张仙女连贯的动态图就好了,可惜我不是美工,尽管这整套都是我自己设计实现的,但不是美工无法设计出仙女散爱心的连续图,于是只能是晃啊晃啊然后爱心下落,下落后可以漂往左边或右边,并程抛物线飞行。

爱心变大,星星闪烁,花朵移动都在一个线程里:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public void show(){  
  2.             int ii = 30;  
  3.             boolean run = true;  
  4.             Paint p = new Paint();  
  5.               
  6.             //旋转的花  
  7.             Bitmap hua = bitmapcache.getBitmap(R.drawable.hua, mContext);  
  8.             int huax = dest_x-40+xadd/2;  
  9.             int huay = 30;  
  10.             int huaw = hua.getWidth();  
  11.             int huah = hua.getHeight();  
  12.             int huamax = 180;  
  13.             int huamin = 0;  
  14.             int hua_add_plus = 2;  
  15.             int huar=0;   
  16.             int re_num = 0;  
  17.               
  18.             //星  
  19.             Bitmap xin1 = bitmapcache.getBitmapByLM(R.drawable.xin1, mContext,2);  
  20.             Bitmap xin2 = bitmapcache.getBitmapByLM(R.drawable.xin2, mContext,2);  
  21.             int xin1w = xin1.getWidth();  
  22.             int xin1h = xin1.getHeight();  
  23.             int xin2w = xin2.getWidth();  
  24.             int xin2h = xin2.getHeight();  
  25.             //三个星1,三个星2  
  26.             ArrayList<LoveDot> xinall = new ArrayList<LoveDot>();  
  27.             xinall.add(new LoveDot(1+xadd,10,1));  
  28.             xinall.add(new LoveDot(48+xadd,18,1));  
  29.             xinall.add(new LoveDot(110+xadd,40,1));  
  30.             xinall.add(new LoveDot(20+xadd,150,2));  
  31.             xinall.add(new LoveDot(150+xadd,160,2));  
  32.             xinall.add(new LoveDot(130+xadd,190,2));  
  33.               
  34.             boolean xinboolean = true;  //为真的时候画星,为假的时候擦除  
  35.             int oldx = 0;  
  36.             while (true  && !isallstop) {  
  37.                 try {  
  38.                     Thread.sleep(150);  
  39.                 } catch (InterruptedException e2) {  
  40.                     // TODO 自动生成的 catch 块  
  41.                     e2.printStackTrace();  
  42.                 }  
  43.                   
  44.                 //旋转加透明  
  45.                 synchronized (holder) {  
  46.                     Canvas c = null;  
  47.                     Bitmap b2 = null;     
  48.                     try {  
  49.                         //c.drawColor(co);                    
  50.                         Matrix m = new Matrix();  
  51.                         m.setRotate(huar);  
  52.                         p.setAlpha(255-Math.abs(huar));  
  53.                         b2 = Bitmap.createBitmap(  
  54.                                     hua, 0, 0, huaw,huah, m, true);   
  55.                         c = holder.lockCanvas(new Rect(huax,huay,huax+b2.getWidth(),  
  56.                                 huay+b2.getHeight()));  
  57.                         c.drawColor(Color.TRANSPARENT,Mode.CLEAR);  
  58.                         c.drawBitmap(b2, huax,huay, p);  
  59.                         //c.drawBitmap(big, dest_x, dest_y, p);  
  60.                           
  61.                         huar = huar+hua_add_plus;  
  62.                         if(huar==huamax) hua_add_plus = -2;  
  63.                         if(huar == huamin) hua_add_plus = 2;  
  64.                           
  65.                     } catch (Exception e) {  
  66.                         e.printStackTrace();  
  67.                     } finally {  
  68.                         try{  
  69.                             if (c != null){  
  70.                                 holder.unlockCanvasAndPost(c);// 结束锁定画图,并提交改变。  
  71.                             }  
  72.                         }catch(Exception e){  
  73.                             e.printStackTrace();  
  74.                         }  
  75.                         //if(b2 != null)  
  76.                         //  b2.recycle();  
  77.                     }  
  78.                 }  
  79.                   
  80.                   
  81.                 //星星闪烁  
  82.                 //为真的时候画星,为假的时候擦除  
  83.                 if(xinboolean){  
  84.                       
  85.                     LoveDot d = xinall.get(oldx);  
  86.                     Bitmap xinb = null;  
  87.                     int xw,xh;  
  88.                     int xx = d.x;  
  89.                     int yy = d.y;  
  90.                     if(d.num == 2){  
  91.                         xinb = xin2;  
  92.                         xw = xin2w;  
  93.                         xh = xin2h;  
  94.                     }  
  95.                     else {  
  96.                         xinb = xin1;  
  97.                         xw = xin1w;  
  98.                         xh = xin1h;  
  99.                     }  
  100.                       
  101.                     synchronized (holder) {  
  102.                         Canvas c = null;  
  103.                         try {  
  104.                             c = holder.lockCanvas(new Rect(xx,yy,xx+xw,yy+xh));                           
  105.                             p.setAlpha(255);  
  106.                             //c.drawColor(Color.TRANSPARENT,Mode.CLEAR);  
  107.                             c.drawBitmap(xinb, xx,yy, p);  
  108.                           
  109.                         } catch (Exception e) {  
  110.                             e.printStackTrace();  
  111.                         } finally {  
  112.                             try{  
  113.                                 if (c != null){  
  114.                                     holder.unlockCanvasAndPost(c);// 结束锁定画图,并提交改变。  
  115.                                 }  
  116.                             }catch(Exception e){  
  117.                                 e.printStackTrace();  
  118.                             }  
  119.                         }  
  120.   
  121.                     }  
  122.                     //oldx = thisone;  
  123.                     xinboolean = !xinboolean;  
  124.                 }else{  
  125.                     int thisone = getRandom(0, xinall.size()-1);  
  126.                     LoveDot d = xinall.get(thisone);  
  127.                     int xw,xh;  
  128.                     int xx = d.x;  
  129.                     int yy = d.y;  
  130.                     if(d.num == 2){  
  131.                         //xinb = xin2;  
  132.                         xw = xin2w;  
  133.                         xh = xin2h;  
  134.                     }  
  135.                     else {  
  136.                         //xinb = xin1;  
  137.                         xw = xin1w;  
  138.                         xh = xin1h;  
  139.                     }  
  140.                     synchronized (holder) {  
  141.                         Canvas c = null;  
  142.                         try {  
  143.                             c = holder.lockCanvas(new Rect(xx,yy,xx+xw,yy+xh));                           
  144.                             p.setAlpha(255);  
  145.                             c.drawColor(Color.TRANSPARENT,Mode.CLEAR);                        
  146.                         } catch (Exception e) {  
  147.                             e.printStackTrace();  
  148.                         } finally {  
  149.                             try{  
  150.                                 if (c != null){  
  151.                                     holder.unlockCanvasAndPost(c);// 结束锁定画图,并提交改变。  
  152.                                 }  
  153.                             }catch(Exception e){  
  154.                                 e.printStackTrace();  
  155.                             }  
  156.                         }  
  157.   
  158.                     }  
  159.                     oldx = thisone;  
  160.                     xinboolean = !xinboolean;  
  161.                 }  
  162.                                                   
  163.                 re_num++;  
  164.                 if(re_num>3){  
  165.                     re_num = 0;  
  166.                 }else continue;  
  167.                   
  168.                 Bitmap big15 = bitmapcache.getBitmap(R.drawable.big99, mContext);  
  169.                 //Y为dest ,x 为dest - w/2  
  170.                 int bw = big15.getWidth();  
  171.                 int bh = big15.getHeight();  
  172.                 Bitmap mBitmap = Bitmap.createScaledBitmap(big15, bw-ii, bh-ii, true);    
  173.                 bw = mBitmap.getWidth();  
  174.                 bh = mBitmap.getHeight();  
  175.                 int x = dest_x-bw/2;  
  176.                 int y = dest_y;  
  177.                 //  
  178.                 dropx = x;  
  179.                 dropy = y;  
  180.                 //dropw = ,droph  
  181.                 synchronized (holder) {  
  182.                     Canvas c = null;  
  183.                     try {  
  184.                         c = holder.lockCanvas(new Rect(x-1,y-1,x+1+bw,y+1+bh));                   
  185.                         p.setAlpha(255);  
  186.                         c.drawColor(Color.TRANSPARENT,Mode.CLEAR);  
  187.                         c.drawBitmap(mBitmap, x,y, p);  
  188.                         //c.drawBitmap(big, dest_x, dest_y, p);  
  189.                     } catch (Exception e) {  
  190.                         e.printStackTrace();  
  191.                     } finally {  
  192.                         try{  
  193.                             if (c != null){  
  194.                                 holder.unlockCanvasAndPost(c);// 结束锁定画图,并提交改变。  
  195.                             }  
  196.                         }catch(Exception e){  
  197.                             e.printStackTrace();  
  198.                         }  
  199.                     }  
  200.   
  201.                 }  
  202.                 ii--;  
  203.                 if(ii <=0) {  
  204.                     try {  
  205.                         Thread.sleep(500);  
  206.                     } catch (InterruptedException e1) {  
  207.                         // TODO 自动生成的 catch 块  
  208.                         e1.printStackTrace();  
  209.                     }  
  210.                     ii = 30;  
  211.   
  212.                     synchronized (holder) {  
  213.                         Canvas c = null;  
  214.                         try {  
  215.                             c = holder.lockCanvas(new Rect(x-1,y-1,x+1+bw,y+1+bh));  
  216.                             //c.drawColor(co);  
  217.                             c.drawColor(Color.TRANSPARENT,Mode.CLEAR);  
  218.                             //c.drawBitmap(big15, x,y, p);  
  219.                             //c.drawBitmap(big, dest_x, dest_y, p);  
  220.                         } catch (Exception e) {  
  221.                             e.printStackTrace();  
  222.                         } finally {  
  223.                             try{  
  224.                                 if (c != null){  
  225.                                     holder.unlockCanvasAndPost(c);// 结束锁定画图,并提交改变。  
  226.                                 }  
  227.                             }catch(Exception e){  
  228.                                 e.printStackTrace();  
  229.                             }  
  230.                         }  
  231.   
  232.                     }  
  233.                     if(!isallstop){  
  234.                         dropnum++;  
  235.                         Thread drop = new LoveDrop(dropnum,x,y);  
  236.                         drop.start();  
  237.                         dropthread_Add(dropnum, drop);  
  238.                         //new LoveDrop(x,y).start();  
  239.                     }  
  240.                               
  241.                     try {  
  242.                         Thread.sleep(1500);  
  243.                     } catch (InterruptedException e1) {  
  244.                         // TODO 自动生成的 catch 块  
  245.                         e1.printStackTrace();  
  246.                     }  
  247.                 }  
  248.             }  
  249.         }  
  250. <span style="font-size:14px;">下落的代码如下</span>  
  251. private void drop(){  
  252.             //startx = dest_x;  
  253.             //starty = dest_y+10;  
  254.             endy = h-100;  //最多下落到这  
  255.             endx = startx;  
  256.             Bitmap llove = bitmapcache.getBitmap(R.drawable.big_h, mContext);  
  257.             int bw = llove.getWidth();  
  258.             int bh = llove.getHeight();  
  259.             Paint p = new Paint();  
  260.             boolean isr = true;  
  261.             long de = 60;  
  262.             dropw = bw+1;  
  263.             droph = bh+1;  
  264.             while(isr && !isallstop){  
  265.                 synchronized (holder) {  
  266.                     Canvas c = null;  
  267.                     try {  
  268.                         //Xfermode xFermode = new PorterDuffXfermode(Mode.DST_ATOP);  
  269.                         //p.setXfermode(xFermode);  
  270.                         c = holder.lockCanvas(new Rect(startx,starty-2,startx+bw,starty+bh+1));                       
  271.                         c.drawColor(Color.TRANSPARENT,Mode.CLEAR);  
  272.                         c.drawBitmap(llove, startx,starty++, p);                          
  273.                     } catch (Exception e) {  
  274.                         e.printStackTrace();  
  275.                     } finally {  
  276.                         try{  
  277.                             if (c != null){  
  278.                                 holder.unlockCanvasAndPost(c);// 结束锁定画图,并提交改变。  
  279.                             }  
  280.                         }catch(Exception e){  
  281.                             e.printStackTrace();  
  282.                         }  
  283.                     }  
  284.   
  285.                 }  
  286.                 try {  
  287.                     Thread.sleep(de);  
  288.                 } catch (InterruptedException e) {  
  289.                     // TODO 自动生成的 catch 块  
  290.                     e.printStackTrace();  
  291.                 }  
  292.                 de=(long) (de-0.05);  
  293.                 if(de <=40) de=(long) (de-0.01);  
  294.                 if(de <=20) de = 20;  
  295.                 if(starty >= endy && !isallstop){//准备往左右  
  296.                     isr = false;  //结束线程  
  297.                      // 初始化y轴数据                   
  298.                     int centerY = endy;  
  299.                     //int[] Y_axis = new int[w-startx];  
  300.                     int left_right = getRandom(0, 10);  
  301.                     boolean isright = true;  //为真为右,为假为左,为右的机会大些  
  302.                     if(left_right<4){  //左  
  303.                         isright = false;  
  304.                     }  
  305.                     int le,top;  
  306.                       
  307.                     int maxhh = endy - maxziy;  
  308.                     int rmin = 80,rmax = 100;  
  309.                     int lmin = 40,lmax = 50;  
  310.                     if(maxhh <100 && maxhh>50 ) {  
  311.                         rmin = 50;  
  312.                         rmax = 60;  
  313.                     }  
  314.                     if(maxhh < 50){  
  315.                         rmin = 30;  
  316.                         rmax = 40;  
  317.                         lmin = 20;  
  318.                         lmax = 30;  
  319.                     }  
  320.                       
  321.                     if(isright){  
  322.                         le = w-startx;  
  323.                         top = getRandom(rmin, rmax);  
  324.                     }  
  325.                     else {  
  326.                         le = startx+bw;  
  327.                         top = getRandom(lmin,lmax);  
  328.                     }  
  329.                     for (int i = 1; i < le && !isallstop; i++) {// 计算正弦波  
  330.                         int x;  
  331.                         if(isright)  
  332.                             x = startx+i;  
  333.                         else x = startx-i;  
  334.                         //y=Asin(ωx+φ φ(初相位):决定波形与X轴位置关系或横向移动距离(左加右减)  
  335.                         //ω:决定周期(最小正周期T=2π/|ω|)  
  336.                         //      A:决定峰值(即纵向拉伸压缩的倍数)    
  337.                         int y = centerY-Math.abs( (int) (top * Math.sin(i * 2 * Math.PI/ 180)));  
  338.                         synchronized (holder) {  
  339.                             Canvas c = null;  
  340.                             try {  
  341.                                 c = holder.lockCanvas(new Rect(x-2,  
  342.                                         y-15, x + bw+1, y + bh  
  343.                                                 + 15));                               
  344.                                 c.drawColor(Color.TRANSPARENT, Mode.CLEAR);  
  345.                                 c.drawBitmap(llove, x, y, p);                                 
  346.                             } catch (Exception e) {  
  347.                                 e.printStackTrace();  
  348.                             } finally {  
  349.                                 try{  
  350.                                     if (c != null){  
  351.                                         holder.unlockCanvasAndPost(c);// 结束锁定画图,并提交改变。  
  352.                                     }  
  353.                                 }catch(Exception e){  
  354.                                     e.printStackTrace();  
  355.                                 }  
  356.                             }  
  357.   
  358.                         }// sy  
  359.                         int delay = endy - y;  
  360.                         try {  
  361.                             //顶峰慢,delay 大,越慢,时间长  
  362.                             Thread.sleep(50+delay*2);  
  363.                             //System.out.println("y:"+y);  
  364.                         } catch (InterruptedException e) {  
  365.                             // TODO 自动生成的 catch 块  
  366.                             e.printStackTrace();  
  367.                         }  
  368.                     }     
  369.                 }  
  370.             }  
  371.         }  

至于字的显现,应该没什么好说,值得注意的是,还有一些闪烁的爱心和下缀的那一段,要注意这些坐标,不能覆盖字,要处理所有出现的心的坐标,和字的坐标。

另外,还有播放声音的功能,可以自己设置合适的音乐或者自己录好自己想说的话然后播放出来。

源码还在不断修改更新中,随意拿去自用。



这里不能附件下载,于是源码上传到资源那里了。


作者:wuxia2001

转载自: http://blog.csdn.net/wuxia2001/article/details/18268005


展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读