如何在android中从左到右和从右到左移动图像?

时间:2011-06-17 09:39:15

标签: android image android-animation

我是android的新手。我想从左到右,从右到左移动图像。当用户触摸图像的右侧图像时,它将向右移动。当用户触摸图像的右侧时会发生这种情况。但是图像将移动预定点跨越x轴。

例如:图像将顺序移动p1(100,100),p2(150,100),p3(200,100),p4(250,100)。如果用户触摸p1的左侧,它将保持当前位置。在p4处将发生事情。我可以将图像从p1移动到p2,将p2移动到p1。当我添加p3和p4时,它没有像我预期的那样工作。

这是我的GameView课程。

public class GameView extends SurfaceView{
    private Bitmap BG_image;
    private SurfaceHolder holder;
    //private GameLoopThread gameLoopThread;    
    int x;
    int y;
    private int srcX=100;
    private int srcY=100;
    int replaceX=0,areaThreshold=50;
    int distance=50;

    public GameView(Context context) {

        super(context);
        //gameLoopThread = new GameLoopThread(this);
        holder = getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

/*              boolean retry = true;
                gameLoopThread.setRunning(false);

                while (retry) {

                    try {

                        gameLoopThread.join();
                        retry = false;

                    } catch (InterruptedException e) {

                    }
                }*/
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {

/*              gameLoopThread.setRunning(true);
                gameLoopThread.start();*/
                Canvas c = holder.lockCanvas(null);
                onDraw(c);
                holder.unlockCanvasAndPost(c);
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format,

                    int width, int height) {
            }
        });
        BG_image = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        Bitmap.createScaledBitmap(BG_image, BG_image.getWidth(), BG_image.getHeight(), false);
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
               // x = (int) ev.getX();
               // y = (int) ev.getY();
                updateX(srcX);
                replaceX=(int)ev.getX();
                int StartX=0, EndX=0;
                StartX=replaceX-areaThreshold;
                EndX=replaceX+areaThreshold;
                if(StartX<=100){
                    SurfaceHolder holder=getHolder();
                    Canvas myCanvas=holder.lockCanvas(null);
                    onDraw(myCanvas);
                    holder.unlockCanvasAndPost(myCanvas);
                    srcX=100;
                }
                else if(StartX>100 && StartX<250){
                    SurfaceHolder holder=getHolder();
                    Canvas myCanvas=holder.lockCanvas(null);
                    onDraw(myCanvas);
                    holder.unlockCanvasAndPost(myCanvas);
                    srcX=srcX+distance;
                }
                if(EndX>100 && EndX<250){
                    SurfaceHolder holder=getHolder();
                    Canvas myCanvas=holder.lockCanvas(null);
                    onDraw(myCanvas);
                    holder.unlockCanvasAndPost(myCanvas);
                    srcX=srcX-distance;
                }
                else if(EndX>250){
                    SurfaceHolder holder=getHolder();
                    Canvas myCanvas=holder.lockCanvas(null);
                    onDraw(myCanvas);
                    holder.unlockCanvasAndPost(myCanvas);
                    srcX=250;
                }

        return super.onTouchEvent(ev);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub  
        updateX(srcX);
        Paint paint = new Paint();
        canvas.drawColor(Color.BLACK);
        canvas.drawRect(new Rect(0,0,getWidth(),getHeight()),paint);
        canvas.drawBitmap(BG_image, srcX, srcY, null);
    }

    private int updateX(int srcX){
        this.srcX =srcX;
        return srcX ;

    }
}

请查看我的代码并提供宝贵的建议以解决我的问题。 希望我能尽快得到回复!

2 个答案:

答案 0 :(得分:4)

使用Android TranslateAnimation

非常容易
ImageView img_animation = (ImageView) findViewById(R.id.img_animation);

TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
            0.0f, 0.0f);          //  new TranslateAnimation(xFrom,xTo, yFrom,yTo)
    animation.setDuration(5000);  // animation duration 
    animation.setRepeatCount(5);  // animation repeat count
    animation.setRepeatMode(2);   // repeat animation (left to right, right to left )
    //animation.setFillAfter(true);      

    img_animation.startAnimation(animation);  // start animation 

从此处move an image from left to right and right to left in android

了解更多详情

答案 1 :(得分:0)

现在,我理解我的问题。非常感谢你提出你的建议。 这是我更新的代码。

    public class GameView extends SurfaceView{
    private Bitmap BG_image;
    private SurfaceHolder holder;
    int x=100;
    private int srcX=100;
    private int srcY=100;
    int distance=50;

    public GameView(Context context) {

        super(context);
        holder = getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {

                Canvas c = holder.lockCanvas(null);
                onDraw(c);
                holder.unlockCanvasAndPost(c);
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format,

                    int width, int height) {
            }
        });
        BG_image = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        Bitmap.createScaledBitmap(BG_image, BG_image.getWidth(), BG_image.getHeight(), false);
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        x = (int) ev.getX();        
        if(x-srcX>0)
            srcX += distance;
        else if(x-srcX<0)
            srcX -= distance;

        if(srcX<=100)
            srcX = 100;
        else if(srcX>250)
            srcX = 250;     
        Log.w("ev.getX : srcX",x+" : "+srcX);
        SurfaceHolder holder=getHolder();
        Canvas myCanvas=holder.lockCanvas(null);
        onDraw(myCanvas);
        holder.unlockCanvasAndPost(myCanvas);
        return super.onTouchEvent(ev);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub  
        Paint paint = new Paint();
        canvas.drawColor(Color.BLACK);
        canvas.drawRect(new Rect(0,0,getWidth(),getHeight()),paint);
        canvas.drawBitmap(BG_image, srcX-BG_image.getWidth()/2, srcY, null);
    }
}