![](https://i-blog.csdnimg.cn/blog_migrate/579157d4015f8691d3b58b0e068ee914.png)
最近做一些东西需要用到九宫格解锁,上网找了一些资料,实现的代码属实太多了,让我这个刚入门的新手难以下手,所以自己用控件和自定义View做了一个九宫格解锁,相比其他的九宫格解锁代码减少了许多,废话不多说直接上代码。
因为刚入门命名啥的都不太懂,还有好多需要学习,代码可以优化的地方可能还有很多,凑合着看巴。
MainActivity.java
package com.example.bizideal.test9g;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
//这里使用的是九个TextView,因为自定义View还不够熟练用控件来代替
TextView yuan1, yuan2, yuan3, yuan4, yuan5, yuan6, yuan7, yaun8, yuan9;
//定义数组方便遍历减少代码
int[]textInts = {
R.id.text1, R.id.text2, R.id.text3,
R.id.text4, R.id.text5, R.id.text6,
R.id.text7, R.id.text8, R.id.text9
};
TextView[]texts = {
yuan1, yuan2, yuan3,
yuan4, yuan5, yuan6,
yuan7, yaun8, yuan9
};
int x,y;//触摸坐标
RelativeLayout relayout; //父布局
circleView circleView; // 自定义View
boolean[] lean = new boolean[9];//三个按钮状态
String data = ""; //用户解锁顺序
String password = "123456"; //解锁顺序
Handler Handler = new Handler();
Runnable Runnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
relayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
x = (int) event.getX();
y = (int) event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_MOVE:
circleView.x = x;
circleView.y = y;
for (int i = 0; i < texts.length; i++){
if (isCollision(x,y,texts[i].getX(),texts[i].getY(),texts[i].getWidth(),texts[i].getHeight())){
if (!lean[i]){
//取用户解锁顺序
data += i+1;
//圆点状态
lean[i] = true;
//圆点背景
texts[i].setBackgroundResource(R.drawable.y3);
//取圆中心点
circleView.floatsX.add(texts[i].getX()+texts[i].getWidth()/2);
circleView.floatsY.add(texts[i].getY()+texts[i].getHeight()/2);
}
}
}
break;
case MotionEvent.ACTION_UP:
if (data.equals(password)){
Toast.makeText(MainActivity.this, "解锁成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "解锁失败", Toast.LENGTH_SHORT).show();
for (int i = 0; i < lean.length; i++) {
if (lean[i]){
texts[i].setBackgroundResource(R.drawable.y2);
}
}
}
initialize();
break;
}
circleView.invalidate();
return true;
}
});
}
//初始化数据
private void initView() {
for (int i = 0; i < textInts.length; i++) {
texts[i] = findViewById(textInts[i]);
}
relayout = findViewById(R.id.relayout);
circleView = findViewById(R.id.circle);
}
//碰撞算法
public static boolean isCollision(float x1, float y1, float x2, float y2, float w, float h) {
if (x1 >= x2 && x1 <= x2 + w && y1 >= y2 && y1 <= y2 + h) {
return true;
}
return false;
}
//重新加载布局及初始化数据,这里不会其他的就这样写了
public void initialize(){
circleView.x = circleView.floatsX.get(circleView.floatsX.size()-1);
circleView.y = circleView.floatsY.get(circleView.floatsY.size()-1);
Runnable = new Runnable() {
@Override
public void run() {
for (int i = 0; i < texts.length; i++) {
texts[i].setBackgroundResource(R.drawable.y1);
}
circleView.floatsY = new ArrayList<>();
circleView.floatsX = new ArrayList<>();
data = "";
for (int i = 0; i < lean.length; i++) {
lean[i] = false;
}
}
};
Handler.postDelayed(Runnable,2000);
}
}
circleView.java
package com.example.bizideal.test9g;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
/**
* Created by bizideal on 2021/6/4.
*/
public class circleView extends View {
Paint Paint = new Paint();
List<Float> floatsX = new ArrayList<>();
List<Float> floatsY = new ArrayList<>();
float x;
float y;
public circleView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
Paint.setStrokeWidth(20);
Paint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint.setColor(Color.parseColor("#FFCCCCCC"));
for (int i = 0; i < floatsX.size()-1; i++) {
canvas.drawLine(floatsX.get(i), floatsY.get(i), floatsX.get(i+1), floatsY.get(i+1),Paint);
}
try{
canvas.drawLine(floatsX.get(floatsX.size()-1), floatsY.get(floatsY.size()-1),x,y,Paint);
}catch (Exception e){
e.printStackTrace();
}
Paint.setColor(Color.parseColor("#408aec"));
for (int i = 0; i < floatsX.size(); i++) {
canvas.drawCircle(floatsX.get(i), floatsY.get(i),20,Paint);
}
}
}