当前位置:  首页  >  PHP资讯  >  业界资讯

Android自定义UI手势密码简单版

这篇文章主要为大家详细介绍了Android自定义UI手势密码简单版

先看看效果图:

ImageLockActivity

 package com.example.imagelock; import com.example.view.NinePointLineView; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; public class ImageLockActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); View v = new NinePointLineView(this); setContentView(v); } } 

NinePointLineView

 package com.example.view; import com.example.imagelock.R; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Cap; import android.graphics.Typeface; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.Toast; public class NinePointLineView extends View { /** * 定义3个Paint,还有一个坐标圆点图片 */ Paint linePaint = new Paint(); Paint whiteLinePaint = new Paint(); Paint textPaint = new Paint(); Bitmap defaultBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.lock); PointInfo[] points = new PointInfo[9]; int width, height; //坐标点的半径长度 int defaultBitmapRadius = defaultBitmap.getWidth() / 2; //绘制密码时候出现的原点的直径,半径 Bitmap selectedBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.indicator_lock_area); int selectedBitmapDiameter = selectedBitmap.getWidth(); int selectedBitmapRadius = selectedBitmapDiameter / 2; StringBuffer lockString = new StringBuffer(); Context context; /** 构造器*********************************************/ public NinePointLineView(Context context) { super(context); this.cOntext= context; this.setBackgroundColor(Color.WHITE); initPaint(); } public NinePointLineView(Context context, AttributeSet attrs) { super(context, attrs); this.cOntext= context; this.setBackgroundColor(Color.WHITE); initPaint(); } private void initPaint() { //线--包裹9个原点 linePaint.setColor(Color.RED); linePaint.setStrokeWidth(defaultBitmap.getWidth()); linePaint.setAntiAlias(true); linePaint.setStrokeCap(Cap.ROUND); //线内--比原点直径少5 whiteLinePaint.setColor(Color.GREEN); whiteLinePaint.setStrokeWidth(defaultBitmap.getWidth() - 5); whiteLinePaint.setAntiAlias(true); whiteLinePaint.setStrokeCap(Cap.ROUND); //字体设置 textPaint.setTextSize(30); textPaint.setAntiAlias(true); textPaint.setTypeface(Typeface.MONOSPACE); } /********************************************************** * 测量 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { width = getWidth(); height = getHeight(); if (width != 0 && height != 0) { initPoints(points); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } /** * 初始化原点 */ private void initPoints(PointInfo[] points) { int len = points.length; //2个原点的间距 int seletedSpacing = (width - selectedBitmapDiameter * 3) / 4; //第1个原点的坐标 int seletedX = seletedSpacing; int seletedY = height - width + seletedSpacing; //第1个原点内部的小圆的坐标 int defaultX = seletedX + selectedBitmapRadius - defaultBitmapRadius; int defaultY = seletedY + selectedBitmapRadius - defaultBitmapRadius; for (int i = 0; i  seletedX && x <(seletedX + selectedBitmapDiameter); boolean inY = y > seletedY && y <(seletedY + selectedBitmapDiameter); return (inX && inY); } } } 

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

内容推荐:免费高清PNG素材下载
吐了个 "CAO" !
扫码关注 PHP1 官方微信号
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved PHP1.CN 第一PHP社区 版权所有 京ICP备19059560号-4