搞一个心形闪烁的图,出来的效果就下如图,里面设置两个类,一个是我们的activity类,这个类用来显示示图,然后建一个继承SurfaceView的类,我们在这里面画图。
模拟器效果图.jpg- 画图的时候你可以继承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 接口回调中可以通过重写来改变这些方法
- 程序其实很简单, 既然生命了Runnable接口,就有相对应的Run方法,在surfaceCreate()的时候开启线程,线程每隔500ms就刷新一次,这样我们看到的效果就是闪烁的,每500毫秒 画一次图,根据经过的间隔时间来设置画笔的颜色,然后通过循环描点,画出心形,然后设置字体大小,画字和字下面的横线。
Activity类
/**
*
* @author Looper
*
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LoveView view = new LoveView(this);
setContentView(view);
}
}
SurfaceView类
/**
*
* @author Looper
*
*/
public class LoveView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
boolean mbloop = false;
SurfaceHolder mSurfaceHolder = null;
private Canvas canvas;
int miCount = 0;
int y = 50;
/**
* @param context
*/
public LoveView(Context context) {
super(context);
mSurfaceHolder = this.getHolder();
mSurfaceHolder.addCallback(this);
this.setFocusable(true);
this.setKeepScreenOn(true);
mbloop = true;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
new Thread(this).start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mbloop = false;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (mbloop) {
try {
Thread.sleep(500);
} catch (Exception e) {
// TODO: handle exception
}
synchronized (mSurfaceHolder) {
Draw();
}
}
}
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) {
e.printStackTrace();
}
}
}
网友评论