Android studio一个手势锁的界面布局

目的:完成一个手势锁的界面布局,对Relative,Contains等布局进行简单的使用,onWindowFocusChanged进行一定的熟练。大概来说,就是增加对界面布局的了解。
技术:(1)简单的RelativeLayout布局
(2)简单的onWindowFocusChanged的使用
(3)内部类Layout的使用
(4)简单视图方法

效果:
效果.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的,系统自带不用管
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        //当Activity的当前Window获得或失去焦点时会被回调此方法。
        // 当回调了这个方法时表示Activity是完全对用户可见的,即是真正的界面构建完成了。
        // 当对话框弹起/消失及Activity新创建及回退等都会调用此方法。
        //相比之下,onResume()方法更多的是指Activity进入了可见的状态,并不是真正的界面构
        // 建完成了,因此获得手机界面的高度和宽度用这个方法比较好。
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {//判断是否已经显示
            RelativeLayout mtemp1 = findViewById(R.id.fir_iv);
            //一个视图链接我们主视图的ID
            ImageView mtemp2 = findViewById(R.id.thr_iv);
            //得到我们所需操作界面
            float mscale = getResources().getDisplayMetrics().density;
            //得到我们手机的分辨率
            int x = (int) mtemp2.getX();
            int y = (int) mtemp2.getY();
            //这个界面的坐标
            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++)//一个循环,因为我们的这个界面要求6
                // 个这种控件
                for (int j = 0; j < 3; j++) {
                    ImageView mtemp3 = new ImageView(this);
                    //一个新的视图,它的环境在我们的MainActivity
                    mtemp3.setBackgroundResource(R.drawable.normal_highlight1);
                    //背景环境资源
                    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_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是所有视图的父类,只有最基本的功能
    //线性布局默认是横向的

    //改容器所有控件都会改变,所以只想改一个控件的话,要单独改这个控件
    //background背景颜色
    //线性布局的方向:orientation方法(也可能是类)里的
    // Vertical纵向
    // horizontal横向

    //layout 有左右Left Right 与开始结束 Start End的Margin边距,注意开始结束是相对这个控件而言的(对于相邻的控件或者手机边距)
    //上边距 Top 下边距 Button
    // 外间距 既然View里面都能用
    //那......所有布局都有(所有的布局类里面,包括控件)应为所有的布局类都维护一个LayoutParams

    //padding是一个控件与自己内容位置的关系 内间距
    //权重按比例分配,layout_weight值越小越重要

    //线性布局,没有交叉
    //相对布局,在Margin_layout的基础上添加了对齐,layout_align(上下左右),两个控件采用一个设置id一个调用方法的方式也是一种方法
    //margin_horizontal="大小"  横向对齐的基础上,移动多少
    //center_horizontal=中间的基础上横向对齐
    //constraint(上下左右)相对大小,设置的默认大小为0不知道不为0时会怎样,应该会失败吧(特有方法)androidx.constraintlayout.widget.ConstraintLayout
    //constraintDimensionRatio(h/w,?:?)的用法不是太清楚高宽比或者宽高比,后面的为前面那个高或者宽
    //要试一试
    //align是什么?
    //资源图片的名称,必须a-z,0-9或者下划线
    //scaleType里面用fitXY进行拉伸
    //R-values-style-noActionbar不要标题栏
    //一个Imageview对象下的setbackgroudResource()方法是什么?
    //坐标的获取......onWindowFocusChanged
    //控件是矩形的
    // RelativeLayout mfirst=findViewById(R.id.fir_iv);
    //ImageView temp=new ImageView(this);
    //创建了一个视图,
    //temp.setBackgroundResource(R.drawable.selected_dot);
    //设置视图背景资源
    //一个控件的尺寸,只是尺寸
    //RelativeLayout.LayoutParams templocation=new RelativeLayout.LayoutParams(
    //      ViewGroup.LayoutParams.WRAP_CONTENT,
    //    ViewGroup.LayoutParams.WRAP_CONTENT);
    //这个类是内部静态类,每个布局都有的,一般
    //这个方法怎么用的,我的理解是,每个布局都有其特有的LayoutParams方法,其创建这个对
    // 象时需要给予两个参数,大小,一个横一个纵宽高,就是宽高......可能不是,去查查
    //getlocalVisibleRect......一些找坐标的方法
    //安卓 在容器中添加的控件需要被window计算
    //Viewgroup的意义?
    //Constraint里面那个对齐方法会调整边框大小
    //重写onWindowFocusChanged这个方法,因为它会在所有子控件测量好后调用,不过这个方法好像会调用多次,占内存
    //addView(?,?)第一个参数是我们要加入的视图,第二个是以怎样的尺寸去添加这个视图
    //get可以上下左右,而且可以得到宽高
    //真正的图片要用另一个方法
    //不同的屏幕分辨率不一样,显示同一张图片的大小不同
    //什么是屏幕的密度?嗯,,,,,分辨率?嗯。
    //float fentemp=getResources().getDisplayMetrics().density;
    // System.out.println(fentemp);//得到拉伸尺寸
    //x,y超出了图片的范围会闪屏,,昨天那个程序的问题
    //setVisibility设置隐藏性
    //onTouchEvent方法,来实现点亮功能


}

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>

没有要标题栏,在res-values-style里面进行了一些修改

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

这是这里要用到的图片,很简单的一个程序,嗯,不看我随笔的话挺短的,而且那几个循环都是一样的原理,理解一个就行了的:
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

感悟:万事开头难吧,在最近的学习中确实有很多的新东西,不过,谁让我决定学了呢。既然做了决定而且自己又有能力去学好,就不应该放弃呀。浮云落霞日悠悠,画池鳞影月空空。伊盼君归心无忧,君归已然青云楼。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容