Android studio一個(gè)手勢(shì)鎖的界面布局

目的:完成一個(gè)手勢(shì)鎖的界面布局,對(duì)Relative,Contains等布局進(jìn)行簡(jiǎn)單的使用,onWindowFocusChanged進(jìn)行一定的熟練。大概來(lái)說(shuō),就是增加對(duì)界面布局的了解。
技術(shù):(1)簡(jiǎn)單的RelativeLayout布局
(2)簡(jiǎn)單的onWindowFocusChanged的使用
(3)內(nèi)部類Layout的使用
(4)簡(jiǎn)單視圖方法

效果:
效果.jpg

代碼:
這是我們的源代碼
package ly.pxd.mtext;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//讀取XML的,系統(tǒng)自帶不用管
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        //當(dāng)Activity的當(dāng)前Window獲得或失去焦點(diǎn)時(shí)會(huì)被回調(diào)此方法。
        // 當(dāng)回調(diào)了這個(gè)方法時(shí)表示Activity是完全對(duì)用戶可見(jiàn)的,即是真正的界面構(gòu)建完成了。
        // 當(dāng)對(duì)話框彈起/消失及Activity新創(chuàng)建及回退等都會(huì)調(diào)用此方法。
        //相比之下,onResume()方法更多的是指Activity進(jìn)入了可見(jiàn)的狀態(tài),并不是真正的界面構(gòu)
        // 建完成了,因此獲得手機(jī)界面的高度和寬度用這個(gè)方法比較好。
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {//判斷是否已經(jīng)顯示
            RelativeLayout mtemp1 = findViewById(R.id.fir_iv);
            //一個(gè)視圖鏈接我們主視圖的ID
            ImageView mtemp2 = findViewById(R.id.thr_iv);
            //得到我們所需操作界面
            float mscale = getResources().getDisplayMetrics().density;
            //得到我們手機(jī)的分辨率
            int x = (int) mtemp2.getX();
            int y = (int) mtemp2.getY();
            //這個(gè)界面的坐標(biāo)
            for (int i = 0; i < 2; i++)//豎線
                for (int j = 0; j < 3; j++) {
                    ImageView mtemp3 = new ImageView(this);
                    mtemp3.setBackgroundResource(R.drawable.normal_highlight2);
                    RelativeLayout.LayoutParams temp = new RelativeLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT
                    );
                    temp.leftMargin = (int) (x + 42 * mscale) + (int) (99 * mscale) * j;
                    temp.topMargin = (int) (y + 170 * mscale) + (int) (99 * mscale) * i;
                    mtemp1.addView(mtemp3, temp);
                }
            for (int i = 0; i < 2; i++)//一個(gè)循環(huán),因?yàn)槲覀兊倪@個(gè)界面要求6
                // 個(gè)這種控件
                for (int j = 0; j < 3; j++) {
                    ImageView mtemp3 = new ImageView(this);
                    //一個(gè)新的視圖,它的環(huán)境在我們的MainActivity
                    mtemp3.setBackgroundResource(R.drawable.normal_highlight1);
                    //背景環(huán)境資源
                    RelativeLayout.LayoutParams temp = new RelativeLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT
                    );//一個(gè)控件的尺寸,定義時(shí)要表示出它的寬高
                    temp.leftMargin = (int) (x + 42 * mscale) + (int) (99 * mscale) * i;
                    temp.topMargin = (int) (y + 170 * mscale) + (int) (99 * mscale) * j;
                    mtemp1.addView(mtemp3, temp);
                }
            for (int i = 0; i < 2; i++)//左斜線
                for (int j = 0; j < 2; j++) {
                    ImageView mtemp3 = new ImageView(this);
                    mtemp3.setBackgroundResource(R.drawable.normal_highlight3);
                    RelativeLayout.LayoutParams temp = new RelativeLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT
                    );
                    temp.leftMargin = (int) (x + 42 * mscale) + (int) (99 * mscale) * i;
                    temp.topMargin = (int) (y + 170 * mscale) + (int) (99 * mscale) * j;
                    mtemp1.addView(mtemp3, temp);
                }
            for (int i = 0; i < 2; i++)//右斜線
                for (int j = 0; j < 2; j++) {
                    ImageView mtemp3 = new ImageView(this);
                    mtemp3.setBackgroundResource(R.drawable.normal_highlight4);
                    RelativeLayout.LayoutParams temp = new RelativeLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT
                    );
                    temp.leftMargin = (int) (x + 50 * mscale) + (int) (99 * mscale) * i;
                    temp.topMargin = (int) (y + 170 * mscale) + (int) (99 * mscale) * j;
                    mtemp1.addView(mtemp3, temp);
                }
            for (int i = 0; i < 3; i++)//右斜線
                for (int j = 0; j < 3; j++) {
                    ImageView mtemp3 = new ImageView(this);
                    mtemp3.setBackgroundResource(R.drawable.selected_dot);
                    RelativeLayout.LayoutParams temp = new RelativeLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT
                    );
                    temp.leftMargin = (int) (x + 35 * mscale) + (int) (99 * mscale) * i;
                    temp.topMargin = (int) (y + 165* mscale) + (int) (99 * mscale) * j;
                    mtemp1.addView(mtemp3, temp);
                }
        }

    }
    //這下面是我的一些隨筆可看可不看
    //view是所有視圖的父類,只有最基本的功能
    //線性布局默認(rèn)是橫向的

    //改容器所有控件都會(huì)改變,所以只想改一個(gè)控件的話,要單獨(dú)改這個(gè)控件
    //background背景顏色
    //線性布局的方向:orientation方法(也可能是類)里的
    // Vertical縱向
    // horizontal橫向

    //layout 有左右Left Right 與開(kāi)始結(jié)束 Start End的Margin邊距,注意開(kāi)始結(jié)束是相對(duì)這個(gè)控件而言的(對(duì)于相鄰的控件或者手機(jī)邊距)
    //上邊距 Top 下邊距 Button
    // 外間距 既然View里面都能用
    //那......所有布局都有(所有的布局類里面,包括控件)應(yīng)為所有的布局類都維護(hù)一個(gè)LayoutParams

    //padding是一個(gè)控件與自己內(nèi)容位置的關(guān)系 內(nèi)間距
    //權(quán)重按比例分配,layout_weight值越小越重要

    //線性布局,沒(méi)有交叉
    //相對(duì)布局,在Margin_layout的基礎(chǔ)上添加了對(duì)齊,layout_align(上下左右),兩個(gè)控件采用一個(gè)設(shè)置id一個(gè)調(diào)用方法的方式也是一種方法
    //margin_horizontal="大小"  橫向?qū)R的基礎(chǔ)上,移動(dòng)多少
    //center_horizontal=中間的基礎(chǔ)上橫向?qū)R
    //constraint(上下左右)相對(duì)大小,設(shè)置的默認(rèn)大小為0不知道不為0時(shí)會(huì)怎樣,應(yīng)該會(huì)失敗吧(特有方法)androidx.constraintlayout.widget.ConstraintLayout
    //constraintDimensionRatio(h/w,?:?)的用法不是太清楚高寬比或者寬高比,后面的為前面那個(gè)高或者寬
    //要試一試
    //align是什么?
    //資源圖片的名稱,必須a-z,0-9或者下劃線
    //scaleType里面用fitXY進(jìn)行拉伸
    //R-values-style-noActionbar不要標(biāo)題欄
    //一個(gè)Imageview對(duì)象下的setbackgroudResource()方法是什么?
    //坐標(biāo)的獲取......onWindowFocusChanged
    //控件是矩形的
    // RelativeLayout mfirst=findViewById(R.id.fir_iv);
    //ImageView temp=new ImageView(this);
    //創(chuàng)建了一個(gè)視圖,
    //temp.setBackgroundResource(R.drawable.selected_dot);
    //設(shè)置視圖背景資源
    //一個(gè)控件的尺寸,只是尺寸
    //RelativeLayout.LayoutParams templocation=new RelativeLayout.LayoutParams(
    //      ViewGroup.LayoutParams.WRAP_CONTENT,
    //    ViewGroup.LayoutParams.WRAP_CONTENT);
    //這個(gè)類是內(nèi)部靜態(tài)類,每個(gè)布局都有的,一般
    //這個(gè)方法怎么用的,我的理解是,每個(gè)布局都有其特有的LayoutParams方法,其創(chuàng)建這個(gè)對(duì)
    // 象時(shí)需要給予兩個(gè)參數(shù),大小,一個(gè)橫一個(gè)縱寬高,就是寬高......可能不是,去查查
    //getlocalVisibleRect......一些找坐標(biāo)的方法
    //安卓 在容器中添加的控件需要被window計(jì)算
    //Viewgroup的意義?
    //Constraint里面那個(gè)對(duì)齊方法會(huì)調(diào)整邊框大小
    //重寫(xiě)onWindowFocusChanged這個(gè)方法,因?yàn)樗鼤?huì)在所有子控件測(cè)量好后調(diào)用,不過(guò)這個(gè)方法好像會(huì)調(diào)用多次,占內(nèi)存
    //addView(?,?)第一個(gè)參數(shù)是我們要加入的視圖,第二個(gè)是以怎樣的尺寸去添加這個(gè)視圖
    //get可以上下左右,而且可以得到寬高
    //真正的圖片要用另一個(gè)方法
    //不同的屏幕分辨率不一樣,顯示同一張圖片的大小不同
    //什么是屏幕的密度?嗯,,,,,分辨率?嗯。
    //float fentemp=getResources().getDisplayMetrics().density;
    // System.out.println(fentemp);//得到拉伸尺寸
    //x,y超出了圖片的范圍會(huì)閃屏,,昨天那個(gè)程序的問(wèn)題
    //setVisibility設(shè)置隱藏性
    //onTouchEvent方法,來(lái)實(shí)現(xiàn)點(diǎn)亮功能


}

