To those favoriting this Question, give it an upvote too
I'm using a a class that extends View. In this class I have the onDraw method where I do my drawing.
When I launch my app, everything works as intended except for a little annoying thing.. whenever I press anywhere on my screen, the drawing, which calculates the coordinates of where I touched the screen - and is supposed to draw the image in those exact coordinates, draws them a little bit off of where I originally pressed.
For example, if I tap the screen at x = 150, y = 250, the image will be displayed off by a significant amount of pixels.
To be clear, when I press the screen, I want my image displayed exactly under my finger or in very close proximity to where I tapped it, however, the image is displayed usually 100 or so pixels below where the press occured.
Is there something that I do not know about coordinates when using a canvas to draw?
Here is my code
public class PixelParticle extends View{
private float locationX;
private float locationY;
private int sizeX;
private int sizeY;
private Paint color;
private Rect rect;
private int paintcolor;
private float myLocX;
private float myLocY;
public PixelParticle(Context context, float locationX, float locationY, int sizeX, int sizeY, int paintcolor) {
super(context);
this.locationX = locationX;
this.locationY = locationY;
this.sizeX = sizeX;
this.sizeY = sizeY;
this.paintcolor = paintcolor;
rect = new Rect();
color = new Paint();
}
#Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, 250, 250, true);
canvas.drawBitmap(resizedBitmap,locationX ,locationY , null);
}
}
And the MainActivity..
public class MainActivity extends Activity {
private int pixelColor;
private float x;
private float y;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
x = event.getX();
y = event.getY();
pixelColor = Color.RED;
PixelParticle pp = new PixelParticle(this, x, y, 50, 50, pixelColor);
setContentView(pp);
return false;
}
}
The height of the screen includes the title and the notification bar.
Make the Activity Full Screen and remove the title will solve the issue.
Try this:
PixelParticle...
public class PixelParticle extends View {
private float locationX;
private float locationY;
private int sizeX;
private int sizeY;
private Paint color;
private Rect rect;
private int paintcolor;
private float myLocX;
private float myLocY;
public PixelParticle(Context context, float locationX, float locationY,
int sizeX, int sizeY, int paintcolor) {
super(context);
this.locationX = locationX;
this.locationY = locationY;
this.sizeX = sizeX;
this.sizeY = sizeY;
this.paintcolor = paintcolor;
rect = new Rect();
color = new Paint();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, 250, 250,
true);
canvas.drawBitmap(resizedBitmap, locationX
- (resizedBitmap.getWidth() / 2),
locationY - (resizedBitmap.getHeight() / 2), null);
}}
MainActivity..
public class MainActivity extends Activity {
private int pixelColor;
private float x;
private float y;
PixelParticle pp ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
x = event.getX();
y = event.getY();
pixelColor = Color.RED;
pp = new PixelParticle(this, x, y, 50, 50, pixelColor);
setContentView(pp);
return false;
}}
Related
I have a problem with moveTo() action. There is no error, but Actor is not moving to the position. The method is called in touchDown(). I spent a few hours and cant find the solution.
Please can you help? Parameters in moveTo() are random for now, so don't worry about it
Please see below the code.
public class GameScreen implements Screen, InputProcessor {
private final float REMOTE_WIDTH = 30;
private final float BATTERY_WIDTH = 11;
private final float BATTERY_HEIGHT = 30;
private final float TRIANGLE_X1 = RgbSortGame.GAME_SCREEN_WIDTH/2 - REMOTE_WIDTH /2;
private final float TRIANGLE_Y1 = RgbSortGame.GAME_SCREEN_HEIGHT/2;
private final float TRIANGLE_X2 = RgbSortGame.GAME_SCREEN_WIDTH/2 + REMOTE_WIDTH /2;
private final float TRIANGLE_Y2 = RgbSortGame.GAME_SCREEN_HEIGHT/2;
private final float TRIANGLE_X3 = RgbSortGame.GAME_SCREEN_WIDTH/2;
private final float TRIANGLE_Y3 = RgbSortGame.GAME_SCREEN_HEIGHT/2 - REMOTE_WIDTH;
private final float BUTTON_RADIUS = REMOTE_WIDTH/4f;
private final float TV_OFFSET_X = (RgbSortGame.GAME_SCREEN_WIDTH-RgbSortGame.GAME_SCREEN_WIDTH/2)/2;
private final float TV_OFFSET_Y = (RgbSortGame.GAME_SCREEN_HEIGHT-RgbSortGame.GAME_SCREEN_HEIGHT/4)/1.1f;
private Level level;
private List<BatteryView> batteryViews;
private Map<Integer, Battery> strategy;
private Viewport viewport;
private TextureAtlas atlas;
private OrthographicCamera camera;
private SpriteBatch batch;
private ShapeRenderer shapeRenderer;
private Texture background;
private Texture tv;
private Texture battery;
private Stage stage;
private Vector2 tempVector;
private Vector2 tempScreenToStage;
private Queue<String> hitActorsNames;
public GameScreen() {
Level1 level1 = new Level1();
level = new Level(level1.getRelativeCoordinates());
batteryViews = new ArrayList<>();
strategy = level.getBatteries();
batch = new SpriteBatch();
camera = new OrthographicCamera();
viewport = new StretchViewport(RgbSortGame.GAME_SCREEN_WIDTH, RgbSortGame.GAME_SCREEN_HEIGHT, camera);
viewport.apply();
stage = new Stage(viewport, batch);
Gdx.input.setInputProcessor(this);
shapeRenderer = new ShapeRenderer();
atlas = new TextureAtlas("buttons/buttons.atlas");
background = new Texture("images/97916b7c0f1f5f582723426ee1f876ce.jpg");
tv = new Texture("images/tv1.png");
battery = new Texture("images/battery.png");
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
camera.update();
hitActorsNames = new ArrayDeque<>();
calculateBatteriesStartCoordinates();
tempVector = new Vector2(0,0);
tempScreenToStage = new Vector2(0,0);
}
#Override
public void show() {
}
#Override
public void render(float delta) {
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(background, 0f,0f,RgbSortGame.GAME_SCREEN_WIDTH, RgbSortGame.GAME_SCREEN_HEIGHT);
batch.draw(tv, TV_OFFSET_X, TV_OFFSET_Y,
RgbSortGame.GAME_SCREEN_WIDTH/2, RgbSortGame.GAME_SCREEN_HEIGHT/4);
batch.end();
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(Color.GOLD);
shapeRenderer.triangle(TRIANGLE_X1, TRIANGLE_Y1,
TRIANGLE_X2, TRIANGLE_Y2,
TRIANGLE_X3, TRIANGLE_Y3);
shapeRenderer.end();
stage.draw();
stage.act(Gdx.graphics.getDeltaTime());
}
#Override
public void resize(int width, int height) {
viewport.update(width, height);
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
camera.update();
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
background.dispose();
batch.dispose();
shapeRenderer.dispose();
atlas.dispose();
stage.dispose();
}
public void calculateBatteriesStartCoordinates(){
float batteryStartPositionX = RgbSortGame.GAME_SCREEN_WIDTH/24;
for (int i = 0; i < strategy.size(); i++) {
if (batteryStartPositionX >= TRIANGLE_X1 && batteryStartPositionX<= TRIANGLE_X2){
batteryStartPositionX += REMOTE_WIDTH;
}
Actor batteryView = new BatteryView(batteryStartPositionX, TRIANGLE_Y3, BATTERY_WIDTH, BATTERY_HEIGHT, BUTTON_RADIUS, strategy.get(i), battery, atlas);
batteryStartPositionX += RgbSortGame.GAME_SCREEN_WIDTH/24 + BATTERY_WIDTH;
batteryView.setName(i + "");
stage.addActor(batteryView);
}
}
#Override
public boolean keyDown(int keycode) {
return false;
}
#Override
public boolean keyUp(int keycode) {
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
tempScreenToStage = stage.screenToStageCoordinates(tempVector.set((float)screenX,(float)screenY));
Actor hitActor = stage.hit(tempScreenToStage.x,tempScreenToStage.y,true);
if (hitActor != null){
hitActorsNames.add(hitActor.getName());
if (hitActorsNames.size() >= 2){
Actor actor = stage.getRoot().findActor(hitActorsNames.poll());
actor.addAction(Actions.moveTo(100f,200f,0.7f));
hitActorsNames.clear();
}
}
}
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolledenter code here(float amountX, float amountY) {
return false;
}
And BatteryView:
public class BatteryView extends Actor{
private float positionX;
private float positionY;
private float width;
private float height;
private float buttonRadius;
private Texture batteryTexture;
private Deque<GameButton> buttonsStrategy;
private TextureAtlas atlas;
public BatteryView(float positionX, float positionY, float width, float height,
float buttonRadius, Battery strategy,
Texture batteryTexture, TextureAtlas atlas) {
this.positionX = positionX;
this.positionY = positionY;
this.batteryTexture = batteryTexture;
this.width = width;
this.height = height;
this.atlas = atlas;
this.buttonRadius = buttonRadius;
this.buttonsStrategy = strategy.getGameButtons();
this.setBounds(positionX,positionY,width,height);
this.setTouchable(Touchable.enabled);
this.setSize(width, height);
}
#Override
public void act(float delta) {
super.act(delta);
}
#Override
public void draw(Batch batch, float parentAlpha){
batch.draw(batteryTexture,positionX,positionY,width,height);
float xButtonPosition = positionX + width / 2 - buttonRadius / 2;
float yButtonPosition = positionY;
for (GameButton gameButton : buttonsStrategy) {
new GameButtonView(xButtonPosition, yButtonPosition,
buttonRadius, atlas.findRegion(gameButton.getColor())).draw(batch);
yButtonPosition += buttonRadius;
}
}
public float getPositionX() {
return positionX;
}
public float getPositionY() {
return positionY;
}
public float getWidth() {
return width;
}`enter code here`
public float getHeight() {
return height;
}
}
Thank you in advance!
UPDATE: I found a solution! I had to change draw method in BatteryView class. It should be batch.draw(batteryTexture,getX(),getY(),width,height)instead off
batch.draw(batteryTexture,positionX,positionY,width,height).
I am new to android programing. I am trying to draw a new bitmap image every time the user touches the screen. With the current code right now, all it does is change where the bitmap is at. It doesn't make a new one. How could I make a new one?? Here is the code:
public class MainDrawingView extends View {
public float eventX;
public float eventY;
public AlertDialog.Builder alertThing;
Context context = getContext();
//Bitmaps
public Bitmap redSquare;
public String x;
public String y;
//Colors
public boolean red = false;
public boolean blue = false;
//Squares
public boolean topRight = false;
public boolean topLeft = false;
public boolean bottomLeft = false;
public boolean bottomRight = false;
public int width = context.getResources().getDisplayMetrics().widthPixels;
public int height = context.getResources().getDisplayMetrics().heightPixels;
public int center = height/2;
public int widthDiv = width/2;
private Paint paint = new Paint();
private Point pointStart = new Point();
private Point pointEnd = new Point();
public MainDrawingView(Context context, AttributeSet attrs){
super(context, attrs);
paint.setAntiAlias(true);
paint.setStrokeWidth(5f);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
alertThing = new AlertDialog.Builder(context);
//define bitmap
redSquare = BitmapFactory.decodeResource(getResources(), R.drawable.red);
}
#Override
public void onDraw(Canvas canvas){
//canvas.drawPath(path, paint);
canvas.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y,paint);
//Makes a straight line go through the center of the screen.
canvas.drawLine(0,height/2.5f, width,height/2.5f,paint);
//Makes a straight line go up and down.
canvas.drawLine(width/2.3f,0, width/2.3f, height,paint);
//Chnages the square colors.
// top left combo
if(topLeft){
if(red){
Toast.makeText(context,"jgjgjgjgjgjgjgjgjjg", Toast.LENGTH_SHORT).show();
new Drawings(redSquare, canvas, eventX, eventY);
}
}
}
#Override
public boolean onTouchEvent(MotionEvent event){
//Gets the coords of the tap
eventX = event.getX();
eventY = event.getY();
int pointX = Math.round(eventX);
int pointY = Math.round(eventY);
x = String.valueOf(eventX);
y = String.valueOf(eventY);
//This is the top left "square"
if(eventY < center && eventX < widthDiv ){
alertThing.setTitle("edit square");
alertThing.setMessage("please choose an option");
alertThing.setPositiveButton("red", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
topLeft = true;
red = true;
invalidate();
}
});
alertThing.setNeutralButton("Chnange color to blue", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertThing.create();
alertThing.show();
}
Toast.makeText(context, "center is :" +x, Toast.LENGTH_SHORT ).show();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
//Sets a new starting point
pointStart.set(pointX,pointY);
return true;
case MotionEvent.ACTION_UP:
//Contects the points
pointEnd.set(pointX,pointY);
break;
default:
return false;
}
invalidate();
return true;
}
}
Here is the Drawings class that draws the bitmaps:
public class Drawings {
public Drawings(Bitmap bitmap, Canvas canvas,float x, float y){
canvas.drawBitmap(bitmap, x,y,null);
}
}
You really should see what canvas.drawBitmap does. Of course it changes the position, since the touch position changes too.
You create a new bitmap just like you did it when defining bitmap.
But creating new objects in the onDraw will be very slow.
I want to draw a texture on a body, which is a box.
How do I convert the coordinates of the body to screen coordinates?
I know that the other way around is with camera.unproject(pos), is it similar to this?
I see a lot of people using constants such as WORLD_TO_SCREEN = 32, but I currently don't have that in my game. Is that a problem, and how can I implement it now? Because it seems like people that are using these factors can convert world to screen positions easily. I currently have a camera and an ExtendViewport
camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
viewport = new ExtendViewport(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, camera);
camera.position.set(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2, 0f);
Viewport width and height are set to this
public static final int VIEWPORT_WIDTH = 20;
public static final int VIEWPORT_HEIGHT = 22;
I don't really know what I'm doing, I read documentation and read some tutorials, but if someone could give some explanation about these variables and the world_to_screen factor that would really help me out.
I also set APP_WIDTH=1280, APP_HEIGHT = 720
Do viewport width and height mean that for box2d my screen is 20 meters wide and 22 meters high?
(asking it again because I added a lot the question and I would really like to know these things)
[EDIT]
So I'm trying to draw a ground picture on the ground body
float x = stage.getGround().getX();
float y = stage.getGround().getY();
float w = stage.getGround().getWidth();
float h = stage.getGround().getHeight();
stage.act(delta);
stage.draw();
stage.updateCamera();
Texture texture = new Texture(Gdx.files.internal("ground.png"));
Sprite sprite = new Sprite(texture);
sprite.setSize(w, h);
sprite.setPosition(x-sprite.getWidth()/2, y-sprite.getHeight()/2);
But I don't see it anywhere
[EDIT 2]
Stage Class
public class Mission1Stage extends Stage{
public static final int VIEWPORT_WIDTH = 20;
public static final int VIEWPORT_HEIGHT = 22;
private World world;
private Ground ground;
private LeftWall leftWall;
private Rocket rocket;
private static final float TIME_STEP = 1 / 300f;
private float accumulator = 0f;
private OrthographicCamera camera;
private Box2DDebugRenderer renderer;
private Viewport viewport;
private SpriteBatch spriteBatch = new SpriteBatch();
private Vector3 touchPoint;
private ShapeRenderer shapeRenderer;
private Button boostButton;
private Skin boostSkin;
private Button boostLeftButton;
private Skin boostLeftSkin;
private Button boostRightButton;
private Skin boostRightSkin;
private Button resetButton;
private Skin resetSkin;
private Game game;
private boolean isTouched = false;
public Mission1Stage(Game game) {
setUpWorld();
renderer = new Box2DDebugRenderer();
shapeRenderer = new ShapeRenderer();
setupCamera();
setUpButtons();
addActor(new Background(ground));
}
private void setUpWorld() {
world = WorldUtils.createWorld();
setUpGround();
setUpRocket();
}
private void setUpGround() {
ground = new Ground(WorldUtils.createGround(world));
addActor(ground);
}
private void setUpLeftWall() {
leftWall = new LeftWall(WorldUtils.createLeftWall(world));
}
private void setUpRocket() {
rocket = new Rocket(WorldUtils.createRocket(world));
addActor(rocket);
}
private void setupCamera() {
camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
viewport = new ExtendViewport(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, camera);
camera.position.set(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2, 0f);
camera.update();
}
private void setUpButtons() {
boostSkin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json"));
boostButton = new Button(boostSkin);
boostButton.setSize(80,80);
boostButton.setPosition(Gdx.graphics.getWidth()-boostButton.getWidth()*2,0);
boostButton.setTransform(true);
boostButton.scaleBy(0.5f);
Gdx.input.setInputProcessor(this);
addActor(boostButton);
boostLeftSkin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json"));
boostLeftButton = new Button(boostLeftSkin);
boostLeftButton.setSize(100, 100);
boostLeftButton.setPosition(0, 0);
addActor(boostLeftButton);
boostRightSkin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json"));
boostRightButton = new Button(boostRightSkin);
boostRightButton.setSize(100, 100);
boostRightButton.setPosition(boostLeftButton.getWidth(), 0);
addActor(boostRightButton);
resetSkin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json"));
resetButton = new Button(resetSkin);
resetButton.setSize(100, 100);
resetButton.setPosition(Gdx.graphics.getWidth()-100, Gdx.graphics.getHeight()-100);
addActor(resetButton);
}
#Override
public void act(float delta) {
super.act(delta);
handleInput();
accumulator += delta;
while(accumulator >= delta) {
world.step(TIME_STEP, 6, 2);
accumulator -= TIME_STEP;
}
}
#Override
public void draw() {
super.draw();
renderer.render(world, camera.combined);
float x = getGround().getBody().getPosition().x;
float y = getGround().getBody().getPosition().y;
float w = getGround().getWidth() * 2;
float h = getGround().getHeight() * 2;
spriteBatch.setProjectionMatrix(getCamera().combined);
Texture texture = new Texture(Gdx.files.internal("ground.png"));
Sprite sprite = new Sprite(texture);
sprite.setSize(w, h);
sprite.setPosition(x-sprite.getWidth()/2, y-sprite.getHeight()/2);
spriteBatch.begin();
sprite.draw(spriteBatch);
spriteBatch.end();
}
public void handleInput() {
if(boostButton.isPressed()) {
rocket.boost();
}
if(boostLeftButton.isPressed()) {
rocket.turnLeft();
}
if(boostRightButton.isPressed()) {
rocket.turnRight();
}
if(resetButton.isPressed()) {
}
}
public boolean resetScreen() {
if(resetButton.isPressed()) return true;
return false;
}
public void updateCamera() {
}
public Ground getGround() {
return ground;
}
public void resize(int width, int height) {
viewport.update(width, height);
camera.position.x = VIEWPORT_WIDTH / 2;
camera.position.y = VIEWPORT_HEIGHT /2;
}
private void translateScreenToWorldCoordinates(int x, int y) {
getCamera().unproject(touchPoint.set(x, y, 0));getCamera();
}
}
Screen class
public class Mission1Screen implements Screen{
private Game game;
private Mission1Stage stage;
private SpriteBatch spriteBatch = new SpriteBatch();
private Skin boostSkin;
private Button boostButton;
public Mission1Screen(Game game) {
this.game = game;
stage = new Mission1Stage(game);
}
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(stage.resetScreen()) {
game.setScreen(new Mission1Screen(game));
}
stage.act(delta);
stage.draw();
stage.updateCamera();
}
#Override
public void resize(int width, int height) {
stage.resize(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}
[EDIT 3]
public class Main extends Game {
#Override
public void create () {
this.setScreen(new Mission1Screen(this));
}
#Override
public void render () {
super.render();
}
#Override
public void dispose () {
}
}
We mostly use Pixel to meter conversion because box2d best works in meters (0-10) but you can avoid this conversion by using small worldwidth and height of your viewport. I mostly prefer 48 and 80 as viewport width and height.
You can use unproject(vector3) method of camera that translate a point given in screen coordinates to world space. I am using this method in touchdown because I get screen coordinate as parameter then I need to convert it into camera world space so that I can generate object at a particular position in world.
public class MyGdxTest extends Game implements InputProcessor {
private SpriteBatch batch;
private ExtendViewport extendViewport;
private OrthographicCamera cam;
private float w=20;
private float h=22;
private World world;
private Box2DDebugRenderer debugRenderer;
private Array<Body> array;
private Vector3 vector3;
#Override
public void create() {
cam=new OrthographicCamera();
extendViewport=new ExtendViewport(w,h,cam);
batch =new SpriteBatch();
Gdx.input.setInputProcessor(this);
world=new World(new Vector2(0,-9.8f),true);
array=new Array<Body>();
debugRenderer=new Box2DDebugRenderer();
vector3=new Vector3();
BodyDef bodyDef=new BodyDef();
bodyDef.type= BodyDef.BodyType.StaticBody;
bodyDef.position.set(0,0);
Body body=world.createBody(bodyDef);
ChainShape chainShape=new ChainShape();
chainShape.createChain(new float[]{1,1,55,1});
FixtureDef fixtureDef=new FixtureDef();
fixtureDef.shape=chainShape;
fixtureDef.restitution=.5f;
body.createFixture(fixtureDef);
chainShape.dispose();
}
#Override
public void render() {
super.render();
Gdx.gl.glClearColor(0,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
world.step(1/60f,6,2);
batch.setProjectionMatrix(cam.combined);
batch.begin();
world.getBodies(array);
for (Body body:array){
if(body.getUserData()!=null) {
Sprite sprite = (Sprite) body.getUserData();
sprite.setPosition(body.getPosition().x-sprite.getWidth()/2, body.getPosition().y-sprite.getHeight()/2);
sprite.setRotation(body.getAngle()*MathUtils.radDeg);
sprite.draw(batch);
}
}
batch.end();
debugRenderer.render(world,cam.combined);
}
#Override
public void resize(int width, int height) {
super.resize(width,height);
extendViewport.update(width,height);
cam.position.x = w /2;
cam.position.y = h/2;
cam.update();
}
private void createPhysicsObject(float x,float y){
float sizeX=2,sizeY=2;
BodyDef bodyDef=new BodyDef();
bodyDef.position.set(x,y);
bodyDef.type= BodyDef.BodyType.DynamicBody;
Body body=world.createBody(bodyDef);
PolygonShape polygonShape=new PolygonShape();
polygonShape.setAsBox(sizeX,sizeY);
FixtureDef fixtureDef=new FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.restitution=.2f;
fixtureDef.density=2;
body.createFixture(fixtureDef);
body.setFixedRotation(false);
polygonShape.dispose();
Sprite sprite=new Sprite(new Texture("badlogic.jpg"));
sprite.setSize(2*sizeX,2*sizeY);
sprite.setPosition(x-sprite.getWidth()/2,y-sprite.getHeight()/2);
sprite.setOrigin(sizeX,sizeY);
body.setUserData(sprite);
}
#Override
public void dispose() {
batch.dispose();
debugRenderer.dispose();
world.dispose();
}
#Override
public boolean keyDown(int keycode) {
return false;
}
#Override
public boolean keyUp(int keycode) {
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
vector3.set(screenX,screenY,0);
Vector3 position=cam.unproject(vector3);
createPhysicsObject(vector3.x,vector3.y);
return false;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return false;
}
}
I have created a 10x10 grid, and now would like to place an image of the player inside one of the grid squares. to do this I think i have to use bitmap? I will put all my code below. to try and do this anyway I created a game class which sets and gets the x position of the player image. I then created a player class, which is where I try to add the image, using Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sokobanplayer1);
i keep getting an error under getResources so i created a get resources method in game, but it didnt fix the problem. could someone please show me how to do this, my code is below.
//DRAW CLASS
public class Draw extends View {
Paint red = new Paint();
Paint green = new Paint();
int rectSide = 1000;
public Draw(Context context) {
super(context);
red.setColor(Color.RED);
green.setColor(Color.GREEN);
red.setStrokeWidth(8);
green.setStrokeWidth(8);
}
public void drawGrid(Canvas canvas) {
int width = canvas.getWidth();
int height = canvas.getHeight();
float startX = (width / 2) - (rectSide / 2);
float stopX = (width / 2) + (rectSide / 2);
float startY = (height / 2) - (rectSide / 2);
float stopY = (height / 2) + (rectSide / 2);
for (int i = 0; i < 1100; i += 100) {
float newY = startY + i;
canvas.drawLine(startX, newY, stopX, newY, green);
}
for (int i = 0; i < 1100; i += 100) {
float newX = startX + i;
canvas.drawLine(newX, startY, newX, stopY, green);
}
}
#Override
public void onDraw(Canvas canvas) {
drawGrid(canvas);
}
}
//GAME CLASS
public class Game {
Bitmap image;
int x;
int y;
int height;
int width;
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public int getHeight(){
return height;
}
public int getWidth(){
return width;
}
public Bitmap getResources(){
return image;
}
}
//PLAYER CLASS
public class Player extends Game {
public Player(Bitmap res, int w, int h){
image = res;
x = 300;
y = 300;
height = h;
width = w;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sokobanplayer1);
}
}
//MAIN ACTIVITY
public class MainActivity extends Activity {
Draw draw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
draw = new Draw(this);
draw.setBackgroundColor(Color.BLUE);
setContentView(draw);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Replace your Player class with this:
//PLAYER CLASS
class Player extends Game {
public Player(Context context, int w, int h){
x = 300;
y = 300;
height = h;
width = w;
image = BitmapFactory.decodeResource(context.getResources(), R.drawable.sokobanplayer1);
}
}
I have implemented zoom on my custom made view which draws a grid onto a canvas, but the zooming won't work if I put both fingers on the screen simultaneously. I have to first put one, then the other.
I followed this blog post by Adam Powell to implement zoom, and made this custom view:
public class ZoomView extends View{
private float width;
private float height;
Paint lineColor;
Paint bgColor;
private float mScaleFactor;
private ScaleGestureDetector scaleDetector;
public CanvasView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
lineColor = new Paint();
lineColor.setColor(getResources().getColor(R.color.white));
bgColor = new Paint();
bgColor.setColor(getResources().getColor(R.color.black));
mScaleFactor = 1;
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh){
width = w/9;
height = h/9;
super.onSizeChanged(w,h,oldw,oldh);
}
#Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.save();
canvas.scale(mScaleFactor, mScaleFactor); //for zooming
for(int i=0; i <= 9; i++){
canvas.drawLine(0,i*height, 9*width,i*height,lineColor);
canvas.drawLine(i*width, 0, i*width, 9*height, lineColor);
}
canvas.restore();
}
#Override
public boolean onTouchEvent(MotionEvent ev){
mScaleDetector.onTouchEvent(ev);
return true;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
setMeasuredDimension(800, 1000);
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
#Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
invalidate();
return true;
}
}
}
So why is the zooming behaving this way?
Any help would be greatly appreciated. Thanks!
This may look like a silly question, but does the multi touch behavior work on other apps (Maps for example) at this device?
I've tested your code (with minor changes, see below) on an LG Optimus Black, Android 4.0.3, and it worked, both fingers simultaneously.
package com.example.teste;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
public class ZoomView extends View {
private float width;
private float height;
Paint lineColor;
Paint bgColor;
private float mScaleFactor;
private ScaleGestureDetector mScaleDetector;
public ZoomView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
lineColor = new Paint();
lineColor.setColor(getResources().getColor(R.color.ics_blue_bright));
bgColor = new Paint();
bgColor.setColor(getResources().getColor(R.color.black));
mScaleFactor = 1;
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
width = w / 9;
height = h / 9;
super.onSizeChanged(w, h, oldw, oldh);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.scale(mScaleFactor, mScaleFactor); // for zooming
for (int i = 0; i <= 9; i++) {
canvas.drawLine(0, i * height, 9 * width, i * height, lineColor);
canvas.drawLine(i * width, 0, i * width, 9 * height, lineColor);
}
canvas.restore();
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
mScaleDetector.onTouchEvent(ev);
return true;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(800, 1000);
}
private class ScaleListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
#Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
invalidate();
return true;
}
}
}