Android開發系列:4.利用 Android Studio開發看美女應用

4.利用 Android Studio開發看美女應用

今天我們會利用 Android Studio開發一個簡單的看美女應用,通過它查看互聯網上的美女圖片。通過本篇文章,你將會學到Android應用簡單開發,Android權限申請,Android應用圖標適配,Android應用簽名打包等內容。

4.1創建安卓應用

請參考上一篇《利用Android Studio開發一個demo應用》,只需要修改項目名稱爲Seeing,創建的項目目錄如下:

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

4.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.4打包運行此應用

點擊Build>Build Bundles/APKs>Build APK打包此應用,最後在

Seeing\app\build\outputs\apk\debug目錄下可以看到app-debug.apk。

安裝此應用,如下所示:

4.5Android應用簽名

(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下,可以看到。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章