현재 내 색상 자원 xml에서 ContextCompact.getColor()
을 사용하여 내 앱에 색상을 넣으려고하고 있지만 어떤 이유로 단일 컨텍스트 버전을 전달할 수 없습니다.클래스에서 컨텍스트를 사용할 수 없습니까?
클래스를 처리기로 사용하고 있으므로 작업에서 전달하려고하지 않습니다. 내 활동에서 나는 그들을 사용할 수 있지만, 내 수업에서 나는 getActivityContext()
this
등을 통과시킬 수 없다. 아무 것도 작동하지 않는다. 나는 무엇을해야합니까?
또한 캔버스에 색상을 추가하여 xml에 색상을 추가 할 수 없습니다.
canvas.drawColor(Color.BLACK);
내가 현재 사용하고있는 것입니다. 내 XML에서 색상으로 바꿀 싶어요. (나는 캔버스의 배경을 근본적으로 설정하려고 노력하고있다.)
내 수업의 전체 코드 : (나는이 응용 프로그램을 "메모"응용 프로그램으로 만들어서 향후 프로젝트에서 다시 볼 수있다. 나는 종류의 해결 방법을 함께했다
public class GameHandling {
private SurfaceHolder holder;
private Resources resources;
private int screenWidth;
private int screenHeight;
private Ball ball;
private Bat player;
private Bat opponent;
public GameHandling(int width, int height, SurfaceHolder holder, Resources resources){
this.holder = holder;
this.resources = resources;
this.screenWidth = width;
this.screenHeight = height;
this.ball = new Ball(screenWidth, screenHeight, 400, 400);
this.player = new Bat(screenWidth, screenHeight, 0, 0, Bat.Position.LEFT);
this.opponent = new Bat(screenWidth, screenHeight, 0, 0, Bat.Position.RIGHT);
}
// setting the ball images to be drawn
public void inIt(){
Bitmap ballShadow = BitmapFactory.decodeResource(resources, R.mipmap.grey_dot);
Bitmap ballImage = BitmapFactory.decodeResource(resources, R.mipmap.red_dot);
Bitmap batPlayer = BitmapFactory.decodeResource(resources, R.mipmap.bat_player);
Bitmap batOpponent = BitmapFactory.decodeResource(resources, R.mipmap.bat_opponent);
ball.inIt(ballImage, ballShadow, 2, 0);
player.inIt(batPlayer, batPlayer, 0, 0);
opponent.inIt(batOpponent, batOpponent, 0, 0);
}
// calling Balls update method to update the ball
public void update(long elapsed){
ball.update(elapsed);
}
public void draw(){
Canvas canvas = holder.lockCanvas(); // Making a canvas object to draw on - .lockcanvas locks canvas
if(canvas != null) {
// draw in area between locking and unlocking
canvas.drawColor(Color.BLACK);
ball.draw(canvas);
player.draw(canvas);
opponent.draw(canvas);
holder.unlockCanvasAndPost(canvas); //-unlockcanvasandposts unlocks the canvas
}
}
}
getApplicationContext()
당신이 무엇을 의미합니까 호출 컨텍스트를 통과'당신은 pass' 수없는 이유는 무엇입니까? 설계 상인가요? 아니면 오류입니까? 오류 일 경우 코드를 게시하고 문제가있는 경우 –나는 그것을 부르는 것을 의미합니다. 그래서 getContext() 등을 호출 할 수 없습니다. – CarbonZonda
생성자에서 'Context'를 전달하십시오. 귀하의 컨트롤러 클래스에서 코드를 게시 –