2012-12-24 8 views
0

안녕하세요. 내가 지금하고있는 일은 캔버스 텍스트를 화면에 올리는 것입니다. 클릭 할 때마다 번호를 증가시켜야하는 캔버스에 비트 맵이 있습니다. 변수를 logcat에 인쇄했는데 실제로 증가하지만 화면에 그려지지 않습니다.Android 앱에서 캔버스 텍스트를 늘리는 방법은 무엇입니까?

package com.example.touchperson; 

import android.os.Bundle; 
import android.app.Activity; 

import android.util.Log; 
import android.view.Menu; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 



public class MainActivity extends Activity implements OnTouchListener { 


    Drawing view; 
    Drawing count; 





    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     view = new Drawing(this); 
     view.setOnTouchListener(this); 
     setContentView(view); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 


    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     // TODO Auto-generated method stub 

     switch(event.getAction()){ 
     case MotionEvent.ACTION_DOWN: 

      if(view.contains((int) event.getX(), (int) event.getY())){ 

       Drawing.touchCount++; 
       count = new Drawing(this); 
       Log.i("touched", Integer.toString(Drawing.touchCount)); 
      } 
      break; 


     } 
     return false; 
    } 



} 

:

가 여기 내 주요 활동 클래스의
package com.example.touchperson; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Rect; 

import android.util.Log; 
import android.view.View; 

public class Drawing extends View { 

    Bitmap robot; 
    Rect rec; 
    Paint text; 
    static int touchCount = 0; 
    public Drawing(Context context) { 
     super(context); 
     robot = BitmapFactory.decodeResource(getResources(), R.drawable.character); 
     Log.i("Robot coord", Integer.toString(robot.getHeight()));; 
     rec = new Rect(0, 0, 200 , 200); 
     text = new Paint(); 
     text.setColor(Color.CYAN); 
     text.setTextSize(100); 

    } 


    public boolean contains(int x, int y){ 

     if(rec.contains(x, y)){ 
      return true; 

     } 
     else{ 
      return false; 
     } 



    } 



    @Override 
    protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    canvas.drawColor(Color.WHITE); 
    canvas.drawBitmap(robot, rec, rec, null); 
    canvas.drawText(Integer.toString(touchCount) ,(int) (canvas.getWidth()/2) , (int) (canvas.getHeight()/2), text); 


} 
} 

:

enter image description here

여기 내 드로잉 클래스의 : 여기

내가 더 좋은 생각이 지금 무슨의 사진입니다 어떤 도움이라도 대단히 감사 할 것입니다.

+0

'setContentView (...) '로'Drawing'의'view' 인스턴스를 사용했지만'onTouch (...)'리스너에서는'count = new Drawing (this);'을 사용합니다. 그게 화면을 업데이 트한다고 생각하게 만드는 이유는 무엇입니까? – Squonk

+0

좋은 지적. 내가 할 수있는 일을하는 중이 었어. 보기를 업데이트하는 방법에 대한 제안 사항이 있습니까? – recheej

답변

0

드로잉 클래스에 업데이트 메서드를 추가 한 다음 해당 메서드에서 그리는 번호를 업데이트해야합니다. 또한 드로잉 클래스가 그린 영역을 무효화하여 새 번호가 화면에 표시되도록해야합니다 (onDraw를 다시 호출하도록 함).

Squonk이 말했듯이 지금은 Drawing 클래스의 인스턴스가 2 개 있고 화면 레이아웃에 하나만 있습니다 ("보기"만 있음).

이외에도 ImageButton을 추가하여 로봇을 표시 한 다음 TextView를 사용하여 번호를 표시하고 해당 요소를 활동 레이아웃에 추가합니다. ImageButton을 클릭하면 TextView의 Text 속성을 다음 번호로 설정하기 만하면됩니다. 사용자 지정된 View 개체를 작성하는 것은 기본 구성 요소로 작성할 수없는 것을 렌더링 할 때만 필요합니다.

+0

숫자를 변경 한 후, View.invalidate() 메소드를 호출하여 OS가 onDraw 메소드를 다시 호출하도록해야합니다. – Ameen

+0

네, 완벽하게 작동했습니다. 그러나 당신의 두번째 점까지. Textview에 캔버스를 넣을 수 있습니까? textview가 더 효율적입니까? – recheej

+0

TextView와 Canvas를 서로 시각적으로 연결할 수 있는지 묻고 있습니까? 그렇다면 대답은 선택한 레이아웃 유형에 따라 예입니다. 레이아웃에 대한 기본 정보는 http://developer.android.com/guide/topics/ui/declaring-layout.html을 참조하십시오. – Ameen

관련 문제