起動画面の実現

13030 ワード

起動画面の実現について.
  • urlからのJSON読み出しを実現し、imgのurlを解析する.
  • imgのurlを取得後、imageview
  • を設定する
  • タイトルバーの削除について、透明タイトルバーとimageviewフルスクリーン
  • urlからJSONコアコードを読み出す
    
        URL url = new URL(path);
        String imageurl=null;
        String json;
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    //        5 
        conn.setConnectTimeout(5000);
    //        Get  
        conn.setRequestMethod("GET");
    //     Url    
        if (conn.getResponseCode() != 200) {
           throw new RuntimeException("  url  ");
        }
        InputStream inStream = conn.getInputStream();  
    
    //           ,     
        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     ,  JSONArray??
    imageurl=jsonObject.getString("img");
    

    urlでimgを設定する考え方はコアコード(jsonを取得する方法と一部似ている)
    1.リンクHttpURLConnection conn = (HttpURLConnection)url.openConnection();をHttpURLConnectionで取得
    2.取得データストリームInputStream inStream = conn.getInputStream();3.byte配列に変換(この段落は分からないが、データストリームからデータを読み出す役割を果たす)
     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でbitmapを作成する
    bitmap= BitmapFactory.decodeByteArray(data,0, data.length);  
    

    5.bitmapでimgを設定する
    imageView.setImageBitmap(bitmap);
    

    タイトルバーを削除
    一般的にactivityが引用したテーマをAndroidManifestで変更します.xmlの中
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    

    あるいはstylesでxmlの中