Android中利用画图类和线程画出闪烁的心形,送给亲爱的他
大家先看图片:
因为前一段时间在写画图类,刚好有一个线程控制画图闪烁的,我就想说我能不能做一个心形闪烁的,出来的效果就如图,先贴再讲解代码:
里面设置两个类,一个是我们的activity类,这个类用来显示示图,然后建一个继承surfaceview的类,我们在这里面画图。先贴两个累的代码:
主类名:iaiaimainactivity,画图类类名:love.
package com.iaiai.activity;import android.app.activity;import android.os.bundle;/** * * <p> * title: iaiaiactivity.java * </p> * <p> * e-mail: 176291935@qq.com * </p> * <p> * qq: 176291935 * </p> * <p> * http: iaiai.iteye.com * </p> * <p> * create time: 2011-8-11 * </p> * * @author 丸子 * @version 0.0.1 */public class iaiaiactivity extends activity {@overridepublic void oncreate(bundle savedinstancestate) {super.oncreate(savedinstancestate);love love = new love(this);setcontentview(love);}}
package com.iaiai.activity;import android.content.context;import android.graphics.canvas;import android.graphics.color;import android.graphics.paint;import android.graphics.rectf;import android.graphics.typeface;import android.view.surfaceholder;import android.view.surfaceview;/** * * <p> * title: love.java * </p> * <p> * e-mail: 176291935@qq.com * </p> * <p> * qq: 176291935 * </p> * <p> * http: iaiai.iteye.com * </p> * <p> * create time: 2011-8-11 * </p> * * @author 丸子 * @version 0.0.1 */public class love extends surfaceview implements surfaceholder.callback,runnable {boolean mbloop = false;surfaceholder msurfaceholder = null;private canvas canvas;int micount = 0;int y = 50;/** * @param context */public love(context context) {super(context);msurfaceholder = this.getholder();msurfaceholder.addcallback(this);this.setfocusable(true);this.setkeepscreenon(true);mbloop = true;}/* * (non-javadoc) * * @see * android.view.surfaceholder.callback#surfacechanged(android.view.surfaceholder * , int, int, int) */@overridepublic void surfacechanged(surfaceholder holder, int format, int width,int height) {// todo auto-generated method stub}/* * (non-javadoc) * * @see * android.view.surfaceholder.callback#surfacecreated(android.view.surfaceholder * ) */@overridepublic void surfacecreated(surfaceholder holder) {// todo auto-generated method stubnew thread(this).start();}/* * (non-javadoc) * * * @seeandroid.view.surfaceholder.callback#surfacedestroyed(android.view. * surfaceholder) */@overridepublic void surfacedestroyed(surfaceholder holder) {// todo auto-generated method stubmbloop = false;}/* * (non-javadoc) * * @see java.lang.runnable#run() */@overridepublic void run() {// todo auto-generated method stubwhile (mbloop) {try {thread.sleep(200);} catch (exception e) {// todo: handle exception}synchronized (msurfaceholder) {draw();}}}private void draw() {// todo auto-generated method stubcanvas = msurfaceholder.lockcanvas();try {if (msurfaceholder == null || canvas == null) {return;}if (micount < 100) {micount++;} else {micount = 0;}paint paint = new paint();paint.setantialias(true);paint.setcolor(color.black);canvas.drawrect(0, 0, 320, 480, paint);switch (micount % 6) {case 0:paint.setcolor(color.blue);break;case 1:paint.setcolor(color.green);break;case 2:paint.setcolor(color.red);break;case 3:paint.setcolor(color.yellow);break;case 4:paint.setcolor(color.argb(255, 255, 181, 216));break;case 5:paint.setcolor(color.argb(255, 0, 255, 255));break;default:paint.setcolor(color.white);break;}int i, j;double x, y, r;for (i = 0; i <= 90; i++) {for (j = 0; j <= 90; j++) {r = math.pi / 45 * i * (1 - math.sin(math.pi / 45 * j))* 20;x = r * math.cos(math.pi / 45 * j)* math.sin(math.pi / 45 * i) + 320 / 2;y = -r * math.sin(math.pi / 45 * j) + 400 / 4;canvas.drawpoint((float) x, (float) y, paint);}}paint.settextsize(32);paint.settypeface(typeface.create(typeface.serif, typeface.italic));rectf rect = new rectf(60, 400, 260, 405);canvas.drawroundrect(rect, (float) 1.0, (float) 1.0, paint);canvas.drawtext("loving you", 75, 400, paint);msurfaceholder.unlockcanvasandpost(canvas);} catch (exception e) {}}}