啟動畫面的實(shí)現(xiàn)

關(guān)于啟動畫面的實(shí)現(xiàn)。
  • 實(shí)現(xiàn)從url讀出JSON,并且解析出img的url。
  • 獲得img的url后,設(shè)置imageview
  • 關(guān)于標(biāo)題欄的去掉,透明標(biāo)題欄和imageview全屏

從url讀出JSON核心代碼


    URL url = new URL(path);
    String imageurl=null;
    String json;
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 設(shè)置連接超時(shí)為5秒
    conn.setConnectTimeout(5000);
// 設(shè)置請求類型為Get類型
    conn.setRequestMethod("GET");
// 判斷請求Url是否成功
    if (conn.getResponseCode() != 200) {
       throw new RuntimeException("請求url失敗");
    }
    InputStream inStream = conn.getInputStream();  

//以下實(shí)現(xiàn)放在另一個(gè)方法,具體不贅述
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer1 = new byte[1024];
    int len = 0;
    while((len = inStream.read(buffer1)) != -1){
       outStream.write(buffer1,0,len);
    }
    inStream.close();
    json=outStream.toByteArray();
    return  json;

解析JSON核心代碼

JSONObject jsonObject=new JSONObject(json);//假如json是一個(gè)數(shù)組,要用JSONArray??
imageurl=jsonObject.getString("img");

用url設(shè)置img的思路與核心代碼(和獲取json的方法有部分相似)

1.用HttpURLConnection獲取鏈接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();

2.獲取數(shù)據(jù)流
InputStream inStream = conn.getInputStream();

3.轉(zhuǎn)換成byte數(shù)組(這一段并不懂,作用是從數(shù)據(jù)流中讀取數(shù)據(jù))

 public static byte[] read(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while((len = inStream.read(buffer)) != -1)
        {
            outStream.write(buffer,0,len);
        }
        inStream.close();
        return outStream.toByteArray();
    }

4.用byte創(chuàng)建bitmap

bitmap= BitmapFactory.decodeByteArray(data,0, data.length);  

5.用bitmap設(shè)置img

imageView.setImageBitmap(bitmap);

去掉標(biāo)題欄

一般是改變activity引用的主題
在AndroidManifest.xml里面

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

或者在styles.xml里面

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

狀態(tài)欄透明

在onCreate()里,注意要在 setContentView(R.layout.activity); 后面

// 隱藏標(biāo)題欄
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 隱藏狀態(tài)欄
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

img的全屏

  • 上面的去掉標(biāo)題欄和標(biāo)題欄的透明化。
  • 在activity_main里有可能會有以下幾行

    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"

控制了img和邊框的邊距,刪掉就可以了。

  • 同樣在activity_main里,對image view的屬性設(shè)置
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"

或者android:scaleType="fitXY"

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容