Android初体验之撕衣服项目

2019-08-26  本文已影响0人  凌云struggle

一、准备工作

Android 工程目录介绍

工程目录

Activity的⽣生命周期

在日常应用中Activity是与用户交互的接口,它提供了一个用户完成相关操作的窗口。当我们在开发中创建Activity后,通过调用setContentView(View)方法来给该Activity指定一个布局界面,而这个界面就是提供给用户交互的接口。而生命周期就是管理一个界面从创建到运行到结束的整个过程。

Activity生命周期

界面布局

XML界⾯布局

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

布局:这么多控件如何布局

  1. 约束布局 ConstraintLayout
  2. 线性布局 LinearLayout 垂直 水平
  3. 相对布局 RelativeLayout
  4. 帧布局 FrameLayout
  5. 表格布局 tableLayout GridLayout
  6. 绝对布局 AbsoluteLayout
  1. match_parent 和父视图一样大
  2. wrap_content 包裹内容 和控件内容一样大
  3. 具体的大小 比如20dp
android:layout_width="match_parent"
 android:layout_height="match_parent"

XML添加控件

FrameLayout(帧布局)

更改主题样式和图标等

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testcloth">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"  // 图标
        android:label="@string/app_name"  // 名称
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">  // 主题
//  注册界面
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
//  配置作为启动起来的死一个界面
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

使用Java代码创建

&esnp;使用Java代码块来布局界面,通过添加id号android:id="@+id/f1_main"

public void code(){
        // 通过代码布局界面
        // 1. 找一个容器
        FrameLayout container = findViewById(R.id.fl_main);


        // 3. 创建一个子视图
        // 创建ImageView显示图片
        ImageView backgroundImageView = new ImageView(this);
        // 设置属性
        backgroundImageView.setBackgroundColor(Color.GREEN);
        // 添加到容器里
        container.addView(backgroundImageView,container.getWidth(),container.getHeight());
// 2. 设置当前这个界面的内容视图为这个容器
        setContentView(container);

    }
注意:安卓推荐使用XML布局界面

二、撕衣服项目

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

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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/p2"
        />
// 显示操作后图片
    <ImageView
        android:id="@+id/iv_forground"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        />

</FrameLayout>
public class MainActivity extends AppCompatActivity {

    ImageView forground;
    Bitmap orgBitmap;
    Bitmap copyBitmap;
    Canvas canvas;
    Paint paint;

    @Override//创建一个界面 界面如何布局
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        // 配置界面 容器在XML文件里面
        setContentView(R.layout.activity_main);

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

        // 将需要操作的图片读取出来
        // BitmapFactory 用于管理位图
        // decodeResources()  从工程的资源路径中去生成一张图片
        // getResources()  获取工程的资源
        // R.drawable.p1   访问一张图片
        orgBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.p1);

        // 操作这张图片 用透明色去替换某个位置的颜色
        // 不能操作原图 只能拷贝一份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,240,400);
        // 平移
        // matrix.setTranslate(500,0);
        // 翻转 set只作用一次
        // matrix.setScale(-1f,1f);
        // matrix.postTranslate(orgBitmap.getWidth(),0);

        // 画一幅图
        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();

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

                    //canvas.drawBitmap(orgBitmap,new Matrix(),paint);

                    forground.setImageBitmap(copyBitmap);
                }
                return true;
            }
        });

}
    }

三、心得体会

  今天是正式进入Android开发的第一天,可能是因为第一天,今天讲的项目还有些地方不是很理解,项目本身也有一些BUG,不过有了界面之后,学习更有激情了!

上一篇 下一篇

猜你喜欢

热点阅读