20. “我”模块(二)之星座

上一节对欢迎模块进行了综述(可参见 “我”模块(一) 进行了解),接下来将从“我”模块(二)开始详细介绍:

  • [“我”模块(二)之日历]
  • [“我”模块(二)之星座]
  • [“我”模块(二)之星座选择]
  • [“我”模块(二)之涂鸦]
  • [“我”模块(二)之地图]

知识点

  • 掌握“日历”界面的开发,使用日历展示当前年份
  • 掌握“星座”界面的开发,选择不同的星座展示不同的运势
  • 掌握“涂鸦”界面的开发,实现图画的绘制功能
  • 掌握“地图”界面的开发,可以定位一个指定地点

星座

任务综述:
“星座”界面主要用于展示被切换的星座的名称、日期、头像、图标、简介、整体运势、爱情运势、事业学业、财富运势、健康运势以及详细信息。为了界面的美观,在界面右下角会设置不断冒出心形泡泡的效果。

3. “星座”界面

任务分析:
“星座”界面主要用于展示被切换的星座的名称、日期、头像、图标、简介、整体运势、爱情运势、事业学业、财富运势、健康运势以及心形泡泡的效果,界面效果如图所示。

10186693-912294ee0f3a303f.png
“星座”界面

任务实施:
(1)创建“星座”界面:ConstellationActivity&activity_constellation。

(2)导入界面图片(6个)。

(3)引入BubbleViews库。该项目中的心形泡泡效果是通过引入第三方库BubbleViews实现的。在AS中,选择File/New/Import Module选项把心形泡泡的框架导入项目,选中项目,右击选择Open Module Settings/Dependencies/+/Module Dependency选项卡/+/Module Dependency选项/加入心形泡泡框架,框架如图所示。


10186693-78e763de2c644962.png
心形泡泡框架

(4)放置界面控件。在布局文件中,放置一个HeartLayout自定义控件显示心形泡泡;通过<include>标签将activity_constellation_content.xml(局部布局)引入。

activity_constellation.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">
    <include layout="@layout/activity_constellation_content" />
    <com.itheima.heartlayout.HeartLayout
        android:id="@+id/heart_layout"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_gravity="bottom|right" />
</FrameLayout>

