2011-08-03 5 views
1

필자는 안드로이드 앱을 위해 무엇을 만들어야하는지 토론하고있는 Java 클래스를 가지고 있습니다. 내가 액티비티, 뷰 또는 둘 모두인지 여부를 불확실하다.Android보기 또는 활동?

필자가 확신 할 수없는 이유는 매개 변수, 구성 요소 수신기 및 생성자가있는 생성자를 가지고 있으며 캔버스에서 그립니다.

보기로 만들려고 할 때 생성자에 문제가 발생했습니다. 어떤 액티비티의 경우, 두 요소 (즉, 컴포넌트 크기 조정, 색상 변경 등) 사이에서 상호 작용하는 방법을 확신 할 수 없습니다. 논리적으로 볼 때 가장 좋은 방법은보기와 활동을 혼합하는 것입니다. 나는이 사고에 오히려 새로운 것이므로 마음에 두십시오.

몇 가지 Android 앱을 만든 사람들이 이전 Java 클래스가 무엇인지 결정하는 방법에 대해 궁금합니다. 내 수업은 어느쪽으로 든 갈 수있는 것 같습니다. 다음은

내 클래스의 일부이다 (자바, 그러나 나는 또한 안드로이드로 변환이) :

public class GraphDisplay extends JPanel implement Serializable { 

    public static final int BLUE_RED   = 0; 
    public static final int BLACK_WHITE   = 1; 

    protected int displayBoard; 
    protected int centerXcoord; 
    protected int centerYcoord; 

    private GeneralPath circlePath; 
    private GeneralPath rectPath; 

    private Color triangleColor; 
    private Color rectColor; 

    /* 
    * Create an instance of GraphDisplay using the default 
    * blue/red display 
    */ 
    public GraphDisplay() { 
     this(BLUE_RED, 0); 

    } 

    /* 
    * Create an instance of GraphDisplay using the board value 
    * and index of 0. Throws exception if model isn't a 
    * prederfined value 
    */ 
    public GraphDisplay(int board) { 
     this(model, 0); 

    } 



    public GraphDisplay(int board, int index) throws IllegalArgumentException { 
     if(board == BLUE_RED || board == BLACK_WHITE) { 
      displayBoard = board; 
      this.index = index; 

      if(displayBoard == BLUE_RED) { 
       setBackground(Color.blue); 
       triangleColor = Color.red; 
       rectColor = Color.red; 
      } else { 
       setBackground(Color.black); 
       triangleColor = Color.white; 
       rectColor = Color.white; 
      } 

      compListener = new GraphListener(); 
      addGraphListener(compListener); 

      mListener = new GraphMouseListener(); 
      addMouseMotionListener(mListener); 

      trianglePath = new GeneralPath(); 
      rectPath = new GeneralPath(); 

      setMinimumSize(new Dimension(400, 400)); 
      setPreferredSize(new Dimension(400,400)); 

     } else { 
      throw new IllegalArgumentException("Improper model") 
     } 
    } 


    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D)g; 

     centerXcoord = getWidth()/2; 
     centerYcoord = getHeight()/2; 

     drawTriangle(g2); 
     drawCircle(g2); 

    } 

    private void drawTriangle(Graphics2D g2) { 
     //code for drawing triangle with lines 
    } 

    private void drawRectangle(Graphics2D g2) { 
     //code for drawing circle 
    } 

    public void setTriangleColor(Color color) { 
     triangleColor = color; 
     repaint(); 
    } 

    public Color getTriangleColor() { 
     return triangleColor; 
    } 

    //Omitted other getters/setter for brevity. 
    //The above get/set methods are a variety of what the class entails 

    protected class GraphMouseMotionListener extends MouseMotionAdapter { 
     public void mouseMoved(MouseEvent me) { 
      int x = mouseEvent.getX(); 
      int y = mouseEvent.getY(); 

      //if in component display message 
     } 
    } 

    protected class GraphComponentListener extends ComponentsAdapter { 
     public void componentResized(ComponentEvent ce) { 
      super.componentResized(ce); 
     } 
    } 


} 

그래서 제 질문은 당신이 만들 것입니다 그것은, 뷰에보기를 그리는 활동 , 또는 다른 것? 너무 많은 문제가 아니라면, 당신이 언급 한 것처럼 왜 그렇게하는지 자세히 설명해 주실 수 있습니까? (액티비티와 뷰 사이에서이를 분할하는 경우 간단한 예제를 제공 할 수 있지만 클래스를 포함 할 필요는 없습니다. 개념을 이해하기 위해)

답변

3

생성 한 뷰를 사용할 수있는 Activity을 사용합니다

귀하의 활동 클래스 :

public class YourClass extends Activity { 

    private Button exampleButton; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     exampleButton = (Button)findViewById (R.id.myButton); 

     exampleButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(YourClass.this, "I did something", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 
} 

onCreate 방법 다음 클래스 Activity을 확장하고하여 XML 파일에 당신은 예를 들어 setContentView(R.layout.your_view);

말 UR XML보기는 :

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <TextView android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Hello World" /> 

     <Button android:id="@+id/myButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 

    </LinearLayout> 

그래서 XML는 등등 배치 및 방법, 당신이보기를 정의 할 경우, 어떤 부품이 될 것입니다.

그런 다음 Activity 클래스는보기를 실제로 사용하고 기능을 추가하는 곳입니다. 프로그램 적으로보기에 구성 요소를 추가 할 수도 있지만 지금은 그 부분으로 들어가지 않을 것입니다.

그런 다음 오버레이를 조사하여 아이디어를 얻은 다음 해당 상황에 적용 할 수있는 오버레이를 조사 할 수 있습니다 (일부 예제는지도 오버레이를 통해 활동에서 캔버스에 그려지는 방법을 보여줍니다). 예 : http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/ItemizedOverlay.html

희망이 있습니다.

+0

생성자는 무엇을 할 것입니까? 또한 캔버스와 액티비티를 액티비티 (@onDraw를 오버라이드)에서 가져온 것으로 가정하여 올바른 것인가? – StartingGroovy

+0

예 onDraw를 재정의하면 작동합니다. 생성자에 대해서는 해당 정보를 보유하는 별도의 Java 클래스를 만든 다음 onDraw에서 호출하거나 필요한 곳에서 호출 할 수 있습니다. 또는 onCreate 메소드에서 onCreate 메소드를 정의하려고 시도 할 수 있습니다. onCreate 메소드는 액티비티 생성자와 비슷하지만, 그 중 하나만 가질 수는 있지만 그 안에 너무 많은 작업을 수행 할 수 있으므로 이론적으로는 onCreate에서 사용할 수있는 케이스를 사용할 수 있습니다 하나는 '생성자'이고 다른 하나는 생성자입니다. – Wolfcow

+0

무슨 뜻인지 알려주시겠습니까? 예를 들어, 위의 코드에서 두 개의 다른 보드 유형과 인덱스가 내 생성자의 매개 변수임을 알 수 있습니다.내가 인수를 어떻게 통과 시킬지 나는 확신 할 수 없다. 아마도 내가 지정하지 않으면 생성자가 기본값을 사용하도록 설정해야할까요? 액티비티는 지정된 View에 대해 onDraw에 어떻게 액세스합니까? 캔버스 및 드로잉에 관한 – StartingGroovy

관련 문제