2016-12-11 9 views
2

私は現在、色リソース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 
     } 


    } 
} 
+1

のいずれかの呼び出しコンテキストを渡しますか?それは設計によるのですか、それともエラーですか?それがエラーであれば、あなたのコードを投稿して、あなたが持っている問題を –

+0

私はそれを呼び出すことを意味します。だから私はgetContext()等を呼び出すことはできません – CarbonZonda

+0

コンストラクタで 'Context'を渡してください。コントローラクラスのコードを投稿してください –

答えて

2

変更に私の問題、あなたのコンストラクタを解決し、ContextCompat.getColor(context,...パターンを使用していないので、それは、まだ回避策ですが

。あなたは(活動/フラグメント)は、このクラスを作成しているどこ

、あなたはあなたがpass`することはできません `何を意味するかgetActivity()またはgetApplicationContext()

new GameHandling(getActivity()/getApplicationContext(), ...)

public GameHandling(Context context, int width, int height, SurfaceHolder holder, Resources resources){ 
    this.context = context; 
    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); 
} 
+1

ITは働いた!どうもありがとうございます! – CarbonZonda

+0

コンストラクタにコンテキストを渡している場合は、実際にリソースを渡す必要はありません。 context.getResources()を使用してコンテキストからリソースを取得できます。 – androholic

0

は、私は私の望ましい色でイメージを作成し、アプリの背景としてそれを使用しました。それはこれに

関連する問題