(5)放置activity_constellation_content.xml文件中的控件。
7个ImageView控件,其中1个ImageView控件用于显示星座头像,1个ImageView控件用于显示星座图标,剩余5个ImageView控件分别用于显示整体运势、爱情运势、事业学业、财富运势以及健康运势的图标;
17个TextView控件,其中1个TextView控件用于显示星座名称,1个TextView控件用于显示星座日期,1个TextView控件用于显示星座介绍信息,4个TextView控件用于显示整体运势、爱情运势、事业学业、财富运势的文本,10个TextView控件分别是用于显示整体运势、爱情运势、事业学业、财富运势以及健康运势的文本与详细信息;
4个RatingBar控件,分别用于显示整体运势、爱情运势、事业学业、财富运势的星级信息。
activity_constellation_content.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/constellation_bg"
            android:orientation="vertical">
            <include layout="@layout/main_title_bar" />
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_marginLeft="40dp">
                <ImageView
                    android:id="@+id/iv_head"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:scaleType="fitXY" />
                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="15dp"
                    android:layout_toRightOf="@id/iv_head"
                    android:orientation="vertical">
                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">
                        <ImageView
                            android:id="@+id/iv_icon"
                            android:layout_width="18dp"
                            android:layout_height="18dp"
                            android:scaleType="fitXY" />
                        <TextView
                            android:id="@+id/tv_name"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="4dp"
                            android:textColor="@android:color/white"
                            android:textSize="18sp" />
                    </LinearLayout>
                    <TextView
                        android:id="@+id/tv_date"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="4dp"
                        android:textColor="@android:color/white"
                        android:textSize="18sp" />
                </LinearLayout>
            </RelativeLayout>
        </LinearLayout>
        <TextView
            android:id="@+id/tv_info"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="8dp"
            android:background="@color/constellation_info_bg_color"
            android:padding="6dp"
            android:textColor="@color/constellation_info_color"
            android:textSize="14sp" />
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp">
            <LinearLayout
                android:id="@+id/ll_whole"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="整体运势:"
                    android:textColor="@color/constellation_info_color2"
                    android:textSize="12sp" />
                <RatingBar
                    android:id="@+id/rb_whole"
                    style="?android:attr/ratingBarStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:progressTint="@color/rating_bar_color"
                    android:rating="2.5" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="爱情运势:"
                    android:textColor="@color/constellation_info_color2"
                    android:textSize="12sp" />
                <RatingBar
                    android:id="@+id/rb_love"
                    style="?android:attr/ratingBarStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:progressTint="@color/rating_bar_color"
                    android:rating="2.5" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/ll_whole"
                android:layout_marginTop="4dp"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="事业学业:"
                    android:textColor="@color/constellation_info_color2"
                    android:textSize="12sp" />
                <RatingBar
                    android:id="@+id/rb_career"
                    style="?android:attr/ratingBarStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:progressTint="@color/rating_bar_color"
                    android:rating="2.5" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_below="@id/ll_whole"
                android:layout_marginTop="4dp"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="财富运势:"
                    android:textColor="@color/constellation_info_color2"
                    android:textSize="12sp" />
                <RatingBar
                    android:id="@+id/rb_money"
                    style="?android:attr/ratingBarStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:progressTint="@color/rating_bar_color"
                    android:rating="2.5" />
            </LinearLayout>
        </RelativeLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:scaleType="fitXY"
                android:src="@drawable/whole_icon" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:text="整体运势"
                android:textColor="@color/whole_text_color"
                android:textSize="14sp" />
        </LinearLayout>
        <TextView
            android:id="@+id/tv_whole"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textColor="@color/constellation_info_color2"
            android:textSize="12sp" />
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:scaleType="fitXY"
                android:src="@drawable/love_icon" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:text="爱情运势"
                android:textColor="@color/love_text_color"
                android:textSize="14sp" />
        </LinearLayout>
        <TextView
            android:id="@+id/tv_love"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textColor="@color/constellation_info_color2"
            android:textSize="12sp" />
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:scaleType="fitXY"
                android:src="@drawable/career_icon" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:text="事业学业"
                android:textColor="@color/career_text_color"
                android:textSize="14sp" />
        </LinearLayout>
        <TextView
            android:id="@+id/tv_career"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textColor="@color/constellation_info_color2"
            android:textSize="12sp" />
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:scaleType="fitXY"
                android:src="@drawable/money_icon" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:text="财富运势"
                android:textColor="@color/money_text_color"
                android:textSize="14sp" />
        </LinearLayout>
        <TextView
            android:id="@+id/tv_money"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textColor="@color/constellation_info_color2"
            android:textSize="12sp" />
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:scaleType="fitXY"
                android:src="@drawable/health_icon" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:text="健康运势"
                android:textColor="@color/health_text_color"
                android:textSize="14sp" />
        </LinearLayout>
        <TextView
            android:id="@+id/tv_health"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textColor="@color/constellation_info_color2"
            android:textSize="12sp" />
    </LinearLayout>
</ScrollView>

(6)修改colors.xml文件。“星座”界面中有几种颜色用于修饰文本或者背景,因此为了便于后续调用,在res/values文件夹的colors.xml文件中添加如下代码。

    <color name="constellation_info_color">#7f8080</color>
    <color name="constellation_info_bg_color">#f7f2fd</color>
    <color name="rating_bar_color">#702ec4</color>
    <color name="constellation_info_color2">#454545</color>
    <color name="whole_text_color">#7fbee4</color>
    <color name="love_text_color">#ff8cb4</color>
    <color name="career_text_color">#b29ddd</color>
    <color name="money_text_color">#f0c062</color>
    <color name="health_text_color">#9cd47a</color>

4. 创建ConstellationBean

任务分析:
项目中星座属性包括星座Id、星座选择界面的带色图标、白色星座图标、星座名称、星座日期、星座头像、星座介绍信息、整体运势星级、爱情运势星级、事业学业星级、财富运势星级、整体运势星级、爱情运势信息,为了便于后续对这些属性进行操作,因此创建一个ConstellationBean类用于存放这些属性。

任务实施:
在bean包中创建ConstellationBean,在该类中创建星座所需的属性。

ConstellationBean.java

