0

調子はどうだい。私が今やろうとしているのは、現在画面に表示されているこのキャンバス テキストをインクリメントすることです。キャンバスにビットマップがあり、クリックするたびに数値が増加します。変数をlogcatに出力しましたが、実際には増加していますが、画面に描画されていません。

より良いアイデアのために、私が今持っているものの写真を次に示します。

ここに画像の説明を入力

これが私の描画クラスです:

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



}

どんな助けでも大歓迎です。

4

1 に答える 1

0

Drawing クラスに更新メソッドを追加してから、そのメソッドでペイントする数値を更新する必要があります。おそらく、 Drawing クラスが描画した領域を無効にして、新しい番号が画面に表示されるようにする必要もあります (onDraw を強制的に再度呼び出すため)。

Squonk が言ったように、現在 Drawing クラスのインスタンスが 2 つあり、そのうちの 1 つは画面のレイアウトにあります (「ビュー」のみ)。

これとは別に、ImageButton を追加してロボットを表示し、次に TextView を追加して数字を表示し、それらの要素をアクティビティのレイアウトに追加します。ImageButton がクリックされたら、TextView の Text プロパティを次の数値に設定するだけです。カスタマイズされた View オブジェクトを記述する必要があるのは、すぐに使用できるコンポーネントから構成できないものをレンダリングする場合のみです。

于 2012-12-24T22:58:41.427 に答える