zgz345

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
本文讲解主要涉及的知识点:
1.线程控制 
2.画图类 
3.心形函数
大家先看图片:
<ignore_js_op>
因为前一段时间在写画图类,刚好有一个线程控制画图闪烁的,我就想说我能不能做一个心形闪烁的,出来的效果就如图,先贴再讲解代码:
里面设置两个类,一个是我们的activity类,这个类用来显示示图,然后建一个继承SurfaceView的类,我们在这里面画图。先贴两个累的代码:
主类名:GameMainActivity,画图类类名:Love.
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		setContentView(new FxCanvasView(this, null));
		setContentView(new Love(this));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

  

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;

/**
 * @author CZ
 * 
 */
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)
     */
    @Override
    public 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
     * )
     */
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        new Thread(this).start();
    }

    /*
     * (non-Javadoc)
     * 
     * @seeandroid.view.SurfaceHolder.Callback#surfaceDestroyed(android.view.
     * SurfaceHolder)
     */
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        mbloop = false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (mbloop) {
            try {
                Thread.sleep(200);
            } catch (Exception e) {
                // TODO: handle exception
            }
            synchronized (mSurfaceHolder) {
                Draw();
            }
        }
    }

    /**
     * 
     * Year:2011 Date:2011-7-27 Time:下午06:52:04 Author:CZ TODO
     */
    private void Draw() { 
         // TODO Auto-generated method stub 
         canvas = 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) { 
         } 
 
     }
}
关于这个程序要讲解的几点:
1. 画图的时候你可以继承View,也可以继承SurfaceView,这两者的区别在于:surfaceView是在一个新起的单独线程中可以重新绘制画面而View必须在UI的主线程中更新画面。SurfaceView可以控制表面的格式,比如大小,显示在屏幕中的位置,最关键是的提供了SurfaceHolder类,使用getHolder方法获取,还有涉及的surfaceCreated(SurfaceHolder holder),surfaceDestroyed(SurfaceHolder holder),surfaceChanged(SurfaceHolder holder, int format, int width, int height)方法,而在SurfaceHolder.Callback 接口回调中可以通过重写来改变这些方法
2.程序其实很简单, 既然生命了Runnable接口,就有相对应的Run方法,在surfaceCreate()的时候开启线程,线程每隔200ms就刷新一次,这样我们看到的效果就是闪烁的,每200毫秒 画一次图,根据经过的间隔时间来设置画笔的颜色,然后通过循环描点,画出心形,然后设置字体大小,画字和字下面的横线。
3.关于心形函数,是从一个例子中看来得,关于x和y的得到,
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;
320是屏幕的宽度,本来竖屏我设置的是480,可是下面得写字,就设置为400的了,关于画更好看的心形还有一个函数,大家可以看下:
posted on 2013-08-28 19:04  zonyZhang  阅读(627)  评论(0编辑  收藏  举报