2012-07-25 1 views
0

나는 게임에서 배경으로 사용할 전화에서 자신의 이미지를 선택할 수있는 기능을 포함하도록 코드를 수정하고있다. 현재 작업중인 파일에는 GameActivityGameView이라는 두 클래스가 있습니다. GameActivity에있는 활동을 통해 휴대 전화의 갤러리가 열리고 화면에서 버튼을 누르면 이미지를 선택할 수 있습니다. 이미지의 위치는 Drawable 객체의 경로로 전달된다고 가정합니다. setBackgroundDrawable() 함수는 View 클래스 (GameView 확장)의 일부이므로 이미지 선택을위한 활동 결과 함수에서 직접 액세스 할 수 없습니다. 원래 setBackgroundDrawable()GameView's 생성자의 일부입니다.클래스 외부의 드로어 블 배경 새로 고침

public GameView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    requestFocus(); 


    mDrawableBg = getResources().getDrawable(R.drawable.lib_bg);  //Mod 1 

    setBackgroundDrawable(mDrawableBg); 

    mBmpPlayer1 = getResBitmap(R.drawable.lib_cross); 
    mBmpPlayer2 = getResBitmap(R.drawable.lib_circle); 

    if (mBmpPlayer1 != null) { 
     mSrcRect.set(0, 0, mBmpPlayer1.getWidth() -1, mBmpPlayer1.getHeight() - 1); 
    } 

    mBmpPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 

    mLinePaint = new Paint(); 
    mLinePaint.setColor(0xFFFFFFFF); 
    mLinePaint.setStrokeWidth(5); 
    mLinePaint.setStyle(Style.STROKE); 

    mWinPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    mWinPaint.setColor(0xFFFF0000); 
    mWinPaint.setStrokeWidth(10); 
    mWinPaint.setStyle(Style.STROKE); 

    for (int i = 0; i < mData.length; i++) { 
     mData[i] = State.EMPTY; 
    } 

    if (isInEditMode()) { 
     // In edit mode (e.g. in the Eclipse ADT graphical layout editor) 
     // we'll use some random data to display the state. 
     Random rnd = new Random(); 
     for (int i = 0; i < mData.length; i++) { 
      mData[i] = State.fromInt(rnd.nextInt(3)); 
     } 
    } 
} 

내가 GameActivity에서 배경을 조절할 수있는 방법이 있나요 : 여기

생성자입니까?

답변

0

예, GameActivity 클래스에서 mDrawableBg를 설정할 메서드를 설정하십시오.

public void setBackground(URL url){ 
GameViewClassInstance.mDrawableBg = url 
} 
+0

이것은 작동하지 않으며 mDrawableBg를 설정해도 아무런 변화가 없습니다. setBackgroundDrawable() 함수는 내가 누락 된 것이 없으면 여전히 호출되어야합니다. – Mike

+0

ClassInstanceName.VariableOrFunction을 사용하여 다른 클래스의 변수 및 함수에 액세스 할 수 있습니다. –