【问题标题】:How to move an object use Sin& Cos in Android如何在 Android 中使用 Sin 或 Cos 移动对象
【发布时间】:2015-07-28 15:42:27
【问题描述】:

我希望我的圆圈运行图形缠绕。我应该怎么办? 此代码沿对角线运行。

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_move);
    final MyCustomPanel view = new MyCustomPanel(this);


    ViewGroup.LayoutParams params =
            new     ViewGroup.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.FILL_PARENT);
    addContentView(view, params);
    findViewById(R.id.btB).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for (int i=1;i<=1000;i++){
             x = i;
             y = i;
             view.animate().x(x).y(y).setDuration(5000).start();               
            }
        }
    });

【问题讨论】:

    标签: android animation translate-animation


    【解决方案1】:

    简单您必须通过扩展BaseInterpolator 来创建您的插值器,并为getInterpolation(float) 方法提供一个实现。

    我想你只想回来

    public float getInterpolation(float input) {
           return (float)(Math.cos((input) * Math.PI)
           }
    

    【讨论】:

    • 我试过了,效果很好。我使用了代码 DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); TranslateAnimation 翻译 = new TranslateAnimation(0f, 0F, 0f, metrics.widthPixels);翻译.setStartOffset(500);翻译.setDuration(2000);翻译.setFillAfter(true); translation.setInterpolator(new SinInterpolator()); animView.startAnimation(翻译);
    • 嗨。 Neeraj Kumar prntscr.com/86oueb 和这个 prntscr.com/86ousc 但不活跃