1

我想要做的是在触摸按下时绘制图像并且当它移动时图像也应该与手指一起移动并且当触摸向上时图像应该保持在那个位置。再次,如果我将手指放下,则同一图像的另一个副本应该再次出现在触地坐标中,并且该过程继续进行。我看了很多教程并尝试了一切。我试图在手指向下时保存画布的图像,当它移动时清除屏幕,并在手指向下时保存图像,但这不起作用。然后,我尝试用黑色清除画布并粘贴手指向下保存的图像,然后绘制我想要出现的新图像,但它仍然无法正常工作。它会导致闪烁,有时当抬起我的手指然后再次放下时,最后一个图像会被删除。

public class renderView extends SurfaceView implements Runnable{

    boolean isRunning=false;
    SurfaceHolder holder;
    Thread t=null;
    Bitmap img;
    Paint paint;
    Canvas canvas2,canvas3;
    Bitmap cache,backup,cache2;
    float x,y;
    boolean up,down,move;
    boolean dummy;

    public renderView(Context context) {
        super(context);
        isRunning=true;
        holder = getHolder();
        img = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        paint= new Paint();
        x=y=0;
        cache = Bitmap.createBitmap(600, 800, Config.ARGB_8888);
        cache2 = Bitmap.createBitmap(600, 800, Config.ARGB_8888);
        backup = Bitmap.createBitmap(600,800, Bitmap.Config.ARGB_8888);
        canvas2 = new Canvas(cache);
        canvas3 =new Canvas(cache2);

        move=down=false;
        up=true;
        dummy=false;
    }
    @Override
    public void run() {
        Log.d("status","run");
        while(isRunning){
            if(!holder.getSurface().isValid()){
                continue;
            }

             Canvas canvas1 = holder.lockCanvas();

            canvas1.drawColor(Color.BLUE);
            canvas1.drawBitmap(cache2,0,0,paint);
            canvas1.drawBitmap(cache,0,0,paint);

            holder.unlockCanvasAndPost(canvas1);

        }
    }



    public void pause(){
        isRunning = false;
        while(true){
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            break;

        }
        t=null;
    }


    public void resume(){
        isRunning=true;
        t=new Thread(this);
        t.start();
    }
    public void down(float posX, float posY) {

        /*x=posX;
        y=posY;*/

        canvas2.drawBitmap(img,posX,posY,paint);
        canvas3.drawBitmap(img,posX,posY,paint);
        up=false;
    }
    public void move(float posX, float posY) {

        move=true;
        dummy=true;

        canvas2.drawColor(Color.BLACK); 
        canvas2.drawBitmap(cache2, 0, 0,paint);
        canvas2.drawBitmap(img,posX,posY,paint);


    }

    public void up(float posX, float posY) {
        up=true;

        move=false;
        canvas2.drawColor(Color.BLACK);
        canvas2.drawBitmap(cache2,0,0,paint);

        canvas2.drawBitmap(img,posX,posY,paint);
    }


}
4

0 回答 0