android实现九宫格程序

所属分类: 软件编程 / Android 阅读数: 42
收藏 0 赞 0 分享

本文实例为大家分享了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);
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享

TextView显示系统时间(时钟功能带秒针变化

用System.currentTimeMillis()可以获取系统当前的时间,我们可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间,可以做一个时钟的功能
收藏 0 赞 0 分享

Android用ListView显示SDCard文件列表的小例子

本文简单实现了用ListView显示SDCard文件列表,目录的回退等功能暂不讨论,获取文件列表,files即为所选择目录下的所有文件列表
收藏 0 赞 0 分享

Android拦截外拨电话程序示例

这篇文章主要介绍了Android拦截外拨电话的示例,大家参考使用吧
收藏 0 赞 0 分享

通过Html网页调用本地安卓(android)app程序代码

如何使用html网页和本地app进行传递数据呢?经过研究,发现还是有方法的,总结了一下,大致有一下几种方式
收藏 0 赞 0 分享

android Textview文字监控(Textview使用方法)

以手机号充值为例,当用户输入最后一位数时候,进行汇率的变换,本文就实现类似这样的功能
收藏 0 赞 0 分享

Android ListView长按弹出菜单二种实现方式示例

这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现
收藏 0 赞 0 分享
查看更多