80,490
社区成员
发帖
与我相关
我的任务
分享package com.example.hello5;
import java.io.File;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.FrameLayout;
import android.widget.VideoView;
public class VideoService extends Service {
private LayoutParams mLayout;
private WindowManager mWindowManager;
private VideoView vv2;
private long startTime;
float x, y;
int top;
private View view;
private FrameLayout layout;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
createWindowManager();
view = View.inflate(this, R.layout.vedio_window, null);
vv2 = (VideoView) view.findViewById(R.id.vv);
layout = new FrameLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
layout.addView(view);
// MediaController mc2 = new MediaController(this);
File video = new File("/mnt/sdcard/test.mp4");
mWindowManager.addView(layout, mLayout);
if(video.exists()) {
vv2.setVideoPath(video.getAbsolutePath());
// vv2.setMediaController(mc2);
// mc2.setMediaPlayer(vv2);
// vv2.requestFocus();
vv2.start();
}
layout.setOnTouchListener(new OnTouchListener() {
float mTouchStartX;
float mTouchStartY;
@Override
public boolean onTouch(View v, MotionEvent event) {
// System.out.println("mDesktopLayout.width:" + view.getWidth() + view.getHeight());
x = event.getRawX();
y = event.getRawY() - top;
Log.i("startP", "startX" + mTouchStartX + "====startY"
+ mTouchStartY);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mTouchStartX = event.getX();
mTouchStartY = event.getY();
Log.i("startP", "startX" + mTouchStartX + "====startY"
+ mTouchStartY);
long end = System.currentTimeMillis() - startTime;
if (end < 300) {
// closeDesk();
System.out.println("双击!");
}
startTime = System.currentTimeMillis();
break;
case MotionEvent.ACTION_MOVE:
mLayout.x = (int) (x - mTouchStartX);
mLayout.y = (int) (y - mTouchStartY);
mWindowManager.updateViewLayout(v, mLayout);
// view.layout(mLayout.x, mLayout.y, mLayout.x+view.getWidth(), mLayout.y + view.getHeight());
break;
case MotionEvent.ACTION_UP:
mLayout.x = (int) (x - mTouchStartX);
mLayout.y = (int) (y - mTouchStartY);
mWindowManager.updateViewLayout(v, mLayout);
// view.layout(mLayout.x, mLayout.y, mLayout.x+view.getWidth(), mLayout.y + view.getHeight());
mTouchStartX = mTouchStartY = 0;
break;
}
return true;
}
});
}
private void createWindowManager() {
mWindowManager = (WindowManager)this.getSystemService(Context.WINDOW_SERVICE);
mLayout = new WindowManager.LayoutParams();
mLayout.type = WindowManager.LayoutParams.TYPE_PHONE;
mLayout.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
mLayout.format = PixelFormat.RGBA_8888;
mLayout.gravity = Gravity.TOP | Gravity.LEFT;
mLayout.width = 400;
mLayout.height = 300;
}
}