Android实战_note1(MyMirror_一款小型摄像处理的App)

最近在实战做明日科技的一个叫《魔镜》的APP,接触到不少有趣的技能tip,这里记录一下。功能点主要有启动页、摄像头设置、亮度调节、相机焦距调节。界面选择换镜框、吹气起雾、长安碎屏、摇一摇换镜框、系统帮助等子功能,博客会陆续更近

本文自觉有些有趣的地方(这里仅做摘要,详见文中):
  • handler.sendEmptyMessageDelayed()
  • 1.3 中的修改全局配置文件 AndroidManifest.xml
  • name属性表示颜色变量名,在java中调用时就是调用这个名称;#3F51B5表示颜色值;调用格式为@color/setbackground。其中颜色值可以直接在xml中输入,或者点击色块,在弹出窗口中进行选择或输入设置;(如文《资源准备1:颜色资源》中图)
  • 资源准备4:styles样式资源
    MyTheme表示样式的名称,
    android:windowFrame表示窗口的背景颜色,
    android:windowBackground表示窗口的背景图片,
    android:windowIsTranslucent表示窗口是否显示,
    android:windowNoTitle表示窗口是否有标题
  • 一般情况,除了直接使用放在drawable目录下的图片,其实drawable的用法都与XML有关,使用shape、layer-list等标签绘制一些背景,还可以通过selector标签定义view的状态效果。
  • ImageView.ScaleType设置图解

功能点1.快速构建启动页:

此时目录结构:




1.1 Activity.java全文:
注意代码中的注释,其中 handler.sendEmptyMessageDelayed(1,3000); 这个方法比较有趣

package com.example.mymirror.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;

import com.example.mymirror.MainActivity;
import com.example.mymirror.R;

public class GuideActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        handler.sendEmptyMessageDelayed(1,3000);//传递what值为1的,空消息,延迟3秒
    }
    //消息处理,接收消息
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 1){
                //创建意图
                Intent intent = new Intent(GuideActivity.this, MainActivity.class);
                startActivity(intent);//跳转界面
                finish();//关闭界面
            }
            return false;
        }
    });

    //屏蔽返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            return false;
        }
        return false;
    }
}

1.2 布局问价设置背景图片:

android:background="@mipmap/background"

全文:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    android:background="@mipmap/background"
    tools:context="com.example.mymirror.activity.GuideActivity">

</android.support.constraint.ConstraintLayout>

1.3 修改全局配置文件 AndroidManifest.xml

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

    <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/Theme.AppCompat.Light.NoActionBar">
        <activity android:name=".activity.GuideActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity"></activity>
    </application>

</manifest>

到此,运行程序,结果:启动页界面停顿3秒后,切到MainActivity上:

启动页

停顿3秒后,切到MainActivity上

2.主窗体模块设计

资源准备1:颜色资源
打开app/res/values目录下的colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="setbackground">#a6000000</color>
    <color name="white">#FFFFFF</color>
    <color name="setbackground2">#3FFFFFFF</color>
    <color name="setbackground3">#00000000</color>
    <color name="setbackground4">#7e000000</color>
</resources>

name属性表示颜色变量名,在java中调用时就是调用这个名称;#3F51B5表示颜色值;调用格式为@color/setbackground。其中颜色值可以直接在xml中输入,或者点击下图框中的色块,在弹出窗口中进行选择或输入设置:



资源准备2:尺寸资源

调用格式为@dimen/dp_0

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--尺寸资源 -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="dp_0">0dp</dimen>
    <dimen name="dp_3">3dp</dimen>
    <dimen name="dp_5">5dp</dimen>
    <dimen name="dp_10">10dp</dimen>
    <dimen name="dp_18">18dp</dimen>
    <dimen name="dp_20">20dp</dimen>
    <dimen name="dp_30">30dp</dimen>
    <dimen name="dp_45">45dp</dimen>
    <dimen name="dp_55">55dp</dimen>
    <dimen name="dp_160">160dp</dimen>
    <dimen name="dp_200">200dp</dimen>
    <dimen name="dp_300">300dp</dimen>
</resources>

资源准备3:字符串资源

<resources>
    <string name="app_name">MyMirror</string>
    <string name="back_txt">返回</string>
</resources>

资源准备4:styles样式资源

MyTheme表示样式的名称,
android:windowFrame表示窗口的背景颜色,
android:windowBackground表示窗口的背景图片,
android:windowIsTranslucent表示窗口是否显示,
android:windowNoTitle表示窗口是否有标题

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="MyTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowFrame">@android:color/transparent</item>
        <item name="android:windowBackground">@color/setbackground4</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
    </style>
</resources>

资源准备5:drawable图片资源

一般情况,除了直接使用放在drawable目录下的图片,其实drawable的用法都与XML有关,使用shape、layer-list等标签绘制一些背景,还可以通过selector标签定义view的状态效果。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false" android:drawable="@drawable/back_shape_pink"/>
    <item android:state_pressed="true" android:drawable="@drawable/back_shape_white"/>
</selector>



<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--实心 -->
    <solid android:color="@color/setbackground2"/>
    <!--描边 -->
    <stroke android:color="@color/white"
        android:width="@dimen/dp_3"/>
    <!--圆角 -->
    <corners android:radius="@dimen/dp_20"/>
    <!--间隔 -->
    <padding android:left="@dimen/dp_10"
        android:top="@dimen/dp_5"
        android:bottom="@dimen/dp_5"
        android:right="@dimen/dp_10"/>
</shape>



<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/setbackground2"/>
    <stroke android:color="@color/colorAccent"
        android:width="@dimen/dp_3"/>
    <corners android:radius="@dimen/dp_20"/>
    <padding android:left="@dimen/dp_10"
        android:top="@dimen/dp_5"
        android:bottom="@dimen/dp_5"
        android:right="@dimen/dp_10"/>
