我的目标是创建一个应用程序,即使它在后台也是如此。 为了更清楚,我向您展示了我将得到的一些例子。有可能吗?
答案 0 :(得分:1)
您可以使用窗口管理器addView方法添加视图,就像facebook chathead的实现方式一样。 请参阅链接:what-apis-in-android-is-facebook-using-to-create-chat-heads
答案 1 :(得分:1)
使用此代码
<强> floatingBubble.java 强>
package com.likith.floatingbubble;
import com.likith.gesturelauncherPro.R;
import com.likith.gesturelauncherPro.gestureDrawer;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
public class FloatingFaceBubbleService extends Service
{
private WindowManager windowManager;
private ImageView floatingFaceBubble;
@SuppressLint("RtlHardcoded")
public void onCreate()
{
super.onCreate();
floatingFaceBubble = new ImageView(this);
floatingFaceBubble.setImageResource(R.drawable.ic_launcher);
windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
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=this.getResources().getDisplayMetrics().widthPixels;
myParams.y=0;
windowManager.addView(floatingFaceBubble, myParams);
try
{
floatingFaceBubble.setOnTouchListener(new View.OnTouchListener()
{
@SuppressWarnings("unused")
WindowManager.LayoutParams paramsT = myParams;
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
private long touchStartTime = 0;
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event)
{
if(System.currentTimeMillis()-touchStartTime>ViewConfiguration.getLongPressTimeout() && initialTouchX== event.getX())
{
windowManager.removeView(floatingFaceBubble);
stopSelf();
return false;
}
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
touchStartTime = System.currentTimeMillis();
initialX = myParams.x;
initialY = myParams.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
myParams.x = initialX + (int) (event.getRawX() - initialTouchX);
myParams.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(v, myParams);
break;
}
return false;
}
});
floatingFaceBubble.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//Toast.makeText(getApplicationContext(), "Clicked",Toast.LENGTH_LONG).show();
Intent myNewActivity = new Intent(getBaseContext(),gestureDrawer.class);
myNewActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myNewActivity);
}
});
} catch (Exception e){
e.printStackTrace();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
<强> LittleIcon.java 强>
package com.likith.floatingbubble;
import com.likith.gesturelauncherPro.R;
import android.annotation.SuppressLint;
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;
import android.widget.Toast;
public class LittleIcon extends View
{
private float viewX;
private float viewY;
private Paint mPaint;
private Bitmap androidIcon;
public LittleIcon(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);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event)
{
Toast.makeText(getContext(), "Clicked", Toast.LENGTH_LONG).show();
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;
}
}
<强>权限强>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<service android:name="com.likith.floatingbubble.FloatingFaceBubbleService" />
<强>用法强>
Intent intent = new Intent(mainActivity.this, com.likith.floatingbubble.FloatingFaceBubbleService.class);
startService(intent);