Android開發(fā)第二天

布局——手勢(shì)解鎖

目錄

  1. 布局
  2. 線性布局(LinearLayout )
  3. 相對(duì)布局(RelativeLayout )
  4. 約束布局(ConstraintLayout)
  5. 注意

詳解

  1. 布局
    用容器來管理子控件宰睡,達(dá)到控制要求
  • 基本分類:
    FrameLayout
    LinearLayout
    ConstraintLayout(主要)
  1. 線性布局(LinearLayout )
    只能將圖片水平蒙揣、豎直位移

基本方法
LayoutParams:
所有的布局類??都維護(hù)?個(gè)LayoutParams extends MarginLayoutParmas
?于管理當(dāng)前這個(gè)布局容器?控件的布局
LinearLayout: LinearLayout.LayoutParams
移動(dòng):
layout_marginStart=“位移屬性”(控件邊緣和其他控件的間距- 外間距)
layout_paddingStart=“位移屬性”(控件內(nèi)部和??邊緣的間距- 內(nèi)間距)

方向
End=Right=右
Start=Left=左
Top=上
Bottom=下

實(shí)現(xiàn)

<LinearLayout 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:orientation="vertical">

    <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/colorAccent"
        />

    <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/colorPrimary"
        android:layout_marginStart="50dp"/>

    <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/colorPrimaryDark"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="50dp"/>
視圖1
  1. 相對(duì)布局(RelativeLayout )
    控件與控件的關(guān)系,必須確定 x 忿等、y 、w 、h

基本方法:包含LinearLayout方法
對(duì)齊:android:layout_alignStart=“對(duì)齊對(duì)象”
權(quán)重:layout_weight(按比例分配)

權(quán)重計(jì)算方式

權(quán)重計(jì)算方式

實(shí)現(xiàn)

<View
    android:id="@+id/v1"
    android:layout_width="300dp"
    android:layout_height="200dp"
    android:background="@color/colorAccent"

    />

<View
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignRight="@+id/v1"
    android:layout_alignBottom="@id/v1"
    android:layout_marginRight="47dp"
    android:layout_marginBottom="33dp"
    android:background="@color/colorPrimary" />
視圖2
  1. 約束布局(ConstraintLayout)

基本方法
寬??例:
layout_constraintDimensionRatio="h,1^2" 寬和?的?例
layout_constraintDimensionRatio=“w,1^2” ?和寬的?例
間距:
app:layout_constraintStart_toStartOf="后一個(gè)對(duì)象"

實(shí)現(xiàn)
1.左右上下間距20,旋轉(zhuǎn)屏幕也不變

<View
   android:layout_width="0dp"
   android:layout_height="0dp"
//不能確定
   android:background="@color/colorPrimary"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintTop_toTopOf="parent"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintBottom_toBottomOf="parent"
//設(shè)置大小
   android:layout_marginLeft="20dp"
   android:layout_marginTop="20dp"
   android:layout_marginRight="20dp"
   android:layout_marginBottom="20dp"
//設(shè)置間距/>
視圖3

2.兩個(gè)控件平分寬度倔喂,一直間距20,旋轉(zhuǎn)屏幕也不變

<androidx.constraintlayout.widget.ConstraintLayout 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"
    >

    <View
        android:id="@+id/v1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorPrimary"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/v2"
        app:layout_constraintBottom_toBottomOf="parent"
 android:layout_marginTop="20dp"
        android:layout_marginStart="20dp"

        android:layout_marginBottom="20dp"
        app:layout_constraintHorizontal_weight="1"/>
    <View
        android:id="@+id/v2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        app:layout_constraintTop_toTopOf="@id/v1"
        app:layout_constraintBottom_toBottomOf="@id/v1"
        app:layout_constraintStart_toEndOf="@id/v1"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        app:layout_constraintHorizontal_weight="1"
        />
</androidx.constraintlayout.widget.ConstraintLayout>
視圖4-橫屏

視圖5-豎屏
  1. 注意
    在容器中添加的控件需要被window計(jì)算/測(cè)量

方式:window->viewGroup->子控件

所以在onCreate onStart onResume無法獲取控件的位置

方法:所有的測(cè)量都是在另外一個(gè)線程中實(shí)現(xiàn)

練習(xí)——手勢(shì)解鎖圖案布局

1.添加控件9個(gè)點(diǎn)圖片 20條線圖片
2.imageview顯示圖片
3.容器來管理子控件RelativeLayout:
下期實(shí)現(xiàn)
4.手的觸摸事件
5.點(diǎn)亮:取消隱藏
6.記錄密碼

activity_main.xml

<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"/>

    <!--9個(gè)點(diǎn)-->
    <ImageView
        android:id="@+id/opView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/op_bg"
        />

