Reputation: 51
I try to draw an image with canvas.draw on Android Studio and after few days of struggling, I finnaly draw an image but...
My image has a weird color and it's very very tiny.
My sprite class (for now I don't do any animations with )
package com.framework.game;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.util.Map;
import java.util.HashMap;
import android.R;
import android.content.Context;
/**
* Created by Tong3 on 01/08/2017.
*/
public class Sprite {
private Bitmap image;
private int x, y;
boolean valid;
Sprite(Context context, int R, int X, int Y){
x = X;
y = Y;
image = BitmapFactory.decodeResource(context.getResources(), R);
if(image != null)
valid = true;
else
valid = false;
}
public Bitmap getImage(){ return image; }
public int getX(){ return x; }
public int getY(){ return y; }
public int getWidth(){ return getImage().getWidth(); }
public int getHeight(){ return getImage().getHeight(); }
public boolean isValid(){ return valid; }
}
My view :
package com.framework.game;
import android.graphics.Matrix;
import android.view.SurfaceView;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.content.Context;
import com.example.tong3.tdp.R;
import java.lang.Runnable;
import static com.example.tong3.tdp.R.drawable.testplayer;
/**
* Created by Tong3 on 01/08/2017.
*/
public class GameView extends SurfaceView implements Runnable {
volatile boolean running;
private Thread mainThread = null;
private int FRAME_RATE = 17;
private Canvas canvas;
private Paint paint;
private SurfaceHolder holder;
private Sprite test;
public GameView(Context context){
super(context);
holder = this.getHolder();
test = new Sprite(context, R.drawable.testplayer, 100, 100);
}
@Override
public void run(){
while(running){
update();
draw();
control();
}
}
private void update(){
}
private void draw(){
if(holder.getSurface().isValid() && test.isValid()) {
canvas = holder.lockCanvas();
canvas.drawColor(Color.BLACK);
//TEST
canvas.drawBitmap(test.getImage(), test.getX(), test.getY(), paint);
holder.unlockCanvasAndPost(canvas);
}
}
private void control(){//FPS CONTROL ~60
try {
mainThread.sleep(FRAME_RATE);
}catch(InterruptedException e){
e.printStackTrace();
}
}
public void pause(){
running = false;
try {
mainThread.join();
}catch(InterruptedException e){
}
}
public void resume(){
running = true;
mainThread = new Thread(this);
mainThread.start();
}
}
My main activity :
package com.framework.game;
import android.app.Activity;
import android.os.Bundle;
import com.example.tong3.tdp.R;
public class MainActivity extends Activity{
private GameView gameView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameView = new GameView(this);
setContentView(gameView);
}
@Override
protected void onPause(){
super.onPause();
gameView.pause();
}
@Override
protected void onResume(){
super.onResume();
gameView.resume();
}
}
I can't put any screenshots, I test on my device because my computer is not supporting something it says.
EDIT : The color problem was coming from my assets which was not correctly imported for all dpi's variations. Some of the images created by Android Studio from my image were corrupted.**
The color problem is sovled but the size one is still happening.
Upvotes: 0
Views: 908
Reputation: 51
The problem was coming from my assets which was not correctly imported for all dpi's variations. Some of the images created by Android Studio from my image were corrupted. I had to put the image in drawable folder to prevent android from scaling my image according to the device's dpi.
Upvotes: 0
Reputation: 537
paint.setColor(Color.BLACK)
canvas.drawBitmap(test.getImage(), test.getX(), test.getY(), paint);
This Paint is for drawing your bitmap.
canvas.drawBitmap(bitmap, x, y, paint);
x: The position of the left side of the bitmap being drawn
y: The position of the top side of the bitmap being drawn
So you actually draw your bitmap in the position(100,100). It does not decide the size of your bitmap.
Look at the official Document here.
Upvotes: 1