启动画面的实现

作者: chauI | 来源:发表于2016-05-18 13:15 被阅读38次
    关于启动画面的实现。
    • 实现从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获取链接
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();

    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里面

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

    状态栏透明

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

    // 隐藏标题栏
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    // 隐藏状态栏
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    

    img的全屏

    • 上面的去掉标题栏和标题栏的透明化。
    • 在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的属性设置
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    

    或者android:scaleType="fitXY"

    相关文章

      网友评论

        本文标题:启动画面的实现

        本文链接:https://www.haomeiwen.com/subject/mtrwrttx.html