android canvas 源代码,Android应用开发揭秘的第9个程序5_4_canvas修改版和高仿版的源码注释...

//学会运用画布canvas

Activity01.java

package com.canvas;

import android.app.Activity;

import android.os.Bundle;

public class Activity01 extends Activity

{

private GameView mGameView =null;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

mGameView = new GameView(this);

setContentView(mGameView);

}

}

GameView.java

package com.canvas;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Rect;

import android.view.KeyEvent;

import android.view.MotionEvent;

import android.view.View;

public class GameView extends View implements Runnable

{

/* 声明Paint对象 */

private Paint    mPaint = null;

public GameView(Context context)

{

super(context);

/* 构建对象 */

mPaint = new Paint();

/* 开启线程 */

new Thread(this).start();

}

public void onDraw(Canvas canvas)

{

super.onDraw(canvas);

/* 设置画布的颜色 */

canvas.drawColor(Color.BLACK);

/* 设置取消锯齿效果 */

mPaint.setAntiAlias(true);

/* 设置裁剪区域 */

//canvas.clipRect(10, 10, 280, 260);

/* 线锁定画布 */

canvas.save();

/* 旋转画布 */

canvas.rotate(10.0f); //45.0f

/* 设置颜色及绘制矩形 */

mPaint.setColor(Color.RED);

canvas.drawRect(new Rect(15,15,140,70), mPaint);

/* 解除画布的锁定 */

canvas.restore();

/* 设置颜色及绘制另一个矩形 */

mPaint.setColor(Color.GREEN);

canvas.drawRect(new Rect(150,75,260,120), mPaint);

}

// 触笔事件

public boolean onTouchEvent(MotionEvent event)

{

return true;

}

// 按键按下事件

public boolean onKeyDown(int keyCode, KeyEvent event)

{

return true;

}

// 按键弹起事件

public boolean onKeyUp(int keyCode, KeyEvent event)

{

return false;

}

public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)

{

return true;

}

public void run()

{

while (!Thread.currentThread().isInterrupted())

{

try

{

Thread.sleep(100);

}

catch (InterruptedException e)

{

Thread.currentThread().interrupt();

}

// 使用postInvalidate可以直接在线程中更新界面

postInvalidate();

}

}

}

Gf_canvasActivity.java

package pak.canvas;

import android.app.Activity;

import android.os.Bundle;

public class Gf_canvasActivity extends Activity {  private GameView mGameView =null;  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState)  {   super.onCreate(savedInstanceState);      mGameView = new GameView(this);      setContentView(mGameView);  } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值