xml文件

<?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/fir_iv">
    <ImageView
        android:id="@+id/sec_iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/main_bg"
        android:scaleType="fitXY"/>
    <ImageView
        android:id="@+id/thr_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/op_bg"
        android:layout_centerInParent="true"/>
</RelativeLayout>

沒(méi)有要標(biāo)題欄,在res-values-style里面進(jìn)行了一些修改

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

這是這里要用到的圖片,很簡(jiǎn)單的一個(gè)程序,嗯,不看我隨筆的話挺短的,而且那幾個(gè)循環(huán)都是一樣的原理,理解一個(gè)就行了的:
main_bg.png
normal_highlight1.png
normal_highlight2.png
normal_highlight3.png
normal_highlight4.png
op_bg.png
selected_dot.png
wrong_dot.png
wrong_highlight1.png
wrong_highlight2.png
wrong_highlight3.png
wrong_highlight4.png

感悟:萬(wàn)事開(kāi)頭難吧,在最近的學(xué)習(xí)中確實(shí)有很多的新東西,不過(guò),誰(shuí)讓我決定學(xué)了呢。既然做了決定而且自己又有能力去學(xué)好,就不應(yīng)該放棄呀。浮云落霞日悠悠,畫(huà)池鱗影月空空。伊盼君歸心無(wú)憂,君歸已然青云樓。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容