</RelativeLayout>

MainActivity.java

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);

            //獲取坐標(biāo)
            int x=iv.getLeft();
            int y=iv.getTop();

            //屏幕密度
            float scale=getResources().getDisplayMetrics().density;

            //創(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)建尺寸
                    RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
                    params.leftMargin=(int)(x+47*scale)+(int)(100*scale*j);
                    params.topMargin=(int)(y+170*scale)+(int)(100*scale*i);

                    rl.addView(lineview,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_highlight2);
                    //創(chuàng)建尺寸
                    RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
                    params.leftMargin=(int)(x+42*scale)+(int)(100*scale*i);
                    params.topMargin=(int)(y+170*scale)+(int)(99*scale*j);

                    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)建尺寸
                    RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
                    params.leftMargin=(int)(x+47*scale)+(int)(100*scale*j);
                    params.topMargin=(int)(y+170*scale)+(int)(99*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_highlight4);
                    //創(chuàng)建尺寸
                    RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
                    params.leftMargin=(int)(x+53*scale)+(int)(100*scale*j);
                    params.topMargin=(int)(y+170*scale)+(int)(99*scale*i);

                    rl.addView(lineview,params);

                }
            }

            //9個(gè)點(diǎn)
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    //創(chuàng)建用于顯示點(diǎn)的視圖
                    ImageView dotview=new ImageView(this);
                    //隱藏視圖
                    //dotview.setVisibility(View.INVISIBLE);
                    //顯示對(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);

                    //控件移到對(duì)應(yīng)點(diǎn)
                    params.leftMargin=(int)(x+35*scale)+(int)(99*scale*i);
                    params.topMargin=(int)(y+163*scale)+(int)(99*scale*j);

                    //將控件添加到容器
                    rl.addView(dotview,params);

                }
            }
        }
    }
顯示結(jié)果
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末靖苇,一起剝皮案震驚了整個(gè)濱河市席噩,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌贤壁,老刑警劉巖悼枢,帶你破解...
    沈念sama閱讀 221,430評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異脾拆,居然都是意外死亡馒索,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,406評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門名船,熙熙樓的掌柜王于貴愁眉苦臉地迎上來绰上,“玉大人,你說我怎么就攤上這事渠驼◎诳椋” “怎么了?”我有些...
    開封第一講書人閱讀 167,834評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵迷扇,是天一觀的道長百揭。 經(jīng)常有香客問我,道長蜓席,這世上最難降的妖魔是什么器一? 我笑而不...
    開封第一講書人閱讀 59,543評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮厨内,結(jié)果婚禮上祈秕,老公的妹妹穿的比我還像新娘。我一直安慰自己隘庄,他們只是感情好踢步,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,547評(píng)論 6 397
  • 文/花漫 我一把揭開白布癣亚。 她就那樣靜靜地躺著丑掺,像睡著了一般。 火紅的嫁衣襯著肌膚如雪述雾。 梳的紋絲不亂的頭發(fā)上街州,一...
    開封第一講書人閱讀 52,196評(píng)論 1 308
  • 那天兼丰,我揣著相機(jī)與錄音,去河邊找鬼唆缴。 笑死鳍征,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的面徽。 我是一名探鬼主播艳丛,決...
    沈念sama閱讀 40,776評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼趟紊!你這毒婦竟也來了氮双?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,671評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤霎匈,失蹤者是張志新(化名)和其女友劉穎戴差,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體铛嘱,經(jīng)...
    沈念sama閱讀 46,221評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡暖释,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,303評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了墨吓。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片球匕。...
    茶點(diǎn)故事閱讀 40,444評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖肛真,靈堂內(nèi)的尸體忽然破棺而出谐丢,到底是詐尸還是另有隱情,我是刑警寧澤蚓让,帶...
    沈念sama閱讀 36,134評(píng)論 5 350
  • 正文 年R本政府宣布乾忱,位于F島的核電站,受9級(jí)特大地震影響历极,放射性物質(zhì)發(fā)生泄漏窄瘟。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,810評(píng)論 3 333
  • 文/蒙蒙 一趟卸、第九天 我趴在偏房一處隱蔽的房頂上張望蹄葱。 院中可真熱鬧,春花似錦锄列、人聲如沸图云。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,285評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽竣况。三九已至,卻和暖如春筒严,著一層夾襖步出監(jiān)牢的瞬間丹泉,已是汗流浹背情萤。 一陣腳步聲響...
    開封第一講書人閱讀 33,399評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留摹恨,地道東北人筋岛。 一個(gè)月前我還...
    沈念sama閱讀 48,837評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像晒哄,于是被迫代替她去往敵國和親睁宰。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,455評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容