我嘗試在Android工作室canvas.draw和掙扎後的幾天裏繪製圖像,我finnaly繪製圖像,但...Android和帆布畫 - 格雷和錯誤大小的圖像
我的圖像具有奇怪的顏色,它非常非常小。
我的Sprite類(現在我沒有做任何動畫)
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; }
}
我的觀點:
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();
}
}
我的主要活動:
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();
}
}
我可以」不要放任何截圖,我在我的設備上測試,因爲我的電腦不支持它說的東西。
編輯:顏色問題來自我的資產,這是沒有正確導入所有dpi的變化。 Android Studio從我的圖像中創建的一些圖像已損壞。**
顏色問題已解決,但尺寸仍在發生變化。
我試圖把200,200.它只是將其移動到200,200。該功能是好的。 –