android canvas类

最近在学习android下编程。想把自己实践的代码写下来。

一方面可以加深自己的印象,方便自己以后复习,另一方面可以帮组对想学这方面的朋友一起进步。


⑴  canvas 中的文字输出:drawText();

官方给的参数是:

public void drawText (String text, float x, float y, Paint paint)

Since:  API Level 1

Draw the text, with origin at (x,y), using the specified paint. The origin is interpreted based on the Align setting in the paint.

Parameters
text The text to be drawn
x The x-coordinate of the origin of the text being drawn
y The y-coordinate of the origin of the text being drawn
paint The paint used for the text (e.g. color, size, style)

public void drawBitmap (Bitmap bitmap, float left, float top, Paint paint)
Since:  API Level 1

Draw the specified bitmap, with its top/left corner at (x,y), using the specified paint, transformed by the current matrix.

Note: if the paint contains a maskfilter that generates a mask which extends beyond the bitmap's original width/height (e.g. BlurMaskFilter), then the bitmap will be drawn as if it were in a Shader with CLAMP mode. Thus the color outside of the original width/height will be the edge color replicated.

If the bitmap and canvas have different densities, this function will take care of automatically scaling the bitmap to draw at the same density as the canvas.

Parameters
bitmap The bitmap to be drawn
left The position of the left side of the bitmap being drawn
top The position of the top side of the bitmap being drawn
paint The paint used to draw the bitmap (may be null)

第一个参数就是要显示的图片。同时图片要存放在res\drawable,系统才能找到。后面三个和drawText()后面三个参数差不多。


上面两个的应用代码:
package com.android.game;

import android.app.Activity;
import android.os.Bundle;

public class Hello_tankActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new GameView(this));
     //   setContentView(R.layout.main);
    }
}


package com.android.game;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.*;

public class GameView extends View {

	Bitmap bmp;
	public GameView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		Resources res = context.getResources();
		bmp = BitmapFactory.decodeResource(res, R.drawable.ic_launcher);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		Paint paint = new Paint(); 
		paint.setColor(Color.RED); 
		paint.setStyle(Style.FILL); 
		
		canvas.drawPaint(paint); 
		paint.setColor(Color.BLACK); 
		paint.setTextSize(20); 
		
		canvas.drawText("Some Text", 10, 25, paint); 
		canvas.drawBitmap(bmp, 0, 100, new Paint());
	}

}

运行后的效果



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值