美文网首页
8.25 集训第二十天 【Android Studio 给病娇注

8.25 集训第二十天 【Android Studio 给病娇注

作者: 草莓灵啾啾 | 来源:发表于2019-08-25 23:14 被阅读0次

学习内容

part 1.

程序目录介绍

part 2.

Activity就是一个界面
管理一个界面虫创建到运行到结束的整个过程/生命周期
配置界面 onCreate 这个方法是系统调用的
启动界面 start
重新启动 restart
唤醒界面 resume
暂停界面 pause
销毁界面 destroy

  • 界面启动
    onCreate
    onStart
    onResume

  • 点击home键 回到主界面
    onPause

  • 通过后台重新运行这个程序
    onRestart
    onStart
    onResume

  • 使用返回键返回到主界面
    onPause
    onDestroy

part 3.

  • 使用两种方式界面布局:
    1.xml配置
    2.使用java代码配置
  • 使用java代码来布局界面
    通过添加id号可以唯一标识一个控件或组件(容器)
android:id="@+id/fl-main"
  • xml:解耦 (安卓推荐使用
    什么时候需要用代码创建 什么时候用xml配置
    如果添加的组件是静态的 (变化不多的东西)选择xml配置
    如果需要灵活地操作这个控件,选择代码创建

默认一个Activity对应一个xml配置文件
命名特点:activity_界面功能.xml
xml文件就是一个容器:可以放很多UI控件

  • 布局:这么控件该如何布局
    1.约束布局:ConstraintLayout
    2.线性布局:LinearLayout 垂直 水平
    3.相对布局:RelativeLayout
    4.帧布局:FrameLayout
    5.表格布局:TableLayout GridLayout
    6.绝对布局:AbsoluteLayout
    Android 几种常用布局详解:https://www.jianshu.com/p/2598626b1b04

part 4.

  • 控件
    一个控件就是一个类的具体对象
    ImageView
    属性
    方法
    控件=一个视图=看到的都是控件
    1.学习系统自带的控件 熟悉
    TextView EditText Button ImageView ListView RecycleView ScrollView ViewPager ProgressBar Switch
    2.高级阶段:自定义
    ①在已有的控件基础上加上自己的功能(继承)
    ②自己画
  • 控件的尺寸
    父视图:
    子视图:
    将一个控件添加到一个容器中,控件就是这个容器的子视图,容器就是这个控件的父视图
    1.match_parent 和父视图一样大
    2.wrap_content 包裹内容和控件的内容一样大
    3.20dp 具体尺寸

实际操作

  • 思路:
    使用透明色去替换原有图片对应的像素,立刻获取替换之后的图片 将图片显示在ImageView上

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/fl.main">


    <ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/download"/>

    <ImageView
        android:id="@+id/iv_forground"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     />

</FrameLayout>

MainActivity

public class MainActivity extends AppCompatActivity {
    ImageView forground;
    Bitmap copyBitmap;
    Canvas canvas;
    Paint paint;
    Bitmap orgBitmap;
    @Override//创建一个界面 界面如何布局
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //配置界面
        setContentView(R.layout.activity_main);

        //找到容器里面的图片视图控件
        //findViewById
        forground = findViewById(R.id.iv_forground);

        //将需要操作的图片读取出来 Bitmap
        //BitmapFactory 用于管理位图
        //decodeResource 从工程的资源中去生成一张位图
        //getResources()获取工程的资源
        //R.drawable.fr访问资源路径下 drawable里面的一个文件名为download的资源
        orgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.download2);

        //操作这张图片 用透明色去替换某个位置的颜色
        //不能操作原图 只能copy一份
        //创建一个和原始图片相同环境的空位图
        copyBitmap = Bitmap.createBitmap(orgBitmap.getWidth(),
                orgBitmap.getHeight(), orgBitmap.getConfig());

        //创建一个Canvas 画布-现实中的画板
        canvas = new Canvas(copyBitmap);

        //创建一个画笔
        paint = new Paint();

        //创建一个矩阵
        Matrix matrix = new Matrix();

        //旋转图片
        //matrix.setRotate(90,100,200);
        //平移
        //matrix.setTranslate(50,0);
        //翻转
        // matrix.setScale(-1f,1f);
        //

        //画一幅图
        canvas.drawBitmap(orgBitmap, matrix, paint);

        //显示图片
        forground.setImageBitmap(copyBitmap);

        //给前景图片添加touch事件
        //当有触摸事件发生 系统就会监听这个事件接收并回调这个事件
        forground.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //获取当前事件
                int action = event.getAction();

                //判断状态
                if (action == MotionEvent.ACTION_MOVE) {
                    //获取触摸点的坐标
                    int x = (int) event.getX();
                    int y = (int) event.getY();
                    System.out.println(canvas);
                    System.out.println("x:" + x + "y:" + y);

                    //替换对应的x y像素
                    for (int i = -50; i < 10; i++) {
                        for (int j = -50; j <10; j++) {
                            copyBitmap.setPixel(x+i, y+j, Color.TRANSPARENT);
                        }
                    }
                    //copyBitmap.setPixel(x,y,Color.TRANSPARENT);

                    //canvas.drawBitmap(orgBitmap,new Matrix(),paint);
                    forground.setImageBitmap(copyBitmap);
                }
                return true;
            }
        });
    }
}

style.xml
程序或者某个UI模块都可以有自己的样式Style,可以在values.style.xml里面定义,不需要ActionBar
parent="Theme.AppCompat.Light.NoActionBar"

<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>
最终效果

缺点:鼠标的位置和实际画的位置相差太大

相关文章

网友评论

      本文标题:8.25 集训第二十天 【Android Studio 给病娇注

      本文链接:https://www.haomeiwen.com/subject/acjkectx.html