I tried to make a floating view that the user can drag onto the screen.
The idea is to start the service and then inflate the presentation on the screen.
But the problem, instead of accepting the event belongs to itself, accepts the entire user input event.
here is my code: manifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.floatandroidpractice" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.floatandroidpractice.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.example.floatandroidpractice.WalkingIconService" /> </application> </manifest>
floating view that can be dragged:
package com.example.floatandroidpractice; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.MotionEvent; import android.view.View; public class littleIconView extends View { private float viewX; private float viewY; private Paint mPaint; private Bitmap androidIcon; public littleIconView(Context context) { super(context); mPaint = new Paint(); mPaint.setColor(Color.BLACK); androidIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); } @Override public void onDraw(Canvas cvs) { cvs.drawBitmap(androidIcon, viewX - androidIcon.getWidth() / 2, viewY - androidIcon.getHeight() / 2, mPaint); } @Override public boolean onTouchEvent(MotionEvent event) { boolean touchedX = Math.abs(viewX - event.getX()) > androidIcon.getWidth(); boolean touchedY = Math.abs(viewY - event.getY()) > androidIcon.getHeight(); boolean isValidTouch = !touchedX && !touchedY; if (isValidTouch) { if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE || event.getAction() == MotionEvent.ACTION_UP) { viewX = event.getX(); viewY = event.getY(); } invalidate(); return true; } else return false; } }
and service:
package com.example.floatandroidpractice; import android.app.Service; import android.content.Intent; import android.graphics.PixelFormat; import android.os.IBinder; import android.view.WindowManager; import android.view.WindowManager.LayoutParams; public class WalkingIconService extends Service { @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { littleIconView a = new littleIconView(getApplicationContext()); LayoutParams mLayoutParams = new WindowManager.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0, PixelFormat.TRANSPARENT); WindowManager mWindowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE); mLayoutParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | LayoutParams.FLAG_LAYOUT_INSET_DECOR | LayoutParams.FLAG_LAYOUT_IN_SCREEN; mWindowManager.addView(a, mLayoutParams); return super.onStartCommand(intent, flags, startId); } @Override public IBinder onBind(Intent intent) { return null; } }
full project: https://github.com/shanwu/shanwu_coding_base/tree/xxx/floatAndroidPractice
source share