public class BarChartFragment01 extends BaseFragment {
private ArrayList<PointF> mPointArrayList = new ArrayList<>();
private TextView mTvDayEarningTotal;
private FrameLayout frameLayout;
//日累计总收益
private float mDayEarningTotal;
private String mStrHour;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_barchart_01, container, false);
frameLayout = (FrameLayout) view.findViewById(R.id.frame_layout);
mTvDayEarningTotal = (TextView) view.findViewById(R.id.tv_day_earning_total);
return view;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mStrHour = MyDateUtil.changeH(System.currentTimeMillis());
int mIntHour1 = (int) (Integer.parseInt(mStrHour) / 2);
WindowManager wm = getActivity().getWindowManager();
//获取到屏幕宽度,/13计算出每个柱状图的宽度
int width = (wm.getDefaultDisplay().getWidth()-120) / 13;
Log.e("1", "run:--------->width: "+ width);
for (int i = 0; i <= mIntHour1; i++) {
mPointArrayList.add(new PointF(points[i][0], points[i][1]));
float y = mPointArrayList.get(i).y;
Log.e("1", "run:--------->每两小时收益y "+ y);
mDayEarningTotal = mDayEarningTotal + y;
//代码实现柱状图
View view1 = new View(getActivity());
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width - 20, (int) (y * 100));
layoutParams.gravity = Gravity.BOTTOM;
layoutParams.setMarginStart((width) * i + 60 + 10);
view1.setLayoutParams(layoutParams);
view1.setBackgroundColor(0xffffffff);
frameLayout.addView(view1);
}
mTvDayEarningTotal.setText((float) (Math.round(mDayEarningTotal * 100)) / 100 + "");
}
@Override
public int setRootView() {
return 0;
}
@Override
public void initViews() {
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_total_earning"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:text="日累计收益"
android:textColor="@color/white" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_day_earning_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:textColor="@color/white"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(元)"
android:textColor="@color/white" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="元"
android:textColor="@android:color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="10.0"
android:textColor="@android:color/white" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#9fff" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="8.0"
android:textColor="@android:color/white" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#9fff" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="6.0"
android:textColor="@android:color/white" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#9fff" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="4.0"
android:textColor="@android:color/white" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#9fff" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="2.0"
android:textColor="@android:color/white" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#9fff" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|right"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="0.0"
android:textColor="@android:color/white" />
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="28dp"
android:background="#3fff"
android:gravity="center"
android:orientation="horizontal"
android:paddingLeft="60px">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textColor="@android:color/white" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="2"
android:textColor="@android:color/white" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="4"
android:textColor="@android:color/white" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="6"
android:textColor="@android:color/white" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="8"
android:textColor="@android:color/white" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="10"
android:textColor="@android:color/white" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="12"
android:textColor="@android:color/white" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="14"
android:textColor="@android:color/white" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="16"
android:textColor="@android:color/white" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="18"
android:textColor="@android:color/white" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="20"
android:textColor="@android:color/white" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="22"
android:textColor="@android:color/white" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="24"
android:textColor="@android:color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="60px"
android:gravity="center"
android:text="(时)"
android:textColor="@android:color/white" />
</LinearLayout>
</LinearLayout>