android實(shí)現(xiàn)九宮格程序
本文實(shí)例為大家分享了Android九宮格展示的具體代碼,供大家參考,具體內(nèi)容如下
(設(shè)置的有最少連幾個(gè)和最大連幾個(gè))
MainActivity
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
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í)有所幫助,也希望大家多多支持腳本之家。
- Android 九宮格的實(shí)現(xiàn)方法
- android 九宮格滑動(dòng)解鎖開(kāi)機(jī)實(shí)例源碼學(xué)習(xí)
- Android實(shí)現(xiàn)九宮格(GridView中各項(xiàng)平分空間)的方法
- Android打造流暢九宮格抽獎(jiǎng)活動(dòng)效果
- 輕松實(shí)現(xiàn)Android自定義九宮格圖案解鎖
- Android開(kāi)發(fā)之實(shí)現(xiàn)GridView支付寶九宮格
- Android編程之九宮格實(shí)現(xiàn)方法實(shí)例分析
- Android實(shí)現(xiàn)九宮格解鎖
- Android學(xué)習(xí)教程之九宮格圖片展示(13)
- Android布局案例之人人android九宮格
相關(guān)文章
Android如何禁止向EditText控件中輸入內(nèi)容詳解
EditText是接受用戶輸入信息的最重要控件。下面這篇文章主要給大家介紹了關(guān)于Android如何禁止向EditText控件中輸入內(nèi)容的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-09-09Android中使用BitmapShader類來(lái)制作各種圖片的圓角
這篇文章主要介紹了Android中使用BitmapShader類來(lái)制作各種圖片的圓角的方法,文中隨教程講解帶出的例子可以輕松控制圖片圓形的變換,很好很強(qiáng)大,需要的朋友可以參考下2016-04-04Android6.0指紋識(shí)別開(kāi)發(fā)案例
這篇文章主要為大家分享了Android6.0指紋識(shí)別開(kāi)發(fā)案例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Android studio 4.1打包失敗和插件錯(cuò)誤提示的解決
這篇文章主要介紹了Android studio 4.1打包失敗和插件錯(cuò)誤提示的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Android編程之DatePicker和TimePicke簡(jiǎn)單時(shí)間監(jiān)聽(tīng)用法分析
這篇文章主要介紹了Android編程之DatePicker和TimePicke簡(jiǎn)單時(shí)間監(jiān)聽(tīng)用法,結(jié)合具體實(shí)例形式分析了時(shí)間控件DatePicker和TimePicke布局與具體功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-02-02

Android使用ViewPager實(shí)現(xiàn)圖片滑動(dòng)預(yù)覽效果

Android開(kāi)發(fā)之permission動(dòng)態(tài)權(quán)限獲取詳解