里面的动画窗口管理的ImageView在Android的服务 [英] Animate ImageView inside WindowManager in a Android Service

查看:95
本文介绍了里面的动画窗口管理的ImageView在Android的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动画在服务动态创建一个ImageView的,但没办法得到它的工作......我认为这个问题是因为我使用的是Windows管理员,我应该有权使用的FrameLayout什么类似的,里面放上了ImageView的,但不知道如何做到这一点编程...无论如何,有人知道如何得到这个工作?帮助pleaseee!

  @覆盖公共无效的onCreate(){
    super.onCreate();    最终GestureDetector mGestureDetector =新GestureDetector(getApplicationContext(),新MyGestureDetector());    窗口管理=(窗口管理器)getSystemService(WINDOW_SERVICE);    chatHead =新ImageView的(本);
    chatHead.setImageResource(R.drawable.btn_enable_floating);    最后WindowManager.LayoutParams PARAMS =新WindowManager.LayoutParams(
            100,
            107,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);    params.gravity = Gravity.TOP | Gravity.RIGHT;
    params.x = 0;
    params.y = 100;
    chatHead.setOnTouchListener(新View.OnTouchListener(){
        私人诠释initialX;
        私人诠释initialY;
        私人浮动initialTouchX;
        私人浮动initialTouchY;        @覆盖
        公共布尔onTouch(视图V,MotionEvent事件){
            开关(event.getAction()){
                案例MotionEvent.ACTION_DOWN:
                    initialX = params.x;
                    initialY = params.y;
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();
                    chatHead.setImageResource(R.drawable.btn_enable_floating_hover);
                    返回mGestureDetector.onTouchEvent(事件);
                案例MotionEvent.ACTION_UP:
                    chatHead.setImageResource(R.drawable.btn_enable_floating);
                    返回mGestureDetector.onTouchEvent(事件);
                案例MotionEvent.ACTION_MOVE:
                    //params.x = initialX +(int)的(event.getRawX() - initialTouchX);
                    params.y = initialY +(int)的(event.getRawY() - initialTouchY);
                    windowManager.updateViewLayout(chatHead,则params);
                    返回mGestureDetector.onTouchEvent(事件);
            }
            返回mGestureDetector.onTouchEvent(事件);
        }
    });
    windowManager.addView(chatHead,则params);    RotateAnimation RA =新RotateAnimation(
            0,
            -80,
            Animation.RELATIVE_TO_SELF,0.5F,
            Animation.RELATIVE_TO_SELF,
            0.5F);    //动画需要多长时间的地方
    ra.setDuration(210);    //的保留状态结束后设置动画
    ra.setFillAfter(真);
    //开始动画
    chatHead.startAnimation(RA);
}


解决方案

我刚刚有同样的问题,这里是一个解决方案:
相反,一个ImageView的,你需要添加的FrameLayout,有您的ImageView的。

  LayoutInflater吹气=(LayoutInflater)getApplicationContext()getSystemService(LAYOUT_INFLATER_SERVICE)。
floatingLayout =(的FrameLayout)inflater.inflate(R.layout.floating_layout,NULL);

保存按钮来处理动画后:

  mButton =(ImageView的)floatingLayout.findViewById(R.id.mButton);

Instaed添加一个按钮,添加一个布局:

  windowManager.addView(floatingLayout,floatingLayoutParams);

动画!

 动画旋转= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotation);
    rotate.setDuration(1000);
    mButton.startAnimation(旋转);

I'm trying to animate an ImageView created dynamically in a service, but no way to get it working... I think the problem is because I'm using a windows manager and I should have to use a FrameLayout or something similar and place the ImageView inside, but no idea how to do it programatically... Anyway, someone know how to get this working? Help pleaseee!

 @Override public void onCreate() {
    super.onCreate();

    final GestureDetector mGestureDetector = new GestureDetector(getApplicationContext(), new MyGestureDetector());

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    chatHead = new ImageView(this);
    chatHead.setImageResource(R.drawable.btn_enable_floating);

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            100,
            107,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.RIGHT;
    params.x = 0;
    params.y = 100;


    chatHead.setOnTouchListener(new View.OnTouchListener() {
        private int initialX;
        private int initialY;
        private float initialTouchX;
        private float initialTouchY;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    initialX = params.x;
                    initialY = params.y;
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();
                    chatHead.setImageResource(R.drawable.btn_enable_floating_hover);
                    return mGestureDetector.onTouchEvent(event);
                case MotionEvent.ACTION_UP:
                    chatHead.setImageResource(R.drawable.btn_enable_floating);
                    return mGestureDetector.onTouchEvent(event);
                case MotionEvent.ACTION_MOVE:
                    //params.x = initialX + (int) (event.getRawX() - initialTouchX);
                    params.y = initialY + (int) (event.getRawY() - initialTouchY);
                    windowManager.updateViewLayout(chatHead, params);
                    return mGestureDetector.onTouchEvent(event);
            }
            return mGestureDetector.onTouchEvent(event);
        }
    });


    windowManager.addView(chatHead, params);

    RotateAnimation ra = new RotateAnimation(
            0,
            -80,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF,
            0.5f);

    // how long the animation will take place
    ra.setDuration(210);

    // set the animation after the end of the reservation status
    ra.setFillAfter(true);
    // Start the animation
    chatHead.startAnimation(ra);
}

解决方案

I've just had the same problem, here is a solution: Instead of an ImageView, you need to add a FrameLayout, that has your ImageView.

LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
floatingLayout = (FrameLayout) inflater.inflate(R.layout.floating_layout, null);

Save your button to deal with animations later:

mButton = (ImageView) floatingLayout.findViewById(R.id.mButton);

Instaed of adding a button, add a layout:

windowManager.addView(floatingLayout, floatingLayoutParams);

Animate!

Animation rotate = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotation);
    rotate.setDuration(1000);
    mButton.startAnimation(rotate);

这篇关于里面的动画窗口管理的ImageView在Android的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