public class ConstellationBean {
    private int id;        //星座Id
    private String img;   //星座选择界面的带色星座图标
    private String icon;  //白色星座图标
    private String name;  //星座名称
    private String date;  //星座日期
    private String head;  //星座头像
    private String info;  //星座介绍信息
    private int whole;   //整体运势星级
    private int love;    //爱情运势星级
    private int career;  //事业学业星级
    private int money;   //财富运势星级
    private String whole_info;  //整体运势内容
    private String love_info;   //爱情运势内容
    private String career_info; //事业学业内容
    private String money_info;  //财富运势内容
    private String health_info; //健康运势内容
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getImg() {
        return img;
    }
    public void setImg(String img) {
        this.img = img;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getHead() {
        return head;
    }
    public void setHead(String head) {
        this.head = head;
    }
    public String getInfo() {
        return info;
    }
    public void setInfo(String info) {
        this.info = info;
    }
    public int getWhole() {
        return whole;
    }
    public void setWhole(int whole) {
        this.whole = whole;
    }
    public int getLove() {
        return love;
    }
    public void setLove(int love) {
        this.love = love;
    }
    public int getCareer() {
        return career;
    }
    public void setCareer(int career) {
        this.career = career;
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }
    public String getWhole_info() {
        return whole_info;
    }
    public void setWhole_info(String whole_info) {
        this.whole_info = whole_info;
    }
    public String getLove_info() {
        return love_info;
    }
    public void setLove_info(String love_info) {
        this.love_info = love_info;
    }
    public String getCareer_info() {
        return career_info;
    }
    public void setCareer_info(String career_info) {
        this.career_info = career_info;
    }
    public String getMoney_info() {
        return money_info;
    }
    public void setMoney_info(String money_info) {
        this.money_info = money_info;
    }
    public String getHealth_info() {
        return health_info;
    }
    public void setHealth_info(String health_info) {
        this.health_info = health_info;
    }
    public String getIcon() {
        return icon;
    }
    public void setIcon(String icon) {
        this.icon = icon;
    }
}

5. “星座”界面数据

任务分析:
“星座”界面由“星座图片”与“星座数据”组成,其中图片是通过Tomcat的ROOT文件夹中创建一个图片文件夹constellation存放的,数据是通过在ROOT文件夹中创建一个constellation_data.json文件存放的。

任务实施:
(1)创建“星座”界面图片存放的文件夹。在Tomcat的ROOT/topline/img文件夹中创建一个constellation文件夹,用于存放“星座”界面的图片。

(2)在Tomcat服务器中创建“星座”界面数据文件。在Tomcat的ROOT/topline目录中创建一个constellation_data.json文件,该文件用于存放“星座”界面需要加载的数据。

constellation_data.json

[
{
"id":1,
"name":"白羊座",
"head":"http://172.27.35.1:8080/topline/img/constellation/baiyang_head_icon.png",
"img":"http://172.27.35.1:8080/topline/img/constellation/baiyang_icon.png",
"icon":"http://172.27.35.1:8080/topline/img/constellation/baiyang.png",
"date":"3.21~4.19",
"info":"白羊座的人热情冲动、爱冒险、慷慨、天不怕地不怕而且一旦下定决心,不到黄河心不死,排除万难的要达到目的。",
"whole":3,
"love":3,
"career":4,
"money":4,
"whole_info":"今天对爱情的向往没那么强烈,即使是遇到喜欢的人表现都相比之前要冷静不少,不容易擦出爱火花。财运有好转,投资上会有收获,理财规划好,消费上能够精打细算,货比三家。",
"love_info":"身边出现不少爱慕者,有伴者感情生活稳定。",
"career_info":"做事效率高,但是有点急进,要注意细节之处。",
"money_info":"有得财机会,赚钱轻松,还会得偏财的运气。",
"health_info":"要多爱惜身体,不要忽略了健康。"
},
……
{
"id":12,
"name":"双鱼座",
"head":"http://172.27.35.1:8080/topline/img/constellation/shuangyu_head_icon.png",
"img":"http://172.27.35.1:8080/topline/img/constellation/shuangyu_icon.png",
"icon":"http://172.27.35.1:8080/topline/img/constellation/shuangyu.png",
"date":"2.19~3.20",
"info":"双鱼座集合了所有星座的优缺点于一身,同时受水象星座的情绪化影响,使他们原来复杂的性格又添加了更复杂的一笔。双鱼座的人最大的优点是愿意帮助别人,甚至是牺牲自己。",
"whole":3,
"love":3,
"career":3,
"money":3,
"whole_info":"一个人的时候压抑的情绪特别容易爆发(可能你比较感性吧),可以多接触人群,能够帮助你排解忧郁。工作稳定上升,与同事互动良好,洽谈事务也可在今天进行。对金钱比较迟钝,不要盲目操作。",
"love_info":"恋爱中的人别对爱人说谎,心里有事就说出来。",
"career_info":"职场很容易妥协、顺从,太软弱易被人欺负。",
"money_info":"财运不太好,进账不多,但起码没有金钱麻烦。",
"health_info":"身体变得更加健康,小病小痛开始消失。"
}
]

(3)解析JSON数据。由于从Tomcat服务器中获取的JSON格式的数据不能直接加载到界面上,因此需要在utils包的JsonParse类中创建一个getConstellationList()方法,用于解析“星座”界面获取的JSON数据,在JsonParse类中需要添加如下代码:

