安卓開發實戰-記賬本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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM