黑马程序员技术交流社区
标题:
ANDROID桌面移动球3
[打印本页]
作者:
woshiku
时间:
2015-9-12 10:07
标题:
ANDROID桌面移动球3
package com.desk.move.point.service;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
import com.desk.move.point.R;
import com.desk.move.point.domian.PointViewRemind;
import com.desk.move.point.utils.DimMeasureUtils;
import com.desk.move.point.utils.LoclScreenUtils;
import com.desk.move.point.utils.ScreenSizeUtils;
import com.desk.move.point.utils.VirbriateUtils;
import com.desk.move.point.view.MovePoint;
public class PointView extends Service {
private WindowManager mWM;
private WindowManager.LayoutParams params;
private View view;
private ImageView ig;
private int igDp = 10;
private MovePoint movepoint;
private MyHandler myHandler;
private ScreenSizeUtils screenSize;
private int screenHeight,screenWidth;
private DimMeasureUtils dim;
Context context;
//灯
private boolean firstOpen = true;
private Camera camera = null;
private Parameters one = null;
//锁屏
private LoclScreenUtils lock;
private void openLight(){
if(one == null){
one = camera.getParameters();
}
List<String> list = one.getSupportedFlashModes();
if(firstOpen){
firstOpen = false;
one.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(one);
}
if (list.contains(Parameters.FLASH_MODE_TORCH)||list.contains(Parameters.FLASH_MODE_OFF)) {
if(one.getFlashMode()== Parameters.FLASH_MODE_OFF){
one.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(one);
camera.startPreview();
}else if(one.getFlashMode()== Parameters.FLASH_MODE_TORCH){
one.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(one);
camera.stopPreview();
}
} else {
Toast.makeText(getApplicationContext(), "此设备不支持闪光灯模式",
Toast.LENGTH_SHORT).show();
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
context = PointView.this;
dim = new DimMeasureUtils(context);
myHandler = new MyHandler();
camera = Camera.open();
lock = LoclScreenUtils.getInstance();
Log.e("show"," show");
showToast();
Log.e("show1"," show1");
//Toast.makeText(PointView.this, "开启", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (mWM != null && view != null) {
mWM.removeView(view);// 从window中移除view
view = null;
}
if (null != camera) {
camera.release();
camera = null;
}
}
/**
* 自定义归属地浮窗
*/
private void showToast() {
mWM = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
screenSize = new ScreenSizeUtils(mWM);
screenHeight = screenSize.getHeight();
screenWidth = screenSize.getWidth();
//Toast.makeText(PointView.this, "width"+screenWidth+"height"+screenHeight, Toast.LENGTH_SHORT).show();
params = new WindowManager.LayoutParams();
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.format = PixelFormat.TRANSLUCENT;
params.type = WindowManager.LayoutParams.TYPE_TOAST;
params.gravity = Gravity.TOP+Gravity.LEFT;
params.setTitle("Toast");
// view = new TextView(this);
view = View.inflate(this,R.layout.move_view, null);
movepoint = (MovePoint)view.findViewById(R.id.move_point);
params.x = screenWidth -movepoint.getSize() ;
params.y = screenHeight/2;
ig = (ImageView)view.findViewById(R.id.move_bian);
ig.setVisibility(View.GONE);
ig.setOnClickListener(new ClickMe());
movepoint.setPointViewRemind(new PointRemind());
mWM.addView(view, params);// 将view添加在屏幕上(Window)
}
class ClickMe implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
VirbriateUtils.virbate(context);
params.x = screenWidth-movepoint.getSize();
movepoint.setVisibility(View.VISIBLE);
ig.setVisibility(View.GONE);
mWM.updateViewLayout(view, params);
}
}
class PointRemind implements PointViewRemind{
@Override
public void up(MovePoint point) {
// TODO Auto-generated method stub
VirbriateUtils.virbateOne(context);
openLight();
}
@Override
public void down(MovePoint point) {
// TODO Auto-generated method stub
VirbriateUtils.virbateOne(context);
lock.initDevice(context);
lock.lockScreen(context);
}
@Override
public void left(MovePoint point) {
// TODO Auto-generated method stub
//VirbriateUtils.virbateOne(context);
}
@Override
public void right(MovePoint point) {
// TODO Auto-generated method stub
//VirbriateUtils.virbateOne(context);
}
@Override
public void enterLongPress(MovePoint point) {
// TODO Auto-generated method stub
VirbriateUtils.virbate(context);
}
@Override
public void getXYShow(int x, int y) {
// TODO Auto-generated method stub
sendMessgae(0,x,y);
}
}
private void sendMessgae(int what,int x,int y){
Message msg = new Message();
msg.what = what;
Bundle bd = new Bundle();
bd.putInt("x", x);
bd.putInt("y", y);
msg.setData(bd);
myHandler.sendMessage(msg);
}
@SuppressLint("HandlerLeak")
class MyHandler extends Handler{
public void handleMessage(Message msg){
switch(msg.what){
case 0:
Bundle bd = msg.getData();
int x = bd.getInt("x");
int y = bd.getInt("y");
//show.setText("x="+x+","+"y="+y+","+"left"+myPoint.getLeft());
params.x = x;
params.y = y;
if(x<0){
params.x = 0;
}
if(y<0){
params.y = 0;
}
if(x>screenWidth-movepoint.getSize()){
//params.x = screenWidth-movepoint.getSize();
params.x = screenWidth-dim.dpToPx(igDp);
movepoint.setVisibility(View.GONE);
ig.setVisibility(View.VISIBLE);
}
if(y>screenHeight-movepoint.getSize()){
params.y = screenHeight-movepoint.getSize();
}
mWM.updateViewLayout(view, params);
//myPoint.setLayoutParams(lp);
break;
}
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2