利用 Android Studio开发看美女应用
今天我们会利用 Android Studio开发一个简单的看美女应用,通过它查看互联网上的美女图片。通过本篇文章,你将会学到Android应用简单开发,Android权限申请,Android应用图标适配,Android应用签名打包等内容。
1.创建安卓应用
请参考上一篇《利用Android Studio开发一个demo应用》,只需要修改项目名称为Seeing,创建的项目目录如下:
2.应用各部分源码
(1)MainActivity.java
Seeing/app/src/main/java/com/example/seeing/MainActivity.java
package com.example.seeing;
import androidx.appcompat.app.AppCompatActivity;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
Button button;
ImageView imageView;
String url = "http://www.hinews.cn/pic/0/17/57/58/17575884_413287.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.imageView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
Bitmap bmp = getURLimage(url);
Message msg = new Message();
msg.what = 0;
msg.obj = bmp;
handle.sendMessage(msg);
}
}).start();
}
});
}
//在消息队列中实现对控件的更改
private Handler handle = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
Bitmap bmp=(Bitmap)msg.obj;
imageView.setImageBitmap(bmp);
break;
}
};
};
//加载图片
public Bitmap getURLimage(String url) {
Bitmap bmp = null;
try {
URL myurl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) myurl.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
int code=conn.getResponseCode();
if (code == 200){
InputStream in = conn.getInputStream();
bmp = BitmapFactory.decodeStream(in);
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return bmp;
}
}
(2)AndroidManifest.xml
申请访问网络的权限:<uses-permission android:name="android.permission.INTERNET"/>
Seeing/app/src/main/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.seeing">
<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>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
(3)activity_main.xml
Seeing/app/src/main/res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:gravity="center"
android:text="@string/first_text"
android:textColor="@color/blue"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="210dp"
android:layout_height="320dp"
android:layout_marginStart="100dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="5dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/button"
app:layout_constraintEnd_toEndOf="@+id/imageView"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
(4)strings.xml
Seeing/app/src/main/res/values/strings.xml
<resources>
<string name="app_name">Seeing</string>
<color name="blue">#26A599</color>
<string name="first_text">御风而上,神游天下!</string>
<string name="button">获取图片</string>
</resources>
3.应用图标适配
(1)应用图标适配的横空出世
自iPhone诞生至今,所有应用程序的图标都被强制要求使用圆角矩形图标。而Android系统选择了自由与开放,所以应用图标的形状无任何强制要求,这也导致了Android图标市场的极度混乱,有的手机厂商更是强制给应用添加圆角图标,如下所示:
一面是自由与开放,一面是图标市场的混乱无序,在此背景下Google绝妙的解决了这个问题。在Android 8.0系统开始,应用程序的图标被分为了两层:前景层和背景层,前景层用来展示应用图标的Logo,背景用来衬托应用图标的Logo。细心的你肯定会问图标的形状怎么解决?Google把这个权力交给了手机厂商,根据手机厂商的喜好,可以使用圆角矩形、圆形或方形等等,这下手机上所有应用图标都显的规范了。下面截取了某厂商手机一些应用的图标,看起来清爽、整洁不少:
致敬伟大的Google,不得不说这真是一个堪称完美的方案。
(2)给自己的应用适配图标
打开app/build.gradle文件,确保targetSdkVersion已经指定到了26或更高。
打开AndroidManifest.xml文件,android:icon将应用的图标指定为了mipmap目录下的ic_launcher文件,而android:roundIcon只是一个只适用Android 7.1系统上的过渡版本。
这里mipmap目录很多,但是在Android 8.0及以上系统,会使用mipmap-anydpi-v26这个目录下的ic_launcher来作为图标。正如你所见,它是一个xml格式的文件。
好了,现在我们去适配我们的应用,在Windows系统按下Ctrl+Shift+A键,搜索Image Asset。
Icon Type表示同时创建兼容8.0系统以及老版本系统的应用图标,保持默认即可。
Name表示应用图标的名称,保持默认即可。
Foreground Layer表示前景层,Background Layer表示背景层,Legacy表示老版本的图标。
选择好之后点击Finish,Android Studio会自动帮我们生成适配的应用图标。
4.打包运行此应用
点击Build>Build Bundles/APKs>Build APK打包此应用,最后在
Seeing\app\build\outputs\apk\debug目录下可以看到app-debug.apk。
安装此应用,如下所示:
5.Android应用签名
(1)APK签名
上面的版本使用的是默认的debug签名,然而在以Release发布模式编译时,apk文件就不会得到自动签名,那么就需要我们手工的去签名。所有的Android应用程序都要求开发人员用一个证书进行数字签名,Android系统不会安装没有进行签名的应用程序。
apk签名的好处有:
a.应用程序升级
只有以同一个证书签名,系统才会允许安装升级的应用程序。如果采用了不同的证书,那么系统会要求你的应用程序采用不同的包名称,在这种情况下相当于安装了一个全新的应用程序。
b.应用程序模块化
Android 系统可以允许同一个证书签名的多个应用程序在一个进程里运行,系统实际把他们作为一个单个的应用程序,此时就可以把我们的应用程序以模块的方式进行部署,而用户可以独立的升级其中的一个模块。
c.数据或代码共享
Android提供了基于签名的权限机制,那么一个应用程序就可以为另一个以相同证书签名的应用程序公开自己的功能。
(2)使用 Android Studio给apk签名
选择Build/Generate Signed..., 因为是第一次对apk进行签名,需要先创建签名文件,点击Create new...。
接下来填写New Key Store对话框,点击Ok后回到之前的对话框,按照提示,最后选择Finish完成签名。
最后在Seeing/app/release下,可以看到。