黑马程序员技术交流社区
标题:
ANDROID桌面移动球5
[打印本页]
作者:
woshiku
时间:
2015-9-12 10:11
标题:
ANDROID桌面移动球5
package com.desk.move.point.utils;
import android.content.Context;
import android.os.Vibrator;
public class VirbriateUtils {
public static void virbate(Context context){
@SuppressWarnings("static-access")
Vibrator vb = (Vibrator)context.getSystemService(context.VIBRATOR_SERVICE);
vb.vibrate(new long[]{0,200,0,0}, -1);
}
public static void virbateOne(Context context){
@SuppressWarnings("static-access")
Vibrator vb = (Vibrator)context.getSystemService(context.VIBRATOR_SERVICE);
vb.vibrate(new long[]{0,50,0,0}, -1);
}
}
package com.desk.move.point.view;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import com.desk.move.point.R;
import com.desk.move.point.domian.PointViewRemind;
import com.desk.move.point.utils.DimMeasureUtils;
@SuppressLint("DrawAllocation")
public class MovePoint extends View implements OnTouchListener{
//dp尺寸真实大小
private int sizedp = 70;
private int interval;
//相素大小
private int size;
private Rect src,src1,src2;
private Rect dst,dst1,dst2;
private DimMeasureUtils dim;
//移动点背景
private Bitmap movePointBg;
//移动点可移动范围背景
private Bitmap movePointAreaBg;
//移动点
private Bitmap movePoint;
//宽度高度
private int movePointBgSize;
private int movePointAreaBgSize;
private int movePointSize;
//点背景偏移量
//背景与环的偏移量
private int one_bg_off;
//环与圆的偏移量
private int two_bg_off;
private boolean first =true;
private boolean choosed = false;
//false内部自由动 true移动位置
private boolean mode = false;
//public enum Direct{LEFT,RIGTH,UP,DOWN,NONE};
private PointViewRemind pointViewRemind = null;
public void setPointViewRemind(PointViewRemind pointViewRemind){
this.pointViewRemind = pointViewRemind;
}
public int getWid(){
return movePointBg.getWidth();
}
public int getSize(){
return getScale(movePointBgSize);
}
private void initBgs(Context context){
movePointBg = BitmapFactory.decodeResource(getResources(),R.drawable.move_point_bg);
movePointAreaBg = BitmapFactory.decodeResource(getResources(),R.drawable.move_point_area);
movePoint = BitmapFactory.decodeResource(getResources(),R.drawable.move_point);
dim = new DimMeasureUtils(context);
setOnTouchListener(this);
//movePointAreaBgHeight = movePointAreaBg.getHeight();
//movePointHeight = movePoint.getHeight();
}
private void initSizes(){
if(first){
movePointBgSize = movePointBg.getWidth();
movePointAreaBgSize = movePointAreaBg.getWidth();
movePointSize= movePoint.getWidth();
size = dim.dpToPx(sizedp);
one_bg_off = (movePointBgSize - movePointAreaBgSize)/2;
two_bg_off = (movePointAreaBgSize - movePointSize)/2;
one_bg_off = getScale(one_bg_off);
two_bg_off = getScale(two_bg_off);
src = new Rect(0,0,movePointBgSize,movePointBgSize);
src1 = new Rect(0,0,movePointAreaBgSize,movePointAreaBgSize);
src2 = new Rect(0,0,movePointSize,movePointSize);
dst = new Rect(0,0,getScale(movePointBgSize),getScale(movePointBgSize));
dst1 = new Rect(0,0,getScale(movePointAreaBgSize),getScale(movePointAreaBgSize));
dst2 = new Rect(0,0,getScale(movePointSize),getScale(movePointSize));
interval = getScale((movePointAreaBgSize-movePointSize)/2);
dst1.offsetTo(one_bg_off,one_bg_off);
dst2.offsetTo(one_bg_off+two_bg_off,one_bg_off+two_bg_off);
first = false;
}
}
private void selecteMode(){
if(!choosed){
if(Math.abs(startX)>interval&&startX<0&&Math.abs(startY)<interval){
if(pointViewRemind!=null){
pointViewRemind.left(this);
choosed = true;
}
}else if(Math.abs(startX)>interval&&startX>0&&Math.abs(startY)<interval){
if(pointViewRemind!=null){
pointViewRemind.right(this);
choosed = true;
}
}else if(Math.abs(startY)>interval&&startY<0l&&startX>0&&Math.abs(startX)<interval){
if(pointViewRemind!=null){
pointViewRemind.up(this);
choosed = true;
}
}else if(Math.abs(startY)>interval&&startY>0&&Math.abs(startX)<interval){
if(pointViewRemind!=null){
pointViewRemind.down(this);
choosed = true;
}
}
}
}
private int getScale(int a){
float b = (float) (size*1.0/movePointBgSize)*a;
return (int)b;
}
public MovePoint(Context context) {
super(context);
// TODO Auto-generated constructor stub
initBgs(context);
}
public MovePoint(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
initBgs(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
initSizes();
setMeasuredDimension(size, size);
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(movePointBg, src, dst,null);
canvas.drawBitmap(movePointAreaBg, src1, dst1,null);
canvas.drawBitmap(movePoint, src2, dst2,null);
}
private int startX;
private int startY;
private int oldX;
private int oldY;
private long firstTime;
private long lastTime;
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
//Log.e("press", "按下");
startX = (int) event.getX();
startY = (int) event.getY();
oldX = (int)event.getRawX();
oldY = (int)event.getRawY();
firstTime = SystemClock.uptimeMillis();
break;
case MotionEvent.ACTION_MOVE:
//Log.e("move", "移动");
if(!mode){
lastTime = SystemClock.uptimeMillis();
//大于3秒
if(lastTime-firstTime>140){
int a = (int)event.getRawX();
int b = (int)event.getRawY();
if(a==oldX&&b==oldY){
mode = true;
dst2.offsetTo(one_bg_off+two_bg_off, one_bg_off+two_bg_off);
if(pointViewRemind!=null){
pointViewRemind.enterLongPress(this);
}
}
}
}
if(!mode){
int moveX = (int) (event.getX() - startX);
int moveY = (int) (event.getY() - startY);
int all_move_x = moveX+one_bg_off+two_bg_off;
int all_move_y = moveY+one_bg_off+two_bg_off;
//dst2 = new Rect(0,0,getScale(movePointSize),getScale(movePointSize));
dst2.offsetTo(all_move_x,all_move_y);
startX = moveX;
startY = moveY;
selecteMode();
if(all_move_x<0||all_move_x>(getScale(movePointBgSize-movePointSize))){
return false;
}
if(all_move_y<0||all_move_y>(getScale(movePointBgSize-movePointSize))){
return false;
}
}else{
if(pointViewRemind!=null){
//-getScale(movePointBgSize)
int moveX = (int)(event.getRawX()-getScale(movePointBgSize)/4*3);
int moveY = (int)(event.getRawY()-getScale(movePointBgSize)/4*3);
// int moveX = (int)(event.getRawX());
// int moveY = (int)(event.getRawY());
//oldX = moveX;
//oldY = moveY;
//pointViewRemind.getXYShow(moveX,0);
pointViewRemind.getXYShow(moveX,moveY);
}
}
break;
case MotionEvent.ACTION_UP:
if(!mode){
choosed = false;
dst2.offsetTo(one_bg_off+two_bg_off, one_bg_off+two_bg_off);
}else{
dst2.offsetTo(one_bg_off+two_bg_off, one_bg_off+two_bg_off);
}
mode = false;
break;
}
invalidate();
return true;
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2