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());
	}

}

运行后的效果



你可能感兴趣的:(android,api,Class,float,Constructor,shader)