如何在Android中从左向右和从右向左移动图像?

如何在Android中从左向右和从右向左移动图像?,第1张

概述我是android新手.我想从左到右,从右到左移动图像,当用户触摸图像的右侧图像时它将向右侧移动.当用户触摸图像的右侧时会发生相同的事情,但是图像会移动预定点跨x轴.例如:图像将在这些点上依次移动p1(100,100),p2(150,100),p3(200,100),p4(250,100).如果用户触摸p1的左侧,它将保持当

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

请查看我的代码,并提供宝贵的建议来解决我的问题.
希望我能尽快得到答复!

解决方法:

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

总结

以上是内存溢出为你收集整理的如何在Android中从左向右和从右向左移动图像?全部内容,希望文章能够帮你解决如何在Android中从左向右和从右向左移动图像?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1084054.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存