美文网首页股票行情
股票行情图:使用到的常量类和工具类

股票行情图:使用到的常量类和工具类

作者: d26168ad953a | 来源:发表于2016-08-30 13:38 被阅读172次

    1.1、常量类Const.java

    /**
     * Created by wblobin on 2016/8/29.
     * qq:563084073
     */
    public class Const {
     
        public static final DecimalFormat df4 = new DecimalFormat("0.0000");
        public static final DecimalFormat df3 = new DecimalFormat("0.000");
        public static final DecimalFormat df2 = new DecimalFormat("0.00");
        public static final DecimalFormat df1 = new DecimalFormat("0.0");
        public static final DecimalFormat df0 = new DecimalFormat("0");
    
        public static final String K_DATE = "date";
        public static final String K_UPDATETIME = "UpdateTime";
        public static final String K_CLOSE = "Close";
        public static final String K_OPEN = "Open";
        public static final String K_HIGH = "High";
        public static final String K_LOW = "Low";
        public static final String K_VOLUME = "Volume";
    
    }
    

    2.1时间工具类 TimeUtils.java

    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.TimeZone;
    
    /**
     * Created by wblobin on 2016/8/29.
     * qq:563084073
     */
    public class TimeUtils {
    
        // 获得提交服务器的同步time
        public static String getUnixTime( ) { 
            return String.valueOf((int) (System.currentTimeMillis() / 1000 ));
        }
    
        public static String time2Date(String timestampString, String formats) {
            try{
                Long timestamp = Long.parseLong(timestampString) * 1000;
                SimpleDateFormat sdf = new SimpleDateFormat(formats);
                sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    
                String date = sdf.format(new Date(timestamp));
                return date;
            }catch (Exception e){
    
            }
            return timestampString;
        }
    
     
    }
    
    

    3.1格式化右边值得精确度VolFormatter .java

    
    import com.github.mikephil.charting.components.YAxis;
    import com.github.mikephil.charting.formatter.YAxisValueFormatter;
    
    import java.text.DecimalFormat;
    
    /**
     * Created by wblobin on 2016/8/29.
     * qq:563084073
     */
    public class VolFormatter implements YAxisValueFormatter {
    
        private DecimalFormat mFormat;
        public VolFormatter(DecimalFormat mFormat) {
            this.mFormat = mFormat; 
        }
        @Override
        public String getFormattedValue(float value, YAxis yAxis) {
            return mFormat.format(value);
        }
    }
    
    

    2.1转换工具类Utils.java

    package com.gold678.retrofit2rxjava.a0000kline.tools;
    
    import android.content.Context;
    import android.util.Log;
    
    import com.gold678.retrofit2rxjava.a0000kline.bean.Const;
    import com.gold678.retrofit2rxjava.a0000kline.bean.KLineBean;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    /**
     * Created by wblobin on 2016/8/29.
     * qq:563084073
     */
    public class Utils {
    
        /**
         * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
         */
        public static int dip2px(Context context, float dpValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (dpValue * scale + 0.5f);
        }
    
        /**
         * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
         */
        public static int px2dip(Context context, float pxValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (pxValue / scale + 0.5f);
        }
    
        public float getFloat(String temp) {
    
            float res = 0;
            try {
                res = Float.valueOf(temp);
            } catch (Exception e) {
            }
            return res;
        }
    
        public static List<KLineBean> getJson2KLine(String jsonStr ) {
            List<KLineBean> kLineDatas = new ArrayList<>();
            try {
                JSONArray arrayJson = new JSONArray(jsonStr);
                Log.e("Data4File","----getJson2KLine--------length="+arrayJson.length());
                for (int i = 0; i < arrayJson.length(); i++) {
                    KLineBean kLineData = new KLineBean();
                    JSONObject temp = (JSONObject) arrayJson.get(i);
    
                    kLineData.date = getJsonValue(temp, Const.K_UPDATETIME);
                    kLineData.open = Float.valueOf(getJsonValue(temp, Const.K_OPEN));
                    kLineData.close = Float.valueOf(getJsonValue(temp, Const.K_CLOSE));
                    kLineData.high = Float.valueOf(getJsonValue(temp, Const.K_HIGH));
                    kLineData.low = Float.valueOf(getJsonValue(temp, Const.K_LOW));
                    kLineData.vol = Float.valueOf(getJsonValue(temp, Const.K_VOLUME));
                    kLineDatas.add(kLineData);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            //MyApplication.htlog.e("----------parseKLine2------" + ",Data size="+kLineDatas.size());
            return kLineDatas;
        }
        public static String getJsonValue(JSONObject temp, String key) {
            String res = "";
            try {
                res = temp.getString(key);
            } catch (JSONException e) {
            }
            return res;
        }
    }
    
    

    相关文章

      网友评论

        本文标题:股票行情图:使用到的常量类和工具类

        本文链接:https://www.haomeiwen.com/subject/mmifettx.html