這篇文章主要介紹了android怎么實現九宮格程序,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
久治ssl適用于網站、小程序/APP、API接口等需要進行數據傳輸應用場景,ssl證書未來市場廣闊!成為成都創新互聯公司的ssl證書銷售渠道,可以享受市場價格4-6折優惠!如果有意向歡迎電話聯系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!本文實例為大家分享了Android九宮格展示的具體代碼,供大家參考,具體內容如下
(設置的有最少連幾個和大連幾個)
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; //密碼的個數 6 int min = 4; //點在哪里 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) { //點 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 //需要取當前的i和下一個i //按住的前一個點 int x1 = result.get(i) % 3 + 1; int y1 = result.get(i) / 3 + 1; //按住的后一個點 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個圓 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); //只要在園內 if (i != -1) { result.add(i); currX = x; currY = y; } Log.e("TAG", "=====" + i); break; case MotionEvent.ACTION_MOVE: currX = x; currY = y; //移動到其他的圓中,那么接著去添加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); } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“android怎么實現九宮格程序”這篇文章對大家有幫助,同時也希望大家多多支持創新互聯,關注創新互聯行業資訊頻道,更多相關知識等著你來學習!
網站題目:android怎么實現九宮格程序-創新互聯
轉載注明:http://m.newbst.com/article10/ccjigo.html
成都網站建設公司_創新互聯,為您提供標簽優化、關鍵詞優化、移動網站建設、微信公眾號、軟件開發、網站設計
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