 public List<ConstellationBean> getConstellaList(String json) {
        //使用gson库解析JSON数据
        Gson gson = new Gson();
        //创建一个TypeToken的匿名子类对象,并调用对象的getType()方法
        Type listType = new TypeToken<List<ConstellationBean>>() {
        }.getType();
        //把获取到的信息集合存到constellaList中
        List<ConstellationBean> constellaList = gson.fromJson(json, listType);
        return constellaList;
    }

(4)创建星座信息表。由于“星座”界面需要根据星座Id查询具体星座的详细信息,因此需要把获取的十二星座信息保存到数据库,创建一个CONSTELLATION星座信息表,在sqlite包中的SQLiteHelper类中的“public static final String U_USERINFO = "userinfo"; //用户信息”语句下方添加如下代码:

 public static final String CONSTELLATION = "constellation";//十二星座信息

在SQLiteHelper类中创建一个星座信息表,具体代码如下:

       /**
         * 创建十二星座信息表
         */
        db.execSQL("CREATE TABLE  IF NOT EXISTS " + CONSTELLATION + "( "
                + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "c_id INT, "               //星座id
                + "name VARCHAR, "          //星座名称
                + "head VARCHAR, "          //头像
                + "img VARCHAR,"            //图标
                + "icon VARCHAR,"           //白色图标
                +"date VARCHAR,"            //日期
                +"info VARCHAR,"            //星座信息
                +"whole INT,"               //整体运势
                +"love INT,"                //爱情运势
                +"career INT,"             //事业学业
                +"money INT,"              //财富运势
                +"whole_info VARCHAR,"   //整体运势信息
                +"love_info VARCHAR,"    //爱情运势信息
                +"career_info VARCHAR," //事业学业信息
                +"money_info VARCHAR,"  //财富运势信息
                +"health_info VARCHAR"  //健康运势信息
                + ")");

在SQLiteHelper类的onUpgrade()方法中的“ db.execSQL("DROP TABLE IF EXISTS " + U_USERINFO);”语句下方添加:

  db.execSQL("DROP TABLE IF EXISTS " + COLLECTION_NEWS_INFO);

(5)保存到星座数据到数据库。由于十二星座信息数据需要保存到数据库,因此utils包中的DBUtils类中创建一个saveConstellationInfo()方法保存到十二星座的信息数据。

    /**
     * 保存十二星座信息
     */
    public void saveConstellationInfo(List<ConstellationBean> list) {
        Cursor cursor = db.rawQuery("SELECT * FROM " + SQLiteHelper.CONSTELLATION, null);
        if (cursor.getCount() != 0)//添加数据时,如果星座表中有数据,则在添加新数据之前需删除旧数据
        {
            //删除表中的数据
            db.execSQL("DELETE FROM " + SQLiteHelper.CONSTELLATION);
        }
        for (ConstellationBean bean : list) {
            ContentValues cv = new ContentValues();
            cv.put("c_id", bean.getId());
            cv.put("name", bean.getName());
            cv.put("head", bean.getHead());
            cv.put("img", bean.getImg());
            cv.put("icon", bean.getIcon());
            cv.put("date", bean.getDate());
            cv.put("info", bean.getInfo());
            cv.put("whole", bean.getWhole());
            cv.put("love", bean.getLove());
            cv.put("career", bean.getCareer());
            cv.put("money", bean.getMoney());
            cv.put("whole_info", bean.getWhole_info());
            cv.put("love_info", bean.getLove_info());
            cv.put("career_info", bean.getCareer_info());
            cv.put("money_info", bean.getMoney_info());
            cv.put("health_info", bean.getHealth_info());
            db.insert(SQLiteHelper.CONSTELLATION, null, cv);
        }
    }

(6)根据星座Id从数据库中获取对应星座的信息。由于“星座”界面根据星座Id查询具体星座的详细信息。因此需要在utils包的DBUtils类中创建一个getConstellationInfo()方法以获取对应星座的信息数据,具体代码如下所示:

