Android:用动画移动位于 window 管理器上的 imageView

Android: move imageView thats lay on window manager with animation

a 遇到了一些问题,但没有找到解决方案。问题是,我有一个位于布局上的图像视图,而这个布局位于 window 管理器上,这个图像视图是可点击的。此图像视图必须向右和向左移动并且可以单击。这是我的代码

`public class FloatingAnimationService 扩展服务 {

private WindowManager windowManager;

private ImageView floatingUnit;

private boolean isClicked;

public void onCreate() {

    super.onCreate();

    isClicked = false;

    floatingUnit = new ImageView(this);
    //a unit as imageView
    Picasso.with(this).load(new File(Environment.getExternalStorageDirectory().getPath() +"/" + "cat.png")).into(floatingUnit);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(AppMethods.getPxFromDp(120 ,this), AppMethods.getPxFromDp(120 , this));
    floatingUnit.setLayoutParams(layoutParams);
    final LinearLayout floatingUnitLayout = new LinearLayout(this);

    floatingUnitLayout.addView(floatingUnit);

    windowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
    //here is all the science of params

    final LayoutParams myParams = new WindowManager.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT,
            LayoutParams.TYPE_PHONE,
            LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);
    myParams.gravity = Gravity.TOP | Gravity.LEFT;
    myParams.x=0;
    myParams.y=100;


    windowManager.addView(floatingUnitLayout, myParams);



    // add a floatingUnit icon in window

    Animation animation = AnimationUtils.loadAnimation(this ,R.anim.floating_unit_animation);



    floatingUnit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startQuestion();
            stopSelf();
            windowManager.removeViewImmediate(floatingUnitLayout);
            isClicked = true;

        }
    });
    floatingUnit.startAnimation(animation);
    startAnimationTimer(floatingUnitLayout);

    AnimationSet animationSet = new AnimationSet(true);

    myParams.x = 100;
    myParams.y = 100;
    windowManager.updateViewLayout(floatingUnitLayout, myParams);

    Animation animationToRight = AnimationUtils.loadAnimation(this ,R.anim.left_to_right_animation);
    Animation animationToLeft = AnimationUtils.loadAnimation(this , R.anim.right_to_left_animation);

    animationSet.addAnimation(animationToRight);

    floatingUnit.startAnimation(animation);
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

private void startQuestion (){

    Intent intentQuestionActivity = new Intent(this, QuestionActivity.class);
    intentQuestionActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intentQuestionActivity);

}

private void startAnimationTimer(final View floatingUnitLayout){


    long animationLifeTime = 10000;
    new CountDownTimer(animationLifeTime, 1000) {

        public void onTick(long millisUntilFinished) {
            Log.i("animation remaining: " , Long.toString(millisUntilFinished / 1000));

        }

        public void onFinish() {
            Log.i("animation: " , "DONE");
            if(!isClicked) {
                windowManager.removeViewImmediate(floatingUnitLayout);
                stopSelf();
            }
        }
    }.start();


}

` 请帮忙。谢谢!

谢谢大家。自己找到了解决方案。诀窍是:要移动位于 window 管理器上的图像,您必须更新布局参数。

下面是代码

 private void startAnimationTimer(final View floatingUnitLayout) {

    long animationLifeTime = AppData.first(AppData.class).getCountSecondsPicShow();
    countDownTimer = new CountDownTimer(animationLifeTime, 100) {

        public void onTick(long millisUntilFinished) {

            Log.i("animation remaining: ", Long.toString(millisUntilFinished / 1000));
            myParams.x = myParams.x + 2;
            myParams.y = myParams.y + 2;
            windowManager.updateViewLayout(floatingUnitLayout, myParams);


        }

        public void onFinish() {
            Log.i("animation: ", "DONE");
            if (!isClicked) {

                windowManager.removeViewImmediate(floatingUnitLayout);
                stopSelf();
            }
        }
    }.start();


}