2012-12-24 22 views
0

怎麼樣去傢伙。我現在要做的就是增加屏幕上的文字,我現在在屏幕上。我在畫布上有一個位圖,每次點擊時都會增加數字。我將這個變量打印到logcat中,它確實是遞增的,但它並沒有被繪製在屏幕上。如何增加Android應用程序中的畫布文本?

這裏是什麼,我現在有更好的主意圖片:

enter image description here

這裏是我的繪畫課:

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); 


} 
} 

這是我的主要活動類:

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; 
    } 



} 

任何幫助將非常感激。

+0

你已經用'setContentView(...)'用''Drawing'的'view'實例,但是在'onTouch(...)'監聽器中使用'count = new Drawing(this);'。是什麼讓你覺得會更新屏幕? – Squonk

+0

好點。我只是想盡一切辦法。對如何更新視圖有任何建議嗎? – recheej

回答

0

您應該爲您的繪圖類添加更新方法,然後更新在該方法中繪製的數字。您可能還需要使您的Drawing類已經繪製的區域無效,讓新的數字出現在屏幕上(以強制您的onDraw再次被調用)。

就像Squonk說的那樣,你現在有2個Drawing類的實例,一個在屏幕布局中(只有「view」是)。

除此之外,我會添加一個ImageButton來顯示機器人,然後添加一個TextView來顯示數字並將這些元素添加到活動的佈局中。當單擊ImageButton時,我只需將TextView的Text屬性設置爲下一個數字。編寫一個自定義的View對象只有在渲染一些你不能從開箱即用的組件中渲染的東西時才需要。

+0

更改編號後,您應該調用View.invalidate()方法,以使操作系統再次調用onDraw方法。 – Ameen

+0

是的,謝謝你,完美的工作。但至於你的第二點。你可以把一個畫布上的TextView? textview更有效率嗎? – recheej

+0

您是否問一個TextView和Canvas是否可以在視覺上相互放置?如果是這樣,根據您選擇的佈局類型,答案是肯定的。查看http://developer.android.com/guide/topics/ui/declaring-layout.html瞭解佈局的一些基本信息。 – Ameen

相關問題