0

私は持っています、SurfaceViewそして私はそのBitmap Logocanvasを可動にしたいです

私が間違っているのは何ですか?

static float x, y;
Bitmap logo;

SurfaceView ss = (SurfaceView) findViewById(R.id.svSS);   
    logo = BitmapFactory.decodeResource(getResources(), R.drawable.logo);

    x = 40;
    y = 415;
    ss.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent me) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        switch(me.getAction()) {
        case MotionEvent.ACTION_DOWN:
            x = me.getX();
            y = me.getY();
            break;
        case MotionEvent.ACTION_UP:
            x = me.getX();
            y = me.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            x = me.getX();
            y = me.getY();
            break;
            }
        return true;
        }
    });

public class OurView extends SurfaceView implements Runnable{

    Thread t = null;
    SurfaceHolder holder;
    boolean isItOK = false;


    public OurView(Context context) {
        super(context);
        holder = getHolder();

    }
    public void run (){
        while (isItOK == true){
            //canvas DRAWING
            if (!holder.getSurface().isValid()){
                continue;
            }
            Canvas c = holder.lockCanvas();
            c.drawARGB(255, 200, 100, 100);
            c.drawBitmap(logo, x,y,null);
            holder.unlockCanvasAndPost(c);
        }
    }
    public void pause(){
        isItOK = false;
        while(true){
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            break;
        }
        t = null;
    }
    public void resume(){
        isItOK = true;
        t = new Thread(this);
        t.start();

    }
}

これで、表面ビューは黒になります。色が付いていない場合も何も起こりません。200, 100, 100

4

1 に答える 1

0

onDraw(Canvas c)クラスにメソッドを実装するのを忘れて、クラス内OurViewを移動する可能性がありますonTouchEvent

クラス構造は次のようになります。

public class OurView extends View implements Runnable {

//...your runnable stuff here
//...Runnable stuff means your run(), pause() etc.

  public OurView(Context context) {
    super(context);
    //your constructor stuff here
    // your constructor, do you find the similar stuff to this you wrote? That's your constructor, so you can just add:
    holder = getHolder();
}

protected void onDraw (Canvas c) {

    c.drawBitmap(bitmap, x, y);
//set colour, draw bitmap here, onDraw() will be called automatically, so just call invalidate(); when you need to "refresh" the view
}

public boolean onTouchEvent(MotionEvent e) {
    float x = e.getX();
    float y = e.getY();

    swith(e.getAction()) {
    case MotionEvent.ACTION_DOWN:
     ...
    }
}
}

より明確な例とリファレンスについては、次を参照してください。

カスタムビューの実装

カスタムコンポーネント

それがあなたを助けることができることを願っています。

于 2012-10-12T20:04:15.400 に答える