Android - 在触摸事件的屏幕上绘制位图

时间:2013-06-23 04:44:45

标签: android

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


       }        
    }
}

1 个答案:

答案 0 :(得分: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;
}   

}