    /**
     * 根据id获取星座信息
     */
    public ConstellationBean getConstellationInfo(int c_id) {
        String sql = "SELECT * FROM " + SQLiteHelper.CONSTELLATION + " WHERE c_id=?";
        Cursor cursor = db.rawQuery(sql, new String[]{c_id + ""});
        ConstellationBean bean = null;
        while (cursor.moveToNext()) {
            bean = new ConstellationBean();
            bean.setName(cursor.getString(cursor.getColumnIndex("name")));
            bean.setHead(cursor.getString(cursor.getColumnIndex("head")));
            bean.setImg(cursor.getString(cursor.getColumnIndex("img")));
            bean.setIcon(cursor.getString(cursor.getColumnIndex("icon")));
            bean.setDate(cursor.getString(cursor.getColumnIndex("date")));
            bean.setInfo(cursor.getString(cursor.getColumnIndex("info")));
            bean.setWhole(cursor.getInt(cursor.getColumnIndex("whole")));
            bean.setLove(cursor.getInt(cursor.getColumnIndex("love")));
            bean.setCareer(cursor.getInt(cursor.getColumnIndex("career")));
            bean.setMoney(cursor.getInt(cursor.getColumnIndex("money")));
            bean.setWhole_info(cursor.getString(cursor.getColumnIndex("whole_info")));
            bean.setLove_info(cursor.getString(cursor.getColumnIndex("love_info")));
            bean.setCareer_info(cursor.getString(cursor.getColumnIndex("career_info")));
            bean.setMoney_info(cursor.getString(cursor.getColumnIndex("money_info")));
            bean.setHealth_info(cursor.getString(cursor.getColumnIndex("health_info")));
        }
        cursor.close();
        return bean;
    }

(7) 修改Constant.java文件。在utils包中Constant类中添加名为REQUEST_CONSTELLATION_URL的“星座”界面接口地址:

//星座界面接口
    public static final String REQUEST_CONSTELLATION_URL = "/constellation_data.json";

6. “星座”界面逻辑代码

任务分析:
“星座”界面主要用于展示星座的详细信息,当进入“星座”界面时,首先从服务器中获取信息数据,然后把数据展示到“星座”界面上。当点击右上角的“切换”按钮时,会跳转到“星座选择”界面。

任务实施:
(1)获取界面控件。在ConstellationActivity中创建界面控件的初始化方法init(),获取“星座”界面所要用到的控件。

(2)获取与设置数据。在ConstellationActivity中创建getData()与setData()方法,分别用于获取服务器中的数据与把获取的数据设置到“星座”界面。

(3)回传数据。在ConstellationActivity中重写onActivityResult()方法,接收从星座选择界面获取的星座Id,根据Id从数据库获取对应星座信息并展示到界面上。

ConstellationActivity.java

public class ConstellationActivity extends AppCompatActivity {
    private TextView tv_back, tv_switch;
    private SwipeBackLayout layout;
    private ImageView iv_head, iv_icon;
    private TextView tv_name, tv_date, tv_info;
    private RatingBar rb_whole, rb_love, rb_career, rb_money;
    private TextView tv_whole, tv_love, tv_career, tv_money, tv_health;
    private OkHttpClient okHttpClient;
    public static final int MSG_CONSTELLATION_OK = 1;//获取星座数据
    private MHandler mHandler;
    private Random mRandom;
    private Timer mTimer;
    private HeartLayout mHeartLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        layout = (SwipeBackLayout) LayoutInflater.from(this).inflate(
                R.layout.base, null);
        layout.attachToActivity(this);
        setContentView(R.layout.activity_constellation);
        mHandler = new MHandler();
        okHttpClient = new OkHttpClient();
        getData();
        init();
    }
    private void init() {
        mTimer = new Timer();
        mRandom = new Random();
        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_switch = (TextView) findViewById(R.id.tv_save);
        tv_back.setVisibility(View.VISIBLE);
        tv_switch.setVisibility(View.VISIBLE);
        tv_switch.setText("切换");
        iv_head = (ImageView) findViewById(R.id.iv_head);
        iv_icon = (ImageView) findViewById(R.id.iv_icon);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_date = (TextView) findViewById(R.id.tv_date);
        tv_info = (TextView) findViewById(R.id.tv_info);
        rb_whole = (RatingBar) findViewById(R.id.rb_whole);
        rb_love = (RatingBar) findViewById(R.id.rb_love);
        rb_career = (RatingBar) findViewById(R.id.rb_career);
        rb_money = (RatingBar) findViewById(R.id.rb_money);
        tv_whole = (TextView) findViewById(R.id.tv_whole);
        tv_love = (TextView) findViewById(R.id.tv_love);
        tv_career = (TextView) findViewById(R.id.tv_career);
        tv_money = (TextView) findViewById(R.id.tv_money);
        tv_health = (TextView) findViewById(R.id.tv_health);
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ConstellationActivity.this.finish();
            }
        });
        tv_switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ConstellationActivity.this, ChooseConstellationActivity.class);
                startActivityForResult(intent, 1);
            }
        });
        mHeartLayout = (HeartLayout) findViewById(R.id.heart_layout);
        mTimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                mHeartLayout.post(new Runnable() {
                    @Override
                    public void run() {
                        mHeartLayout.addHeart(randomColor());
                    }
                });
            }
        }, 500, 200);
    }
    private int randomColor() {
        return Color.rgb(mRandom.nextInt(255), mRandom.nextInt(255),
                mRandom.nextInt(255));
    }
    /**
     * 事件捕获
     */
    class MHandler extends Handler {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            switch (msg.what) {
                case MSG_CONSTELLATION_OK:
                    if (msg.obj != null) {
                        String result = (String) msg.obj;
                        List<ConstellationBean> list = JsonParse.getInstance().
                                getConstellaList(result);
                        if (list != null) {
                            if (list.size() > 0) {
                                //保存数据到数据库
                                DBUtils.getInstance(ConstellationActivity.this).
                                        saveConstellationInfo(list);
                                ConstellationBean bean = DBUtils.getInstance(
                                        ConstellationActivity.this).getConstellationInfo(1);
                                setData(bean);
                            }
                        }
                    }
                    break;
            }
        }
    }
    private void setData(ConstellationBean bean) {
        tv_name.setText(bean.getName());
        Glide
                .with(ConstellationActivity.this)
                .load(bean.getHead())
                .error(R.mipmap.ic_launcher)
                .into(iv_head);
        Glide
                .with(ConstellationActivity.this)
                .load(bean.getIcon())
                .error(R.mipmap.ic_launcher)
                .into(iv_icon);
        tv_date.setText(bean.getDate());
        tv_info.setText(bean.getInfo());
        rb_whole.setRating(bean.getWhole());
        rb_love.setRating(bean.getLove());
        rb_career.setRating(bean.getCareer());
        rb_money.setRating(bean.getMoney());
        tv_whole.setText(bean.getWhole_info());
        tv_love.setText(bean.getLove_info());
        tv_career.setText(bean.getCareer_info());
        tv_money.setText(bean.getMoney_info());
        tv_health.setText(bean.getHealth_info());
    }
    private void getData() {
        Request request = new Request.Builder().url(Constant.WEB_SITE +
                Constant.REQUEST_CONSTELLATION_URL).build();
        Call call = okHttpClient.newCall(request);
        //开启异步线程访问网络
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Response response) throws IOException {
                String res = response.body().string();
                Message msg = new Message();
                msg.what = MSG_CONSTELLATION_OK;
                msg.obj = res;
                mHandler.sendMessage(msg);
            }
            @Override
            public void onFailure(Request arg0, IOException arg1) {
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTimer.cancel();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            int id = data.getIntExtra("id", 0);
            ConstellationBean bean = DBUtils.getInstance(ConstellationActivity.this).
                    getConstellationInfo(id);
            setData(bean);
        }
    }
}

(4)修改清单文件。由于“星座”界面由右滑动会给关闭该界面,因此需要给该界面添加透明主题的样式,在清单文件的ConstellationActivity对应的activity标签中添加如下代码:

       <activity
            android:name=".activity.ConstellationActivity"
            android:theme="@style/AppTheme.TransparentActivity" />

(5)修改“我”界面逻辑代码。由于点击“我”界面上的星座图标是会跳转到“星座”界面,因此需要在MeFragment中的onClick()方法,在该方法的“case R.id.ll_constellation:”语句下方添加如下代码:

Intent constellIntent = new Intent(getActivity(), ConstellationActivity.class);
startActivity(constellIntent);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值