0

I've made a service which sets an image on home screen of android phone. Now I want to move that image on the device screen. How should I do that?

Answer is appreciated.

Thanks in advance.

This is my code.

public class FeatureService extends Service
{
private WindowManager windowManager;
private ImageView dialerImage;
@Nullable
@Override
public IBinder onBind(Intent intent)
{
    return null;
}

@Override
public void onCreate()
{
    super.onCreate();
    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    dialerImage = new ImageView(this);
    dialerImage.setImageResource(R.drawable.dialer);

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 100;
    windowManager.addView(dialerImage, params);
    dialerImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            Intent i= new Intent(getApplicationContext(), MainActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
            return;

        }
    });


    dialerImage.setOnLongClickListener(new View.OnLongClickListener()
    {
        @Override
        public boolean onLongClick(View v)
        {
            dialerImage.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_UP:
                        {
                            return false;
                        }

                        case MotionEvent.ACTION_DOWN:
                        {
                            initialX = params.x;
                            initialY = params.y;
                            initialTouchX = event.getRawX();
                            initialTouchY = event.getRawY();
                            return false;
                        }

                        case MotionEvent.ACTION_MOVE:
                        {
                            params.x = initialX + (int) (event.getRawX() - initialTouchX);
                            params.y = initialY + (int) (event.getRawY() - initialTouchY);
                            windowManager.updateViewLayout(dialerImage, params);
                            return false;
                        }

                    }
                    return true;
                }
            });
            return false;
        }
    });
}

@Override
public void onDestroy()
{
    super.onDestroy();
    if (dialerImage != null) windowManager.removeView(dialerImage);
}
}

now i want is to move my image on long click which works fine when i click on image 1st time. But once i move the image 2nd time long click drag doesn't work. from 2nd time image starts moving by just touching on it Long click doesn't work.

  • look at the question again @BradleyWilson – Prashant Jaiswal Jan 17 '17 at 13:25
  • I would suggest looking at a GestureDetector and do an TranslateAnimation when the user flings, once the fling method is called, set the animation co-ordinates the same at the gesture touch co-ordinates. You'll find plenty of information by googling the terms i've mentioned, and also look at http://stackoverflow.com/questions/4098198/adding-fling-gesture-to-an-image-view-android – Bradley Wilson Jan 17 '17 at 13:28
  • Thanks man..hope this helps me and any further suggestion is acceptable. – Prashant Jaiswal Jan 17 '17 at 13:30

0 Answers0