国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Android - android實(shí)現(xiàn)九宮格程序

android實(shí)現(xiàn)九宮格程序

2022-03-02 15:42dr_abandon Android

這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)九宮格程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android九宮格展示的具體代碼,供大家參考,具體內(nèi)容如下

android實(shí)現(xiàn)九宮格程序

(設(shè)置的有最少連幾個(gè)和最大連幾個(gè))

MainActivity

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class MainActivity extends AppCompatActivity {
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    NineView view = new NineView(this);
    setContentView(view);
    view.setOnPasswordFinishListener(new NineView.OnPasswordFinishListener() {
      @Override
      public void onPasswordFinish(String password) {
        Toast.makeText(getBaseContext(), "密碼:" + password, Toast.LENGTH_SHORT).show();
      }
    });
  }
}

NineView

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
public class NineView extends View {
 
  int width;
  Paint paintback = new Paint();
  Paint paintsrc = new Paint();
  int background;
 
  //保證是正方形
 
  int max = 6; //密碼的個(gè)數(shù)  6
  int min = 4;
 
 
  //點(diǎn)在哪里
  float currX, currY;
 
  public NineView(Context context) {
    super(context);
    init();
  }
 
  public NineView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }
 
  public void init() {
    paintback.setDither(true);
    paintback.setAntiAlias(true);
    paintsrc.setDither(true);
    paintsrc.setAntiAlias(true);
    //171625
    background = Color.rgb(0x17, 0x16, 0x25);
    paintback.setColor(background);
    //3791E6
    paintsrc.setColor(Color.rgb(0x37, 0x91, 0xe6));
 
 
  }
 
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    width = getWidth() / 4;
 
  }
 
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
 
    //清屏
    canvas.drawColor(background);
    //劃線
    if (result.size() > 0) {
 
 
      //點(diǎn)
      int x = result.get(result.size() - 1) % 3 + 1;
      int y = result.get(result.size() - 1) / 3 + 1;
      paintsrc.setStrokeWidth(10);
      canvas.drawLine(x * width, y * width, currX, currY, paintsrc);
      canvas.drawCircle(x * width, y * width, width / 3, paintback);
      if (result.size() > 1) {
        //防止越界
        for (int i = 0; i < result.size() - 1; i++) { // 1 2 3 <=2
          //需要取當(dāng)前的i和下一個(gè)i
          //按住的前一個(gè)點(diǎn)
          int x1 = result.get(i) % 3 + 1;
          int y1 = result.get(i) / 3 + 1;
          //按住的后一個(gè)點(diǎn)
          int x2 = result.get(i + 1) % 3 + 1;
          int y2 = result.get(i + 1) / 3 + 1;
          paintsrc.setStrokeWidth(10);
          canvas.drawLine(x1 * width, y1 * width, x2 * width, y2 * width, paintsrc);
          canvas.drawCircle(x1 * width, y1 * width, width / 3, paintback);
 
        }
      }
    }
    paintsrc.setStrokeWidth(2);
    //9個(gè)圓
    paintsrc.setStyle(Paint.Style.STROKE);
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        canvas.drawCircle((i + 1) * width, (j + 1) * width, width / 3, paintsrc);
      }
    }
    paintsrc.setStyle(Paint.Style.FILL);
    for (Integer integer : result) {
      //i j ; // 8  2 2
      int j = integer / 3 + 1;
      int i = integer % 3 + 1;
      canvas.drawCircle(i * width, j * width, width / 8, paintsrc);
    }
 
  }
 
  //密碼
  List<Integer> result = new ArrayList<>();
 
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        //勾股定理
        int i = isConnPoint(x, y);
        //只要在園內(nèi)
        if (i != -1) {
          result.add(i);
          currX = x;
          currY = y;
        }
        Log.e("TAG", "=====" + i);
        break;
      case MotionEvent.ACTION_MOVE:
        currX = x;
        currY = y;
        //移動(dòng)到其他的圓中,那么接著去添加result
        int point = isConnPoint(x, y);
        if (point != -1 && !result.contains((Integer) point)) {
          result.add(point);
          if (result.size() > max) {
            //reslut清空
            if (onPasswordFinishListener != null)
              onPasswordFinishListener.onPasswordFinish(getPassword());
            result.clear();
          }
        }
        break;
      case MotionEvent.ACTION_UP:
        if (result.size() >= min) {
          if (onPasswordFinishListener != null)
            onPasswordFinishListener.onPasswordFinish(getPassword());
        }
        result.clear();
        break;
    }
    invalidate();
    return true;
  }
 
  public String getPassword() {
    String password = "";
    for (Integer integer : result) {
      password += integer + "";
    }
    return password;
  }
 
 
  //判斷
  public int isConnPoint(float x, float y) {
    //9  width,width width
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (pointOnCircle(x, y, (j + 1) * width, (i + 1) * width)) {
          return i * 3 + j; //0-8
        }
      }
    }
    return -1;
  }
 
  public boolean pointOnCircle(float x, float y, int cx, int cy) {//true
    Log.e("TAG", ((cx - x) * (cx - x) + (cy - y) * (cy - y)) + "");
    Log.e("TAG", ((float) width / 3f) * ((float) width / 3f) + "");
    float i = ((cx - x) * (cx - x) + (cy - y) * (cy - y));
    float j = ((float) width / 3f) * ((float) width / 3f);
    return i < j;
  }
 
 
  public void setOnPasswordFinishListener(OnPasswordFinishListener onPasswordFinishListener) {
    this.onPasswordFinishListener = onPasswordFinishListener;
  }
 
  private OnPasswordFinishListener onPasswordFinishListener;
 
  public interface OnPasswordFinishListener {
    void onPasswordFinish(String password);
  }
 
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/dr_abandon/article/details/53944106

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久69精品久久久久久久电影好 | 欧美喷潮久久久xxxxx | 狠狠久久综合 | 成人影院在线 | 精品久久久久久久久久久久久久 | 久久一区 | 日韩欧美国产一区二区 | 久久精品电影 | 日韩在线视频观看 | 黄色成人在线视频 | 国产欧美一区二区视频 | 亚洲激情在线 | 亚洲国产福利一区 | 欧美一级片在线 | 婷婷综合色 | 欧美视频免费 | 国产成人精品一区二区三区四区 | 色婷婷狠狠 | 亚洲乱码国产乱码精品精的特点 | www.色.com| 久久精品一区二区三区中文字幕 | 国产精品久久久久久久久久久久久久 | 精品国产乱码久久久久久久软件 | 欧美高清免费 | 亚洲精品一区中文字幕乱码 | 99热在线精品播放 | 亚洲 欧美 另类 综合 偷拍 | 欧美一级在线视频 | 一区二区三区www. | 欧美日韩不卡视频 | 国产午夜精品久久 | 日韩有码一区二区三区 | 天堂av中文在线 | 国产毛片在线看 | 亚洲理论电影 | 精品一区二区三区免费毛片 | 精品黄色国产 | 色婷婷亚洲一区二区三区 | www中文字幕在线观看 | 黄视频在线观看免费 | 亚洲狠狠爱 |