Java tutorial
/** * Copyright (c) 2015 * * Licensed under the UCG License, Version 1.0 (the "License"); */ package com.app.jdy.ui; import java.util.Calendar; import java.util.Date; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.app.jdy.R; import com.app.jdy.utils.Constants; import com.app.jdy.utils.DialogCallBackInter; import com.app.jdy.utils.HttpUtils; import com.app.jdy.utils.URLs; import com.app.jdy.widget.SelectYearDialog; import com.db.chart.Tools; import com.db.chart.listener.OnEntryClickListener; import com.db.chart.model.LineSet; import com.db.chart.view.LineChartView; import com.db.chart.view.animation.Animation; import com.db.chart.view.animation.style.DashAnimation; import com.umeng.analytics.MobclickAgent; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.animation.TimeInterpolator; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.graphics.DashPathEffect; import android.graphics.Paint; import android.graphics.Rect; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.AccelerateInterpolator; import android.view.animation.DecelerateInterpolator; import android.widget.RelativeLayout.LayoutParams; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MyFaceActivity extends Activity implements OnClickListener { /** * */ private ImageView mBackImg; /** * */ private TextView title; /** * hander */ private Handler handler; /** * ?json? */ private String dataJson; /** * ID? */ private String ID; /** * ?? */ private TextView my_face_sum; /** * */ private TextView select_year_click; /** * */ private TextView select_year; private final TimeInterpolator enterInterpolator = new DecelerateInterpolator(1.5f); private final TimeInterpolator exitInterpolator = new AccelerateInterpolator(); private static float mCurrOverlapFactor; private static int[] mCurrOverlapOrder; private static float mOldOverlapFactor; private static int[] mOldOverlapOrder; private final Runnable mEnterEndAction = new Runnable() { @Override public void run() { // mPlayBtn.setEnabled(true); } }; /** * Line */ private static int LINE_MAX = 20; private final static int LINE_MIN = 0; private final static String[] lineLabels = { "", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "" }; private final static float[][] lineValues = { { 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f } }; private static LineChartView mLineChart; private Paint mLineGridPaint; private TextView mLineTooltip; private final OnEntryClickListener lineEntryListener = new OnEntryClickListener() { @Override public void onClick(int setIndex, int entryIndex, Rect rect) { System.out.println(setIndex); System.out.println(entryIndex); if (mLineTooltip == null) showLineTooltip(setIndex, entryIndex, rect); else dismissLineTooltip(setIndex, entryIndex, rect); } }; private final OnClickListener lineClickListener = new OnClickListener() { @Override public void onClick(View v) { if (mLineTooltip != null) dismissLineTooltip(-1, -1, null); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_face); mCurrOverlapFactor = 1; mOldOverlapFactor = 1; initView(); getDate(); initLineChart(); updateLineChart(); mBackImg.setOnClickListener(this); select_year_click.setOnClickListener(this); } public void initView() { Calendar now = Calendar.getInstance(); Constants.SELECT_YEAR = now.get(Calendar.YEAR) + ""; mBackImg = (ImageView) findViewById(R.id.back_img); mBackImg.setVisibility(View.VISIBLE); title = (TextView) findViewById(R.id.title_tv); title.setText("??"); my_face_sum = (TextView) findViewById(R.id.my_face_sum); select_year_click = (TextView) findViewById(R.id.select_year_click); select_year = (TextView) findViewById(R.id.select_year); SharedPreferences userPreferences = getSharedPreferences("umeng_general_config", Context.MODE_PRIVATE); ID = userPreferences.getString("ID", "").trim(); handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 0: Toast.makeText(MyFaceActivity.this, Constants.NO_INTENT_TIPS, Toast.LENGTH_SHORT).show(); break; case 1: try { JSONArray jsonArray = new JSONArray(dataJson); for (int i = 0; i < lineValues[0].length; i++) { lineValues[0][i] = 0; } for (int i = 0; i < jsonArray.length(); i++) { lineValues[0][jsonArray.getJSONObject(i).getInt("month")] = (float) jsonArray .getJSONObject(i).getDouble("totol"); } } catch (JSONException e) { e.printStackTrace(); } my_face_sum.setText(getSumValue() + ""); select_year.setText(Constants.SELECT_YEAR + ""); getMaxValue(); updateLineChart(); break; case 2: for (int i = 0; i < lineValues[0].length; i++) { lineValues[0][i] = 0; } LINE_MAX = 20; my_face_sum.setText("0"); select_year.setText(Constants.SELECT_YEAR + ""); updateLineChart(); Toast.makeText(MyFaceActivity.this, "?", Toast.LENGTH_SHORT).show(); break; default: break; } } }; } public void getMaxValue() { float max = lineValues[0][0]; for (int i = 1; i < lineValues[0].length; i++) { if (max < lineValues[0][i]) { max = lineValues[0][i]; } } if (max < 20) { LINE_MAX = 20; } else { LINE_MAX = (int) max; } } public int getSumValue() { int sum = 0; for (int i = 0; i < lineValues[0].length; i++) { sum += lineValues[0][i]; } return sum; } private void getDate() { Thread thread = new Thread(new Runnable() { @Override public void run() { Message msg = new Message(); dataJson = HttpUtils.request(null, URLs.MY_FACE + "memberId=" + ID + "&year=" + Constants.SELECT_YEAR); if (dataJson.length() != 0 && !dataJson.equals("0x110")) { if (!dataJson.equals("false")) { msg.what = 1; } else { msg.what = 2; } } else { msg.what = 0; } handler.sendMessage(msg); } }); thread.start(); } /*------------------------------------* * LINECHART * *------------------------------------*/ private void initLineChart() { mLineChart = (LineChartView) findViewById(R.id.linechart); mLineChart.setOnEntryClickListener(lineEntryListener); mLineChart.setOnClickListener(lineClickListener); mLineGridPaint = new Paint(); mLineGridPaint.setColor(this.getResources().getColor(R.color.line_grid)); mLineGridPaint.setPathEffect(new DashPathEffect(new float[] { 5, 5 }, 0)); mLineGridPaint.setStyle(Paint.Style.STROKE); mLineGridPaint.setAntiAlias(true); mLineGridPaint.setStrokeWidth(Tools.fromDpToPx(.75f)); } private void updateLineChart() { mLineChart.reset(); LineSet dataSet = new LineSet(); dataSet.addPoints(lineLabels, lineValues[0]); dataSet.setDots(true).setDotsColor(this.getResources().getColor(R.color.white)) .setDotsRadius(Tools.fromDpToPx(5)).setDotsStrokeThickness(Tools.fromDpToPx(2)) .setDotsStrokeColor(this.getResources().getColor(R.color.title)) .setLineColor(this.getResources().getColor(R.color.title)).setLineThickness(Tools.fromDpToPx(3)) .beginAt(1).endAt(lineLabels.length - 1); // .setSmooth(true) // .setDashed(true); mLineChart.addData(dataSet); // dataSet = new LineSet(); // dataSet.addPoints(lineLabels, lineValues[1]); // dataSet.setLineColor(this.getResources().getColor(R.color.line)) // .setLineThickness(Tools.fromDpToPx(3)) // .setSmooth(true) // .setDashed(true); // mLineChart.addData(dataSet); mLineChart.setBorderSpacing(Tools.fromDpToPx(4)).setGrid(LineChartView.GridType.HORIZONTAL, mLineGridPaint) .setXAxis(false) // .setXLabels(XController.LabelPosition.OUTSIDE) // .setYAxis(false) // .setYLabels(YController.LabelPosition.OUTSIDE) .setAxisBorderValues(LINE_MIN, LINE_MAX, (LINE_MAX - LINE_MIN) / 5).setLabelsMetric("") .setLabelColor(getResources().getColor(R.color.title)) .show(new Animation().setEndAction(mEnterEndAction)); // .show(); mLineChart.animateSet(0, new DashAnimation()); } @SuppressLint("NewApi") private void showLineTooltip(int setIndex, int entryIndex, Rect rect) { mLineTooltip = (TextView) getLayoutInflater().inflate(R.layout.circular_tooltip, null); mLineTooltip.setText(Integer.toString((int) lineValues[setIndex][entryIndex])); LayoutParams layoutParams = new LayoutParams((int) Tools.fromDpToPx(35), (int) Tools.fromDpToPx(35)); layoutParams.leftMargin = rect.centerX() - layoutParams.width / 2; layoutParams.topMargin = rect.centerY() - layoutParams.height / 2; mLineTooltip.setLayoutParams(layoutParams); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) { mLineTooltip.setPivotX(layoutParams.width / 2); mLineTooltip.setPivotY(layoutParams.height / 2); mLineTooltip.setAlpha(0); mLineTooltip.setScaleX(0); mLineTooltip.setScaleY(0); mLineTooltip.animate().setDuration(150).alpha(1).scaleX(1).scaleY(1).rotation(360) .setInterpolator(enterInterpolator); } mLineChart.showTooltip(mLineTooltip); } @SuppressLint("NewApi") private void dismissLineTooltip(final int setIndex, final int entryIndex, final Rect rect) { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { mLineTooltip.animate().setDuration(100).scaleX(0).scaleY(0).alpha(0).setInterpolator(exitInterpolator) .withEndAction(new Runnable() { @Override public void run() { mLineChart.removeView(mLineTooltip); mLineTooltip = null; if (entryIndex != -1) showLineTooltip(setIndex, entryIndex, rect); } }); } else { mLineChart.dismissTooltip(mLineTooltip); mLineTooltip = null; if (entryIndex != -1) showLineTooltip(setIndex, entryIndex, rect); } } private DialogCallBackInter dialogCallBackInter = new DialogCallBackInter() { @Override public void refreshActivity(String text) { getDate(); } }; @Override public void onClick(View v) { switch (v.getId()) { case R.id.back_img: finish(); break; case R.id.select_year_click: Constants.SELECT_YEAR = "0"; SelectYearDialog selectYearDialog = new SelectYearDialog(MyFaceActivity.this, R.style.ForwardDialog, dialogCallBackInter); selectYearDialog.show(); break; default: break; } } @Override protected void onResume() { super.onResume(); MobclickAgent.onPageStart("SplashScreen"); MobclickAgent.onResume(this); } @Override protected void onPause() { super.onPause(); MobclickAgent.onPageEnd("SplashScreen"); MobclickAgent.onPause(this); } }