目的
在AndroidStudio里實(shí)現(xiàn)圖案解鎖
要實(shí)現(xiàn)的基本功能
添加背景圖片,添加一些線岳链,在界面上顯示出來(lái)的是已經(jīng)畫(huà)好了的圖案
具體實(shí)現(xiàn)
1.在xml文件中設(shè)置背景圖片和9個(gè)點(diǎn)的圖片
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/root_layout">
<!--背景圖片-->
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/main_bg"
android:scaleType="fitXY"/>
<!--九個(gè)點(diǎn)的背景圖片-->
<ImageView
android:id="@+id/opView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/op_bg"
android:layout_centerInParent="true"
/>
</RelativeLayout>
2.重寫(xiě)onWindowFocusChanged方法花竞,獲取背景圖片劲件,創(chuàng)建橫豎線和斜線并設(shè)置他們的xy值掸哑,可以根據(jù)所需設(shè)置
public class MainActivity extends AppCompatActivity {
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
//判斷是否已經(jīng)顯示
if (hasFocus){
//獲取容器
RelativeLayout rl=findViewById(R.id.root_layout);
//獲取背景視圖
ImageView iv=findViewById(R.id.opView);
//獲取x,y
int x=iv.getLeft();
int y=iv.getTop();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
//創(chuàng)建用于顯示點(diǎn)的視圖
ImageView dotView=new ImageView(this);
//顯示對(duì)應(yīng)的圖片
dotView.setBackgroundResource(R.drawable.selected_dot);
//創(chuàng)建控件的尺寸
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
float scale=getResources().getDisplayMetrics().density;
params.leftMargin=(int)(x+42*scale)+(int)(112*scale*i);
params.topMargin=(int)(y+279*scale)+(int)(112*scale*j);
System.out.println("zyx"+scale);
//背景視圖的尺寸
//將控件添加到容器
rl.addView(dotView,params);
}
}
//創(chuàng)建六條橫線
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
//創(chuàng)建一個(gè)視圖用于顯示線
ImageView lineView=new ImageView(this);
//設(shè)置圖片
lineView.setBackgroundResource(R.drawable.normal_highlight1);
//創(chuàng)建布局參數(shù)
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
//設(shè)置x零远,y
float scale=getResources().getDisplayMetrics().density;
params.leftMargin = (int) ((int) (x + 54* scale) + (114 * scale * j));
params.topMargin = (int) ((int) (y + 282* scale) + (114 * scale * i));
rl.addView(lineView,params);
}
}
//創(chuàng)建豎線
for (int I = 0; I < 2; I++) {
for (int j = 0; j < 3; j++) {
//創(chuàng)建一個(gè)視圖用于顯示線
ImageView lineView = new ImageView(this);
//設(shè)置圖片
lineView.setBackgroundResource(R.drawable.normal_highlight2);
//創(chuàng)建布局參數(shù)
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//設(shè)置下苗分,x,y
float scale = getResources().getDisplayMetrics().density;
params.leftMargin = (int) ((int) (x + 50* scale) + (114 * scale * j));
params.topMargin = (int) ((int) (y + 292* scale) + (114 * scale * I));
rl.addView(lineView,params);
}
}
//創(chuàng)建右斜線
for (int I = 0; I < 2; I++) {
for (int j = 0; j < 2; j++) {
//創(chuàng)建一個(gè)視圖用于顯示線
ImageView lineView = new ImageView(this);
//設(shè)置圖片
lineView.setBackgroundResource(R.drawable.normal_highlight3);
//創(chuàng)建布局參數(shù)
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//設(shè)置x,y
float scale = getResources().getDisplayMetrics().density;
params.leftMargin = (int) ((int) (x + 40 * scale) + (110 * scale * j));
params.topMargin = (int) ((int) (y + 282 * scale) + (110 * scale * I));
rl.addView(lineView, params);
//創(chuàng)建一個(gè)視圖用于顯示線
ImageView lineView2 = new ImageView(this);
//設(shè)置圖片
lineView2.setBackgroundResource(R.drawable.normal_highlight4);
params.leftMargin = (int) ((int) (x + 80 * scale) + (114 * scale * j));
params.topMargin = (int) ((int) (y + 282 * scale) + (114 * scale * I));
rl.addView(lineView2, params);
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
小結(jié)
今天所學(xué)確實(shí)有些復(fù)雜,要設(shè)置很多東西牵辣,但是很多其實(shí)都差不多摔癣,就像x和y的設(shè)置,后面還會(huì)設(shè)置觸摸,能用手實(shí)現(xiàn)圖案解鎖择浊,而不是這樣已經(jīng)固定好了的圖案戴卜,期待這個(gè)功能完整之后的app