首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TranslateAnimation中的OnClickListener

TranslateAnimation中的OnClickListener
EN

Stack Overflow用户
提问于 2012-01-21 19:04:03
回答 1查看 1.4K关注 0票数 5

我正在做一个使用安卓中的TranslateAnimation的项目。因为我成功地翻译了图像,但我必须添加onClickListener到图像中。这意味着我在翻译过程中单击了图像并获得了它们的值,但我得到的是图像上的onClick操作。

我使用了以下代码:

代码语言:javascript
复制
public class SampleGame extends Activity implements OnClickListener {

    int x10, x20, y10, y20;

    ImageView img;

    Button animation;
    Handler transHandler;
    RelativeLayout layout;
    Random rand;
    int mSpeedX, mSpeedY;
    int width, height;
    Thread thread;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        layout = (RelativeLayout) findViewById(R.id.layout);
        animation = (Button) findViewById(R.id.animation);

        animation.setOnClickListener(this);

        rand = new Random();

        width = getWindow().getWindowManager().getDefaultDisplay().getWidth() - 20;
        height = getWindow().getWindowManager().getDefaultDisplay().getHeight() - 70;

        x10 = width - rand.nextInt(width);
        y10 = height;

        x20 = width;
        y20 = height - rand.nextInt(height);

        thread = new Thread();

        img.setOnClickListener(this);

    }

    public void onPause() {
        super.onPause();
        thread = null;
    }

    @Override
    public void onClick(View v) {

        if (v.equals(img)) {
            Log.e("img clicked  :", "yaaaaaaaaa");
        }

        if (v.equals(animation)) {
            animation.setVisibility(View.INVISIBLE);
            callTransform();
        }
    }

    public void callTransform() {
        if (thread != null) {
            thread = new Thread() {
                public void run() {

//  Log.e("X1 value     :",String.valueOf(x10));    
//  Log.e("X2 value     :",String.valueOf(x20));    
//  Log.e("Y1 value     :",String.valueOf(y10));    
//  Log.e("Y2 value     :",String.valueOf(y20));    

                    transformAnimation(x10, x20, y10, y20, img, 4000);

                    try {
                        Thread.sleep(4000);
                    } catch (Exception e) {
                    }
//  Message msg=transHandler.obtainMessage();
//  transHandler.sendMessage(msg);
                    x10 = x20;
                    y10 = y20;

                    if (x10 == width) {
                        x20 = width - rand.nextInt(width);
                        y20 = 0;
                    } else if (y10 == 0) {
                        x20 = 0;
                        y20 = height - rand.nextInt(height);
                    } else if (x10 == 0) {
                        x20 = width - rand.nextInt(width);
                        y20 = height;
                    } else if (y10 == height) {
                        x20 = width;
                        y20 = height - rand.nextInt(height);
                    }

                    callTransform();
                }
            };
            thread.start();
        }
    }


    public void transformAnimation(int xFrom, int xTo, int yFrom, int yTo, final ImageView image, int time) {
        TranslateAnimation anim = new TranslateAnimation(xFrom, xTo, yFrom, yTo);
        anim.setDuration(time);
        image.setAnimation(anim);
    }

}

我也尝试了下面的例子-- 2D-Tutorial Example

但在这一点上,我也没有对图像执行onClick操作。

那么如何在翻译过程中对图像执行onClick操作呢?

有没有其他方法可以通过单击操作来翻译图像?

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-13 17:38:42

在动画过程中,对象保持在其原始位置,但它会进行动画平移,因此您需要在动画过程中手动更新布局参数

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8952465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档