1. 程式人生 > >安卓開發實戰-記賬本APP(六)

安卓開發實戰-記賬本APP(六)

記賬本APP開發---終結篇

昨天的動態數字錄屏奉上:在抖音上拍了一個(ps:歡迎點贊)

https://v.douyin.com/poEjmG/ 

今天將圖表的內容進行了製作,我用的是MPChart的餅狀圖進行製作的,將之前使用者的資料進行計算,然後定義資料與具體的分類相結合。

設定不同的顏色rgb,實現餅狀圖的分析資料。

 

 到此,已經將記賬本APP的功能實現的差不多了。

接下來我就好好分析一下我的整個APP專案。

首先是建立的:使用者類(User),UserDBHelper,和管理資料庫操作的UserOperator,來進行資料庫的增刪改查。

 

然後開始建立活動:loginActivity登入介面與RegisterActivity(註冊介面),註冊介面包含了一個驗證碼類(Code)來控制使用者的登陸與註冊。

 

登陸進去後,會走向MainActivity,它控制著四個Fragment,分別是Account_Fragment(記賬頁面),Sum_Fragment(收支對比介面),Chart_Fragment(圖表介面),Me_Fragment(個人資訊介面),當點選對應的圖示就會跳轉到對應的Fragment,在MainActivity裡設定了第一個Fragment為Account_Fragment。

 

那就來說一下Account_Fragment裡的內容,記賬頁面包含:(賬目類)Account,AccountAdapter,AccountDBHelper,AccountOperator。通過介面卡將新增的賬單顯示到listview裡,該頁面有三個按鈕:支出,收入,刪除所有。支出和收入都是通過Builder物件來建立彈窗,進行資料的填寫。填完後會進行資料庫的新增,list集合的新增,介面卡的重新整理。刪除所有是呼叫資料庫操作類的deleteall方法實現的。我們還添加了長按刪除操作,由於繼承的是Fragment,無法直接建立長按事件,我們通過onActivityCreated(Bundle savedInstanceState)方法來實現長按刪除事件,該方法是在onCreat()結束後才執行的,因此我們必須建立onCreat()方法。到此Account_Fragment的操作就講完了。

 

接著我們的第二個Fragment:Sum_Fragment,這裡沒有太多的東西可講,但是這裡我從網上學到了模仿支付寶的動態數字滾動效果的實現,分別用到NumAnim和NumUtil,然後再Sum_Fragment裡對想要實現滾動效果的數字與元件,用下面的程式碼就可以實現了

NumAnim.startAnim(tv_shou, sumshou, 1000);     //設定滾動動畫持續1秒

 

接著是我們的第三個介面:Chart_Fragment,該介面就一個功能就是實現圖表的展示,也就是我今天所學習的內容,用的是MPChart圖表庫,來實現圖表的展示。

package com.example.countbook;

import android.app.Fragment;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;

import java.util.ArrayList;

public class Chart_Fragment extends Fragment {

    int gongzi=0,jianzhi=0,jiangjin=0;
    int food=0,clothes=0,life=0,qita=0;
    int sum=0,sumshou=0,sumzhi=0;
    private PieChart pieChart;
    private AccountOperator accountOperator;
    Bundle bundle;
    String username;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.chart_fragment, null);
        accountOperator=new AccountOperator(view.getContext());
        bundle=getArguments();
        username=bundle.getString("username");
        getsum();

        pieChart=(PieChart)view.findViewById(R.id.pieChart);
        pieChart.setUsePercentValues(true);//設定value是否用顯示百分數,預設為false
        pieChart.setDescription("收支情況");//設定描述
        pieChart.setDescriptionTextSize(50);//設定描述字型大小
        pieChart.setDescriptionColor(Color.GREEN);

        pieChart.setExtraOffsets(5, 5, 5, 5);//設定餅狀圖距離上下左右的偏移量

        pieChart.setDrawCenterText(true);//是否繪製中間的文字
        pieChart.setCenterTextColor(Color.RED);//中間的文字顏色
        pieChart.setCenterTextSize(15);//中間的文字字型大小

        pieChart.setDrawHoleEnabled(true);//是否繪製餅狀圖中間的圓
        pieChart.setHoleColor(Color.WHITE);//餅狀圖中間的圓的繪製顏色
        pieChart.setHoleRadius(40f);//餅狀圖中間的圓的半徑大小

        pieChart.setTransparentCircleColor(Color.BLACK);//設定圓環的顏色
        pieChart.setTransparentCircleAlpha(100);//設定圓環的透明度[0,255]
        pieChart.setTransparentCircleRadius(40f);//設定圓環的半徑值

        // enable rotation of the chart by touch
        pieChart.setRotationEnabled(true);//設定餅狀圖是否可以旋轉(預設為true)
        pieChart.setRotationAngle(10);//設定餅狀圖旋轉的角度

        pieChart.setHighlightPerTapEnabled(true);//設定旋轉的時候點中的tab是否高亮(預設為true)

        //右邊小方框部分
        Legend l = pieChart.getLegend(); //設定比例圖
        l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART_CENTER);//設定每個tab的顯示位置(這個位置是指下圖右邊小方框部分的位置 )
