canvas教程

Android中利用画图类和线程画出闪烁的心形

字号+ 作者:H5之家 来源:H5之家 2017-11-14 16:00 我要评论( )

[代码] [Java]代码package com.tt.view;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphi

[代码] [Java]代码

package com.tt.view;

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;

public class Love extends SurfaceView implements SurfaceHolder.Callback,Runnable

{

boolean mbloop = false;

SurfaceHolder mSurfaceHolder = null;

private Canvas canvas;

int minCount = 0;

int y = 50;

public Love(Context context)

{

super(context);

mSurfaceHolder = this.getHolder();

mSurfaceHolder.addCallback(this);

this.setFocusable(true);

this.setKeepScreenOn(true);

mbloop = true;

}

@Override

public void run()

{

// TODO Auto-generated method stub

while(mbloop)

{

try

{

Thread.sleep(200);

} catch (Exception e) {

// TODO: handle exception

}

synchronized (mSurfaceHolder)

{

draw();

}

}

}

@Override

public void surfaceChanged(SurfaceHolder holder, int format, int width,

int height) {

// TODO Auto-generated method stub

}

@Override

public void surfaceCreated(SurfaceHolder holder)

{

// TODO Auto-generated method stub

new Thread(this).start();

}

@Override

public void surfaceDestroyed(SurfaceHolder holder)

{

// TODO Auto-generated method stub

mbloop = false;

}

private void draw()

{

canvas = mSurfaceHolder.lockCanvas();

try

{

if(mSurfaceHolder == null || canvas == null)

{

return;

}

if(minCount < 100)

{

minCount ++;

}

else

{

minCount = 0;

}

Paint paint = new Paint();

paint.setAntiAlias(true);

paint.setColor(Color.BLACK);

canvas.drawRect(0, 0,320,480,paint);

switch (minCount % 6)

{

case 0:

paint.setColor(Color.YELLOW);

break;

case 1:

paint.setColor(Color.BLUE);

break;

case 2:

paint.setColor(Color.GREEN);

break;

case 3:

paint.setColor(Color.RED);

break;

case 4:

paint.setColor(Color.argb(255, 255, 181, 216));

break;

case 5:

paint.setColor(Color.argb(255, 0, 255, 255));

break;

default:

break;

相关搜索(Beta)

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 小程序开发答疑:canvas画图,如何阻止页面上下拖动

    小程序开发答疑:canvas画图,如何阻止页面上下拖动

    2017-11-11 18:00

  • android surfaceView 的简单使用 画图,拖动效果

    android surfaceView 的简单使用 画图,拖动效果

    2017-11-11 09:01

  • 架构图(拓扑图)画图工具分析整理(静态,动态,可交互图.层级t

    架构图(拓扑图)画图工具分析整理(静态,动态,可交互图.层级t

    2017-11-08 13:01

  • 教教我!用C画图,给分!!!!!

    教教我!用C画图,给分!!!!!

    2017-10-28 08:43

网友点评