如何在安卓中让图片从左到右移动并从右到左移动?

3

我是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 ;

    }
}

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

你的代码没有注释,很混乱,而且你的解释有逻辑漏洞。你可以做得更好。 - Anko
同意Aku的观点。此外,你不应该直接调用onDraw,而应该只设置srcX值并调用invalidate,这将告诉系统安排一个对onDraw的调用。而且updateX方法似乎没有任何作用。我认为你可以删除大约一半的代码。 - DocDude
2个回答

5

使用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 

从这里了解如何在Android中将图像从左到右和从右到左移动的更多详细信息。


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);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接