国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

自定義控件之九宮格

2019-11-09 15:05:03
字體:
來源:轉載
供稿:網友

自定義控件手寫九宮格

//屬性封裝部分

public class Circle {    public  int x;    public  int y;    public  int radio;    public  int color;    public  boolean isSelect;    //用來作為個人標示使用    public  int number;}

//自定義邏輯部分

package view;

import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.Toast;import java.util.ArrayList;import java.util.List;/** * Created by Administrator on 2017/1/13. */public class Sudoku extends View {   //畫筆顏色    public final  static  int Before_Color=Color.parseColor("#000000");    //畫之后的顏色    public final  static  int After_Color=Color.parseColor("#ff0000");    //點的集合    List<Circle> circleList=new ArrayList<>();    //線的集合    List<Circle> lineList=new ArrayList<>();    PRivate int width;    private  int height;    private  int radio;    Paint paint;    public Sudoku(Context context) {        super(context);    }    public Sudoku(Context context, AttributeSet attrs) {        super(context, attrs);    }    public Sudoku(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        width=getWidth();        height=getHeight();        //如果寬或高為0,則無意義        if (height==0||width==0){            return;        }        //得到半徑,把寬平均分為10份        radio=width/10;        addList();        Circle(canvas);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()){            case MotionEvent.ACTION_DOWN:                break;            case MotionEvent.ACTION_MOVE:                 move(event);                break;            case MotionEvent.ACTION_UP:                up();                break;        }        return true;    }  public void move(MotionEvent event){      int x= (int) event.getX();      int y= (int) event.getY();      for (int i = 0; i < circleList.size(); i++) {          //xx x的平法          int xx= (int) Math.pow((x-circleList.get(i).x),2);          int yy= (int) Math.pow((y-circleList.get(i).y),2);          int sum=xx+yy;          //點擊在園內          if (sum<=Math.pow(radio,2)){              //將狀態設置為選中           circleList.get(i).isSelect=true;              //滑動時改變畫筆顏色             circleList.get(i).color=After_Color;               //設置判斷避免重復              boolean isFlag=false;              for (int j = 0; j < lineList.size(); j++) {                  //通過數字判斷存在不存在                  if (lineList.get(j).number==circleList.get(i).number)                  {                      isFlag=true;                  }              }              //如果不存在就加入              if (isFlag==false){                  Circle circle=new Circle();                  circle.x=circleList.get(i).x;                  circle.y=circleList.get(i).y;                  circle.color=circleList.get(i).color;                  circle.number=circleList.get(i).number;                  lineList.add(circle);              }              //同時刷新ui              postInvalidate();          }      }  }     public  void up(){        String passWord="";        for (int i = 0; i < lineList.size(); i++) {            password+=lineList.get(i).number+"";        }        Toast.makeText(getContext(),"當前密碼是"+password,Toast.LENGTH_LONG).show();        //清理屏幕        for (int i = 0; i < circleList.size(); i++) {            circleList.get(i).color=Before_Color;            circleList.get(i).isSelect=false;        }        //清空        lineList.clear();        //刷新        postInvalidate();    }    public void addList(){        for (int i = 1; i <=9 ; i++) {            Circle cirle=new Circle();            //當i<=3時,畫第一排三個點的縱坐標相同            if (i<=3){               //當i<=3時,畫第一排三個點的橫坐標相同                cirle.y=height/2-radio*3;            }else if (i>3 && i<=6){                //當i>3&&i<6時,畫第二排三個點的縱坐標相同                cirle.y=height/2;            }else {                //畫第三排的圓                cirle.y=height/2+radio*3;            }            //i==1、4、7每個圓的橫坐標相同            if (i==1||i==4||i==7){                cirle.x=radio*2;            }else if (i==2||i==5||i==8){                cirle.x=radio*5;            }else{                cirle.x=radio*8;            }            //把點和半徑存入集合            cirle.radio=radio;            //默認點的顏色為黑色            cirle.color=Before_Color;            //默認所有點不選中            cirle.isSelect=false;            //把點的數量頁作為屬性存入集合            cirle.number=i;            //把點裝入集合            circleList.add(cirle);        }    }    //繪制圓    public void Circle(Canvas canvas) {        //實例化畫筆        paint=new Paint();        paint.setStrokeWidth(3);        paint.setAntiAlias(true);        paint.setStyle(Paint.Style.STROKE);        //循環點的集合,然后畫圓        for (int i = 0; i < circleList.size(); i++) {            //設置畫圓的顏色            paint.setColor(circleList.get(i).color);            //繪制圓            canvas.drawCircle(circleList.get(i).x,circleList.get(i).y,circleList.get(i).radio,paint);        }                boolean isFirst=false;        int startX=0;        int startY=0;        //劃線的方法        for (int i = 0; i < lineList.size(); i++) {            if (isFirst==false){                isFirst=true;                startX=lineList.get(i).x;                startY=lineList.get(i).y;            }else{                paint.setColor(lineList.get(i).color);                canvas.drawLine(startX,startY,lineList.get(i).x,lineList.get(i).y,paint);                startX=lineList.get(i).x;                startY=lineList.get(i).y;            }        }    }

}

//xml布局部分

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <view.Sudoku        android:layout_width="match_parent"        android:layout_height="match_parent"        /></LinearLayout>


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 白城市| 利川市| 山阳县| 潼南县| 台安县| 洱源县| 龙海市| 廊坊市| 潮安县| 治多县| 林甸县| 林甸县| 中阳县| 清新县| 涿州市| 正镶白旗| 松阳县| 合阳县| 麦盖提县| 旌德县| 巴彦淖尔市| 龙州县| 华坪县| 三台县| 沭阳县| 历史| 绥棱县| 大竹县| 曲麻莱县| 衢州市| 利津县| 博乐市| 临清市| 米脂县| 山阴县| 永和县| 龙岩市| 凤山市| 蓝山县| 怀仁县| 宿迁市|