(libgdx学习)pixmap(1)


一、初始化及常见API

             

pixmap = new Pixmap(800, 480, Format.RGBA8888);
		pixmap.setColor(Color.BLUE);//设置pixmap的颜色
		pixmap.drawLine(0, 0, 100, 100);//画线
		pixmap.drawLine(200, 0, 300, 100);//起点(x1,y1)、终点(x2,y2)
		pixmap.fillCircle(150, 150, 32);//画实心圆.(x,y)和半径
		pixmap.drawCircle(250, 100, 50);//画空心圆.(x,y)和半径
		pixmap.drawRectangle(0, 0, 150, 150);//画空心矩形.起点(x,y),(width,height)
		pixmap.fillRectangle(0, 160, 100, 100);//画实心矩形.起点(x,y),(width,height)
		/**
		 * pixmap.getHeight(): 获取pixmap的高度
		 * pixmap.getWidth(): 获取pixmap的宽度
		 * pixmap.getFormat(): 获取pixmap的格式
		 */
		System.out.println("pixmap.getHeight(): " + pixmap.getHeight() + ",pixmap.getWidth( " + pixmap.getWidth() + ", pixmap.getFormat()" + pixmap.getFormat());


二、应用举例

package com.example.groupactiontest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Input.Peripheral;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;

public class MyGame implements ApplicationListener {

	Stage stage;
	Image image;
	
	SpriteBatch batch;
	Texture texture;
	TextureRegion region;
	Pixmap pixmap;
	
	@Override
	public void create() {
		stage = new Stage(800,480,false);
		
		batch = new SpriteBatch();
		texture = new Texture(1024, 1024, Format.RGBA8888);
		pixmap = new Pixmap(800, 480, Format.RGBA8888);
		
		pixmap.setColor(Color.BLUE);//设置pixmap的颜色
		pixmap.drawLine(0, 0, 100, 100);//画线
		
		pixmap.setColor(Color.GREEN);
		pixmap.drawLine(100, 100, 200, 0);
		
		pixmap.setColor(Color.ORANGE);
		pixmap.drawLine(200, 0, 300, 100);//起点(x1,y1)、终点(x2,y2)
		
		pixmap.setColor(Color.WHITE);
		pixmap.fillCircle(150, 150, 32);//画实心圆.(x,y)和半径
		
		pixmap.setColor(Color.RED);
		pixmap.drawCircle(250, 100, 50);//画空心圆.(x,y)和半径
		
		pixmap.setColor(Color.YELLOW);
		pixmap.drawRectangle(0, 0, 150, 150);//画空心矩形.起点(x,y),(width,height)
		
		pixmap.setColor(Color.MAGENTA);
		pixmap.fillRectangle(0, 160, 100, 100);//画实心矩形.起点(x,y),(width,height)
		
		/**
		 * pixmap.getHeight(): 获取pixmap的高度
		 * pixmap.getWidth(): 获取pixmap的宽度
		 * pixmap.getFormat(): 获取pixmap的格式
		 */
		System.out.println("pixmap.getHeight(): " + pixmap.getHeight() + ",pixmap.getWidth( " + pixmap.getWidth() + ", pixmap.getFormat()" + pixmap.getFormat());
		
		
		texture.draw(pixmap, 0, 0);//在texture中套一个pixmap图层
		
		region = new TextureRegion(texture, 800, 480);
		
//		image = new Image(region);
//		stage.addActor(image);
//		
//		Gdx.input.setInputProcessor(stage);
		
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void render() {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		batch.begin();
		
		batch.draw(region, 0, 0);
		
		batch.end();
		
		
//		stage.act();
//		stage.draw();
		
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

}


四、源码下载

http://download.csdn.net/detail/caihongshijie6/7065437

你可能感兴趣的:((libgdx学习)pixmap(1))