==反正说多了也没人看,就贴个代码
/**
* Created by cc on 2016/3/18.
*/
public class MEIZUCircleLock extends LinearLayout{
ArrayList
circleArrayList;
ArrayList
moveOverArrayList;
ArrayList
downOverArratList;
OnOverListener mOnOverListener;
boolean bug = false;
/** 行数 **/
final int vertical = 4;
/** 列数 **/
final int horizontal = 3;
Handler hanler = new Handler();
final int clearDownOverListTime = 500;
Runnable runnable = new Runnable() {
@Override
public void run() {
downOverArratList.clear();
}
};
public MEIZUCircleLock(Context context, AttributeSet attrs) {
super(context, attrs);
setOrientation(LinearLayout.VERTICAL);
setBackgroundColor(Color.RED);
setGravity(Gravity.CENTER);
LinearLayout linearLayout;
int number = 0;
circleArrayList = new ArrayList
();
moveOverArrayList = new ArrayList
(); downOverArratList = new ArrayList
(); for(int i=0;i
= 2){ mOnOverListener.onOver(getId(), createString(moveOverArrayList)); }else{ mOnOverListener.onOver(getId(), createString(downOverArratList)); } } moveOverArrayList.clear(); clearOther(null); break; default: Circle circle = test(event); if(circle != null){ if(!moveOverArrayList.contains(circle.getIndex())){ moveOverArrayList.add(circle.getIndex()); }else{ break;//已经记录的不再记录 } circle.setEnabled(false); clearOther(circle); } } return true; } public Circle test(MotionEvent event){ for(Circle circle : circleArrayList){ if(comcupe((int)event.getX(), (int)event.getY(), circle.getOwnX() , circle.getOwnY(), circleWidthAndHeight/2)){ return circle; } } return null; } public String createString(ArrayList
list){ StringBuffer stringBuffer = new StringBuffer(); for(Integer objcet : list){ stringBuffer.append(String.valueOf(objcet)); } return stringBuffer.toString(); } public void clearOther(Circle obejct){ for(Circle circle : circleArrayList){ if(circle != obejct && !circle.isEnabled()){ circle.setEnabled(true); } } } /** 碰撞检测 **/ public boolean comcupe(int myX, int myY, int youX, int youY, int radius){ if( myX >= youX - radius && myY >= youY - radius){ if(myX <= youX + radius && myY <= youY + radius){ return true; } } return false; } int circleWidthAndHeight = 0; class Circle extends LinearLayout{ int ownX, ownY, index; public Circle(Context context,int index) { super(context); setGravity(Gravity.CENTER); if(index >= 10){ index = Integer.parseInt(String.valueOf(index).substring(1, 2)); } this.index = index; if(circleWidthAndHeight == 0){ circleWidthAndHeight = ToPx.spToPx(context, 60); } LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(circleWidthAndHeight, circleWidthAndHeight); setLayoutParams(layoutParams); GradientDrawable enableGradientDrawable = new GradientDrawable(); enableGradientDrawable.setStroke(ToPx.spToPx(context, 1), 0xff000000); enableGradientDrawable.setCornerRadius(circleWidthAndHeight); enableGradientDrawable.setColor(0); GradientDrawable downGradientDrawable = new GradientDrawable(); downGradientDrawable.setStroke(ToPx.spToPx(context, 1), 0xffffffff); downGradientDrawable.setCornerRadius(circleWidthAndHeight); downGradientDrawable.setColor(Color.YELLOW); StateListDrawable stateListDrawable = new StateListDrawable(); stateListDrawable.addState(new int[]{android.R.attr.state_enabled}, enableGradientDrawable); stateListDrawable.addState(new int[]{-android.R.attr.state_pressed}, downGradientDrawable); setBackgroundDrawable(stateListDrawable); TextView textView = new TextView(context); textView.setTextColor(Color.BLACK); textView.setText(String.valueOf(index)); textView.setTextSize(ToPx.spToPx(context, 20)); addView(textView); post(new Runnable() { @Override public void run() { View groupView = (View)getParent(); ownX = getLeft() + groupView.getLeft() + circleWidthAndHeight/2; ownY = getTop() + groupView.getTop() + circleWidthAndHeight/2; } }); } public int getOwnX() { return ownX; } public int getOwnY() { return ownY; } public int getIndex() { return index; } } public static class ToPx { /** dp缩放比例 **/ static float density = 0 ; /** sp缩放比例 **/ static float scaledDensity = 0; public static int dpToPx(Context context, int dp){ if(density == 0){ density = context.getResources().getDisplayMetrics().density; } return (int) (dp * density + 0.5f); } public static int spToPx(Context context, int sp){ if(scaledDensity == 0){ scaledDensity = context.getResources().getDisplayMetrics().scaledDensity; } return (int) (sp * scaledDensity + 0.5f); } } public void setOnOverListener(OnOverListener listener){ this.mOnOverListener = listener; } public void setBug(boolean bug){ this.bug = bug; } public interface OnOverListener{ void onOver(int id, String over); } }