</shape>

资源准备6:mipmap资源(项目完成后附上码云地址)

主窗体布局:

即activity_main.xml布局,布局设计框架如下:

        <!--SurfaceView控件,主界面最底层的显示区域,用来显示摄像头的内容-->
            <!--PictureView:自定义控件,在此控件上完成镜框更换的功能,布满整个显示区域-->
            <!--FunctionView:自定义控件,功能组合控件,将系统帮助、选择相框和亮度调节等3个功能组合到一起,形成主界面顶部功能区-->
            <!--LinearLayout布局:底部焦距调节功能-->
                <!--缩小焦距按钮-->
                <!--拖动条控件-->
                <!--放大焦距按钮-->
            <!--DrawView:吹雾擦雾图层-->

activity_main.xml全文:

附:ImageView.ScaleType设置图解

<?xml version="1.0"  encoding="utf-8"?>
<RelativeLayout 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=".activity.MainActivity">
    <!--此处添加主界面上的布局组件 -->
    <SurfaceView
        android:id="@+id/surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <com.example.mymirror.view.PictureView
        android:id="@+id/picture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"/>
    <com.example.mymirror.view.FunctionView
        android:id="@+id/function"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:id="@+id/bottom_bar"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:padding="@dimen/dp_10">
        <!-- 放大、缩小按钮和拖动条布局代码-->
        <ImageView
            android:id="@+id/minus"
            android:layout_marginLeft="@dimen/dp_30"
            android:layout_width="@dimen/dp_45"
            android:layout_height="@dimen/dp_45"
            android:src="@mipmap/downsmall"
            android:scaleType="centerInside"/>
        <SeekBar
            android:id="@+id/seekbar"
            android:layout_width="@dimen/dp_0"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:progress="0"
            android:thumbOffset="@dimen/dp_0"/>
        <ImageView
            android:id="@+id/add"
            android:layout_marginRight="@dimen/dp_30"
            android:layout_width="@dimen/dp_45"
            android:layout_height="@dimen/dp_45"
            android:src="@mipmap/uplarge"
            android:scaleType="centerInside"/>
    </LinearLayout>
    <com.example.mymirror.view.DrawView
        android:id="@+id/draw_glasses"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>

</RelativeLayout>

Design界面显示:

创建view包,在包中添加PictureView、FunctionView、DrawView三个java文件用于描述自定义控件:

layout文件夹下创建文件view_function.xml,存放顶部功能栏布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/dp_10"
    android:background="@color/setbackground"
    android:gravity="center">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_30">
        <!--  问号 按钮-->
        <ImageView
            android:id="@+id/hint"
            android:layout_centerVertical="true"
            android:layout_width="@dimen/dp_45"
            android:layout_height="@dimen/dp_45"
            android:scaleType="centerInside"
            android:src="@mipmap/hint"/>
        <!--  选择相框 按钮-->
        <ImageView
            android:id="@+id/choose"
            android:layout_width="@dimen/dp_45"
            android:layout_height="@dimen/dp_45"
            android:layout_centerVertical="true"
            android:src="@mipmap/choose"
            android:scaleType="centerInside"
            android:layout_alignParentRight="true"/>
        <!--  灯泡图标-->
        <ImageView
            android:id="@+id/cencer"
            android:layout_width="@dimen/dp_45"
            android:layout_height="@dimen/dp_45"
            android:src="@mipmap/light"
            android:scaleType="centerInside"
            android:layout_centerInParent="true"/>
        <!--  减亮度 按钮-->
        <ImageView
            android:id="@+id/light_down"
            android:layout_width="@dimen/dp_45"
            android:layout_height="@dimen/dp_45"
            android:layout_centerVertical="true"
            android:src="@mipmap/downlight"
            android:scaleType="centerInside"
            android:layout_toLeftOf="@+id/cencer"
            android:layout_marginRight="@dimen/dp_20"/>
        <!--  加亮度 按钮-->
        <ImageView
            android:id="@+id/light_up"
            android:layout_width="@dimen/dp_45"
            android:layout_height="@dimen/dp_45"
            android:layout_centerVertical="true"
            android:src="@mipmap/uplight"
            android:scaleType="centerInside"
            android:layout_toRightOf="@+id/cencer"
            android:layout_marginLeft="@dimen/dp_20"/>
    </RelativeLayout>

</LinearLayout>

对应预览图:

FunctionView全文:

package com.example.mymirror.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * Created by 700 on 2018/8/23.
 */

public class FunctionView extends LinearLayout implements View.OnClickListener{
    private LayoutInflater mInflater;//声明寻找XML文件类
    private ImageView hint,choose,down,up;//控件对象
    /**
     * 回调接口,4个按钮
     */
    private onFunctionViewItemClickListener listener;
    public interface onFunctionViewItemClickListener{
        void hint();//提示
        void choose();//选择相框
        void down();//减少亮度
        void up();//增加亮度
    }
    /**
     * 初始化构造函数
     */
    public FunctionView(Context context) {
        super(context);
    }

    public FunctionView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FunctionView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onClick(View v) {
        
    }

    public void setOnFunctionViewItemClickListener(onFunctionViewItemClickListener monFunctionViewItemClickListener){
        this.listener = monFunctionViewItemClickListener;//设置监听对象
    }
}

PictureView全文:

package com.example.mymirror.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;


/**
 * Created by 700 on 2018/8/23.
 */

public class PictureView extends ImageView {
    public PictureView(Context context) {
        super(context);
    }

    public PictureView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PictureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

DrawView全文:

package com.example.mymirror.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by 700 on 2018/8/23.
 */

public class DrawView extends View{
    public DrawView(Context context) {
        super(context);
    }

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

运行结果:


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容