Android:使用动画移动位于窗口管理器上的 imageView

标签 android android-layout android-service android-animation android-windowmanager

遇到一些问题,但没有找到解决方案。问题是,我有一个位于布局上的 ImageView ,该布局位于窗口管理器上,该 ImageView 是可单击的。该 ImageView 必须向右和向左移动并且可单击。这是我的代码

`公共(public)类 FloatingAnimationService 扩展了 Service {

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


}

` 请帮忙。谢谢!

最佳答案

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

下面是代码

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


}

关于Android:使用动画移动位于窗口管理器上的 imageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39791387/

相关文章:

android - Flutter 返回空字符串 - HTTP(S) GET 请求

Android 图像在模拟器中变得模糊

android - 使用 Hilt 将存储库注入(inject) Android 中的服务

android - 无法将Facebook SDK添加为Android Studio中的依赖项

android - 将 build(apk) 上传到 hockeyApp 的管道脚本

Android Button 失去了它的文本对齐

android - 创建具有卡住列和卡住标题的表/网格

android - 电话休眠时停止服务

Android BOOT_COMPLETED 事件未启动服务

Android ImageButton图片对比