1

Android開発は初めてです。ユーザーが画面に触れるたびにビットマップ画像を描画しようとしています。画面上で発生するタッチ イベントの x と y の位置を取得できました。しかし、以下のプログラムがキャンバスに画像を描画しない理由がわかりません。

ここに私が取り組んでいるコードがあります:

public class SurfaceViewTest extends Activity implements OnTouchListener {      
FastRenderView renderView;   
Bitmap bob565;
Bitmap bob4444;
float x,y;
Canvas canvas = new Canvas();

public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
    renderView = new FastRenderView(this);
    setContentView(renderView);
}   


protected void onResume() {
    super.onResume();
    renderView.resume();
}

protected void onPause() {
    super.onPause();         
    renderView.pause();
}    


public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    ;
switch (event.getAction()) {

    case MotionEvent.ACTION_DOWN:
        x = event.getX();
        y = event.getY()
        canvas.drawBitmap(bob4444, x, y, null);
        break;

    }
    return true;
}




class FastRenderView extends SurfaceView implements Runnable {
    Thread renderThread = null;
    SurfaceHolder holder;
    volatile boolean running = false;

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

        try {
            AssetManager assetManager = context.getAssets();
            InputStream inputStream = assetManager.open("bobrgb888.png");
            bob565 = BitmapFactory.decodeStream(inputStream);
            inputStream.close();
            Log.d("BitmapText",
                    "bobrgb888.png format: " + bob565.getConfig());

            inputStream = assetManager.open("bobargb8888.png");
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_4444;
            bob4444 = BitmapFactory.decodeStream(inputStream, null, options);

            inputStream.close();
            Log.d("BitmapText",
                    "bobargb8888.png format: " + bob4444.getConfig());

        } catch (IOException e) {

        } finally {
            // we should really close our input streams here.
        }
    }


    public void resume() {          
        running = true;
        renderThread = new Thread(this);
        renderThread.start();         
    }      

    public void run() {
        while(running) {  
            if(!holder.getSurface().isValid())
                continue;

            Canvas canvas = holder.lockCanvas();            
            canvas.drawRGB(0, 0, 255);    
            canvas.drawBitmap(bob4444, x, y, null);
            holder.unlockCanvasAndPost(canvas);   



        }
    }

    public void pause() {                        
        running = false;                        
        while(true) {
            try {
                renderThread.join();
                return;
            } catch (InterruptedException e) {
                // retry
            }
        }      


       }        
    }
}
4

1 に答える 1

2

どうでも。私は自分でそれを理解しました。ビューにオンタッチリスナーを設定するのを忘れていました。変更後の最終的なコードは次のとおりです。

public class SurfaceViewTest extends Activity implements OnTouchListener{      
    FastRenderView renderView;            
    float x, y;
    Bitmap bob565, bob4444;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
    renderView = new FastRenderView(this);
    renderView.setOnTouchListener(this);
    setContentView(renderView);
}      



protected void onResume() {
    super.onResume();
    renderView.resume();
}

protected void onPause() {
    super.onPause();         
    renderView.pause();
}    

class FastRenderView extends SurfaceView implements Runnable {
    Thread renderThread = null;
    SurfaceHolder holder;
    volatile boolean running = false;

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


        try{
        AssetManager assetManager = context.getAssets();
        InputStream inputStream = assetManager.open("bobrgb888.png");
        bob565 = BitmapFactory.decodeStream(inputStream);
        inputStream.close();
        Log.d("BitmapText",
                "bobrgb888.png format: " + bob565.getConfig());

        inputStream = assetManager.open("bobargb8888.png");
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_4444;
        bob4444 = BitmapFactory
                .decodeStream(inputStream, null, options);
        inputStream.close();
        Log.d("BitmapText",
                "bobargb8888.png format: " + bob4444.getConfig());

        }
        catch (IOException e) {
            // silently ignored, bad coder monkey, baaad!
        } finally {
            // we should really close our input streams here.
        }
    }

    public void resume() {          
        running = true;
        renderThread = new Thread(this);
        renderThread.start();         
    }      

    public void run() {
        while(running) {  
            if(!holder.getSurface().isValid())
                continue;

            Canvas canvas = holder.lockCanvas();            
            canvas.drawRGB(0, 0, 255);  

            canvas.drawBitmap(bob4444, x-bob4444.getWidth()/2, y-bob4444.getHeight()/2, null);
            holder.unlockCanvasAndPost(canvas);            
        }
    }

    public void pause() {                        
        running = false;                        
        while(true) {
            try {
                renderThread.join();
                return;
            } catch (InterruptedException e) {
                // retry
            }
        }      
    }        
}

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

    switch (event.getAction()) {


    case MotionEvent.ACTION_UP:
        x = event.getX();
        y = event.getY();

        break;
    }


    return true;
}   

}

于 2013-06-23T20:49:35.807 に答える