//        l.setForm(Legend.LegendForm.LINE);  //設定比例圖的形狀,預設是方形
        l.setXEntrySpace(0f);
        l.setYEntrySpace(0f);//設定tab之間Y軸方向上的空白間距值
        l.setYOffset(0f);

        //餅狀圖上字型的設定
        // entry label styling
        pieChart.setDrawEntryLabels(true);//設定是否繪製Label
        pieChart.setEntryLabelColor(Color.rgb(95,158,160));//設定繪製Label的顏色
        pieChart.setEntryLabelTextSize(20f);//設定繪製Label的字型大小

//        pieChart.setOnChartValueSelectedListener(this);//設值點選時候的回撥
        pieChart.animateY(3400, Easing.EasingOption.EaseInQuad);//設定Y軸上的繪製動畫
        ArrayList<PieEntry> pieEntries = new ArrayList<PieEntry>();

        pieEntries.add(new PieEntry(gongzi, "工資"));
        pieEntries.add(new PieEntry(jiangjin, "獎金"));
        pieEntries.add(new PieEntry(jianzhi, "兼職"));

        pieEntries.add(new PieEntry(food, "伙食"));
        pieEntries.add(new PieEntry(clothes, "服飾"));
        pieEntries.add(new PieEntry(life, "日用品"));
        pieEntries.add(new PieEntry(qita, "其他"));

        String centerText = "淨收入\n¥" + (sum);
        pieChart.setCenterText(centerText);//設定圓環中間的文字
        PieDataSet pieDataSet = new PieDataSet(pieEntries, "");
        ArrayList<Integer> colors = new ArrayList<Integer>();

        // 餅圖顏色
        colors.add(Color.rgb(205, 205, 205));
        colors.add(Color.rgb(114, 188, 223));
        colors.add(Color.rgb(255, 123, 124));
        colors.add(Color.rgb(57, 135, 200));
        colors.add(Color.rgb(255,20,147));
        colors.add(Color.rgb(255,0,255));
        colors.add(Color.rgb(255,215,0));
        pieDataSet.setColors(colors);

        pieDataSet.setSliceSpace(0f);//設定選中的Tab離兩邊的距離
        pieDataSet.setSelectionShift(5f);//設定選中的tab的多出來的
        PieData pieData = new PieData();
        pieData.setDataSet(pieDataSet);

        //各個餅狀圖所佔比例數字的設定
        pieData.setValueFormatter(new PercentFormatter());//設定%
        pieData.setValueTextSize(20f);
        pieData.setValueTextColor(Color.rgb(148,0,211));

        pieChart.setData(pieData);
        // undo all highlights
        pieChart.highlightValues(null);
        pieChart.invalidate();

        return view;
    }
    private void getsum() {
        Cursor cursor= (Cursor) accountOperator.findall(username);
        while(cursor.moveToNext()){
            String label=cursor.getString(cursor.getColumnIndex("title"));
            int money=cursor.getInt(cursor.getColumnIndex("money"));
            if(label.equals("工資")||label.equals("兼職")||label.equals("獎金")){
                sumshou+=money;
                if(label.equals("工資")){
                    gongzi+=money;
                }else if(label.equals("兼職")){
                    jianzhi+=money;
                }else if(label.equals("獎金")){
                    jiangjin+=money;
                }
            }else{
                sumzhi+=money;
                if(label.equals("伙食")){
                    food+=money;
                }else if(label.equals("服飾")){
                    clothes+=money;
                }else if(label.equals("日用品")){
                    life+=money;
                }else if(label.equals("其他")){
                    qita+=money;
                }
            }
        }
        sum=sumshou-sumzhi;
        cursor.close();
    }
}

 

第四個Fragment為:Me_Fragment,這個介面包含HeadActivity(更換頭像)和ModifypswActivity(忘記密碼),在點選更換頭像按鈕就會實現startActivityForResult事件調動HeadActivity來實現頭像的更換,同樣更改密碼和退出登入都是通過按鈕來實現的開啟不同的Activity。

 

我的第一個Android專案開發就完成了。

原始碼:https://github.com/happy-xlf/AccountBook

&n