高防比特币界面

来源:互联网 发布:gps里程统计算法 编辑:程序博客网 时间:2024/06/09 22:28
比特币(BitCoin)是一种P2P形式的数字货币。点对点的传输意味着一个去中心化的支付系统。比特币不依靠特定货币机构发行,它依据特定算 法,通过大量的计算产生,比特币经济使用整个P2P网络中众多节点构成的分布式数据库来确认并记录所有的交易行为。P2P的去中心化特性与算法本身可以确 保无法通过大量制造比特币来人为操控币值。基于密码学的设计可以使比特币只能被真实的拥有者转移或支付。这同样确保了货币所有权与流通交易的匿名性。比特币是一种网络虚拟货币,数量有限,具有极强的稀缺性。该货币系统在前4年内只有不超过1050万个,之后的总数量将被永久限制在2100万个之内。
    --前言

   

   
UI系列教程第九课:高仿比特币监控大师    
童鞋们好久不见了,最近蓝老师太忙了一直没有时间更新UI教程,让小伙伴们久等了
今天蓝老
师就重磅推出一款比特币监控app,希望大家能够喜欢
不罗嗦先上效果图







IT界的盆友相信对比特币都有所耳闻,2013年的大起大跌可谓扣人心弦
不过LZ对此没有兴趣所以没有参与,前段时间看到一款名为比特币大师的app
不过用户体验不是很好而且还有广告,为了还用户一片净土,遂决定山寨一款类似应用以娱大众



工程简介:



网络请求方面用的http协议得到json字串然后解析,依然使用android-anyc-http开源框架
UI界面比较麻烦的就是委托信息和最近成交的长度条的处理
其实就是每次得到数据源后对每个项进行计算比例值
然后在adapter里处理view的weight比重
其它一些逻辑处理只要做好相应的类封装并使用设计模式将它们组合起来即可


下面附上一些code fragment

委托信息adapter实现

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.geniusgithub.bcoinmonitor.adapter;  
  2.   
  3. import android.content.Context;  
  4. import android.view.LayoutInflater;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.BaseAdapter;  
  8. import android.widget.LinearLayout;  
  9. import android.widget.TextView;  
  10.   
  11. import com.geniusgithub.bcoinmonitor.R;  
  12. import com.geniusgithub.bcoinmonitor.model.BaseType;  
  13. import com.geniusgithub.bcoinmonitor.model.PublicType.ConinDetail;  
  14. import com.geniusgithub.bcoinmonitor.util.CommonLog;  
  15. import com.geniusgithub.bcoinmonitor.util.LogFactory;  
  16.   
  17. public class DetailListViewAdapter extends BaseAdapter{  
  18.   
  19.     private static final CommonLog log = LogFactory.createLog();  
  20.       
  21.     private ConinDetail data = new ConinDetail();  
  22.     private Context mContext;  
  23.       
  24.     public DetailListViewAdapter(Context context, ConinDetail data)  
  25.     {  
  26.         mContext = context;  
  27.         refreshData(data);  
  28.     }  
  29.       
  30.     public void refreshData(ConinDetail data)  
  31.     {  
  32.         if (data != null){  
  33.             this.data = data;         
  34.         }else{  
  35.             this.data = new ConinDetail();        
  36.         }  
  37.         notifyDataSetChanged();  
  38.   
  39.     }  
  40.       
  41.     @Override  
  42.     public int getCount() {  
  43.           
  44.         int size1 = data.mAsksList.size();  
  45.         int size2 = data.mBidsList.size();  
  46.           
  47.         return Math.min(size1, size2);  
  48.           
  49.     }  
  50.   
  51.     @Override  
  52.     public Object getItem(int pos) {  
  53.   
  54.         return data;  
  55.     }  
  56.   
  57.     @Override  
  58.     public long getItemId(int arg0) {  
  59.   
  60.         return 0;  
  61.     }  
  62.   
  63.     @Override  
  64.     public View getView(int pos, View view, ViewGroup parent) {  
  65.           
  66.           
  67.         ViewHolder viewHolder = null;  
  68.         if (view == null)  
  69.         {  
  70.             view = LayoutInflater.from(mContext).inflate(R.layout.detail_listitem_layout, null);  
  71.             viewHolder = new ViewHolder();  
  72.             viewHolder.ivBuy =  view.findViewById(R.id.v_buy_amount);  
  73.             viewHolder.ivBuyEmpty =  view.findViewById(R.id.v_buy_amount_empty);  
  74.             viewHolder.tvBuyAmount = (TextView) view.findViewById(R.id.tv_buy_amount);  
  75.             viewHolder.tvBuyPrice = (TextView) view.findViewById(R.id.tv_buy_price);  
  76.             viewHolder.tvSellPrice = (TextView) view.findViewById(R.id.tv_sell_price);  
  77.             viewHolder.tvSellAmount = (TextView) view.findViewById(R.id.tv_sell_amount);  
  78.             viewHolder.ivSell = view.findViewById(R.id.v_sell_amount);  
  79.             viewHolder.ivSellEmpty =  view.findViewById(R.id.v_sell_amount_empty);  
  80.             view.setTag(viewHolder);  
  81.         }else{  
  82.             viewHolder = (ViewHolder) view.getTag();  
  83.         }  
  84.           
  85.         ConinDetail object = (ConinDetail) getItem(pos);  
  86.           
  87.         BaseType.ConinInst buyInst = getBuyInst(pos);  
  88.         BaseType.ConinInst sellInst = getSellInst(pos);  
  89.           
  90.         try {  
  91.             String value1 = String.valueOf(buyInst.mCount);  
  92.             viewHolder.tvBuyAmount.setText(value1);  
  93.   
  94.             String value2 = String.valueOf(buyInst.mPrice);       
  95.             viewHolder.tvBuyPrice.setText(value2);  
  96.               
  97.             String value3 = String.valueOf(sellInst.mPrice);          
  98.             viewHolder.tvSellPrice.setText(value3);  
  99.               
  100.             String value4 = String.valueOf(sellInst.mCount);          
  101.             viewHolder.tvSellAmount.setText(value4);  
  102.   
  103.             updateRateView(pos, viewHolder);  
  104.               
  105.         } catch (Exception e) {  
  106.             e.printStackTrace();  
  107.         }     
  108.   
  109.             return view;  
  110.     }  
  111.       
  112.     private void updateRateView(int pos, ViewHolder viewHolder){  
  113.         BaseType.ConinInst buyInst = getBuyInst(pos);  
  114.         BaseType.ConinInst sellInst = getSellInst(pos);  
  115.   
  116.         double rateBuy = buyInst.mRate;  
  117.         double rateBuyEmpty = 1- rateBuy;  
  118.       
  119.   
  120.         double rateSell =  sellInst.mRate;  
  121.         double rateSellEmpty = 1- rateSell;  
  122.   
  123.       
  124.         viewHolder.ivBuy.setLayoutParams(getParamByWeight(1-rateBuy));  
  125.         viewHolder.ivBuyEmpty.setLayoutParams(getParamByWeight(1-rateBuyEmpty));  
  126.   
  127.           
  128.         viewHolder.ivSell.setLayoutParams(getParamByWeight(1-rateSell));  
  129.         viewHolder.ivSellEmpty.setLayoutParams(getParamByWeight(1-rateSellEmpty));  
  130.           
  131.     }  
  132.       
  133.     private LinearLayout.LayoutParams getParamByWeight(double weight){  
  134.             int value = (int) (weight * 100);  
  135.             if (value < 1){  
  136.                 value = 1;  
  137.             }  
  138.             LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(  
  139.             LinearLayout.LayoutParams.FILL_PARENT,  
  140.             LinearLayout.LayoutParams.FILL_PARENT, value);  
  141.             return params;  
  142.     }  
  143.       
  144.     private BaseType.ConinInst getBuyInst(int pos){  
  145.         return data.mBidsList.get(pos);  
  146.     }  
  147.       
  148.     private BaseType.ConinInst getSellInst(int pos){  
  149.         return data.mAsksList.get(pos);  
  150.     }  
  151.       
  152.    static class ViewHolder {   
  153.         public View ivBuy;  
  154.         public View ivBuyEmpty;  
  155.         public TextView tvBuyAmount;  
  156.         public TextView tvBuyPrice;  
  157.         public TextView tvSellPrice;  
  158.         public TextView tvSellAmount;  
  159.         public View ivSell;  
  160.         public View ivSellEmpty;  
  161.     }   
  162.   
  163. }  

application全局类实现

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.geniusgithub.bcoinmonitor;  
  2.   
  3.   
  4. import java.util.HashMap;  
  5.   
  6. import android.app.Activity;  
  7. import android.app.Application;  
  8. import android.app.Notification;  
  9. import android.app.NotificationManager;  
  10. import android.app.PendingIntent;  
  11. import android.content.Context;  
  12. import android.content.Intent;  
  13. import android.os.Handler;  
  14. import android.os.Message;  
  15.   
  16. import com.geniusgithub.bcoinmonitor.activity.MainActivity;  
  17. import com.geniusgithub.bcoinmonitor.datacenter.ConinMarketManager;  
  18. import com.geniusgithub.bcoinmonitor.datacenter.IPriceObser;  
  19. import com.geniusgithub.bcoinmonitor.datastore.LocalConfigSharePreference;  
  20. import com.geniusgithub.bcoinmonitor.model.IBCointType;  
  21. import com.geniusgithub.bcoinmonitor.util.CommonLog;  
  22. import com.geniusgithub.bcoinmonitor.util.LogFactory;  
  23. import com.geniusgithub.bcoinmonitor.util.TipHelper;  
  24. import com.tendcloud.tenddata.TCAgent;  
  25.   
  26.   
  27.   
  28.   
  29. public class MonitorApplication extends Application implements IPriceObser, ItatisticsEvent{  
  30.   
  31.     private static final CommonLog log = LogFactory.createLog();  
  32.     private static final int MSG_SHOW_PRICE_INTERVAL = 0x0001;  
  33.       
  34.     private static MonitorApplication mInstance;  
  35.     private ConinMarketManager mConinMarketManager;  
  36.           
  37.     private Handler mHandler;  
  38.       
  39.     private boolean mEnterMain = false;  
  40.       
  41.     private int mWarnType = IBCointType.BTC_CHINA;  
  42.     private int mLowPrice = 0;  
  43.     private int mHightPrice = 0;  
  44.       
  45.       
  46.     private int mComparePF1 = IBCointType.BTC_CHINA;  
  47.     private int mComparePF2 = IBCointType.BTC_CHINA;  
  48.     private int mPriceInterval = 0;  
  49.       
  50.     private int mNotifyPriceType = -1;  
  51.       
  52.     public synchronized static MonitorApplication getInstance(){  
  53.         return mInstance;  
  54.     }  
  55.       
  56.     @Override  
  57.     public void onCreate() {  
  58.         super.onCreate();  
  59.         log.e("MonitorApplication  onCreate!!!");  
  60.         mInstance = this;  
  61.         mConinMarketManager = ConinMarketManager.getInstance(this);  
  62.         mConinMarketManager.registerPriceObser(this);  
  63.           
  64.         mWarnType = LocalConfigSharePreference.getPriceWarningType(this);  
  65.         mLowPrice = LocalConfigSharePreference.getLowPrice(this);  
  66.         mHightPrice = LocalConfigSharePreference.getHightPrice(this);  
  67.           
  68.         mComparePF1 = LocalConfigSharePreference.getComparePF1(this);  
  69.         mComparePF2 = LocalConfigSharePreference.getComparePF2(this);  
  70.         mPriceInterval = LocalConfigSharePreference.getPriceInterval(this);  
  71.           
  72.         mNotifyPriceType = LocalConfigSharePreference.getPriceNoticeType(this);  
  73.           
  74.         mHandler = new Handler(){  
  75.   
  76.             @Override  
  77.             public void handleMessage(Message msg) {  
  78.                 switch(msg.what){  
  79.                     case MSG_SHOW_PRICE_INTERVAL:  
  80.                         String title = getResources().getString(R.string.notify_title_compare);  
  81.                         sendNotifycation(title, notifyPriceIntervalString);  
  82.                         TipHelper.Vibrate(MonitorApplication.this, 2000);  
  83.                         break;  
  84.                 }  
  85.             }  
  86.               
  87.         };  
  88.         startBackgroundService();  
  89.     }  
  90.       
  91.     private void startBackgroundService(){  
  92.         Intent intent = new Intent();  
  93.         intent.setClass(this, BackgroundService.class);  
  94.         startService(intent);  
  95.     }  
  96.       
  97.     public void setStatus(boolean flag){  
  98.         mEnterMain = flag;  
  99.     }  
  100.       
  101.     public boolean getEnterFlag(){  
  102.         return mEnterMain;  
  103.     }  
  104.       
  105.     public void exitProcess(){  
  106.         log.e("MonitorApplication  exitProcess!!!");  
  107.         Intent intent = new Intent();  
  108.         intent.setClass(this, BackgroundService.class);  
  109.         stopService(intent);  
  110.         System.exit(0);  
  111.     }  
  112.   
  113.     public void setWarnPlatiformInfo(int type, int lowPrice, int hightPrice){  
  114.         mWarnType = type;  
  115.         mLowPrice = lowPrice;  
  116.         mHightPrice = hightPrice;  
  117.         log.e("setNoticeInfo type = "  + type + "lowPrice = " + lowPrice + "hightPrice = " + hightPrice);  
  118.     }  
  119.       
  120.     public void setCompareInfo(int comparePF1, int comparePF2, int priceInterval){  
  121.         mComparePF1 = comparePF1;  
  122.         mComparePF2 = comparePF2;  
  123.         mPriceInterval = priceInterval;  
  124.         log.e("setCompareInfo  mComparePF1  = "  + mComparePF1 + "mComparePF2 = " + mComparePF2 + "mPriceInterval = " + mPriceInterval);  
  125.     }  
  126.   
  127.     public void setNotifyPlatiformInfo(int type){  
  128.         mNotifyPriceType = type;  
  129.         log.e("setNotifyPlatiformInfo type = "  + mNotifyPriceType);  
  130.     }  
  131.       
  132.     @Override  
  133.     public void onPriceUpdate(int type, String price) {  
  134.           
  135.         if (mNotifyPriceType != -1 && mNotifyPriceType == type){  
  136.             double priDouble = Double.valueOf(price);  
  137.             int iprice = (int)priDouble;  
  138.             sendLastPrice(mNotifyPriceType, iprice);  
  139.         }  
  140.           
  141.         if (type == mComparePF1 || type == mComparePF2){      
  142.           
  143.             if (mComparePF1 != mComparePF2 && mPriceInterval != 0){  
  144.                 int price1 = mConinMarketManager.getLastPrice(mComparePF1);  
  145.                 int price2 = mConinMarketManager.getLastPrice(mComparePF2);  
  146.                   
  147.                 if (price1 != 0 && price2 != 0){  
  148.                     if (Math.abs(price1 - price2) >= mPriceInterval){  
  149.                         log.e("price1 = " + price1 + "price2 = " + price2 + "mPriceInterval = " + mPriceInterval + ", warning!!!");  
  150.                         sendPriceCompareWarning(mComparePF1, mComparePF2, Math.abs(price1 - price2));  
  151.                     }  
  152.                 }  
  153.             }  
  154.         }  
  155.           
  156.           
  157.   
  158.         if (type == mWarnType){  
  159.             double priDouble = Double.valueOf(price);  
  160.             int priInter = (int)priDouble;  
  161.               
  162.             if (priInter >= mLowPrice && priInter <= mHightPrice){  
  163.                 log.e("priInter = " + priInter + "mLowPrice = " + mLowPrice + "mHightPrice = " + mHightPrice + ", warning!!!");  
  164.                 sendPriceUpdateWarning(mWarnType, priInter);  
  165.             }  
  166.         }  
  167.   
  168.     }  
  169.   
  170.     private String notifyPriceIntervalString = "";  
  171.     private void sendPriceCompareWarning(int comparePF1,int comparePF2, int priceInterval){  
  172.           
  173.         StringBuffer stringBuffer = new StringBuffer();  
  174.         stringBuffer.append(getPfName(comparePF1) + "和" + getPfName(comparePF2) + ", 价格差在" + String.valueOf(priceInterval));  
  175.           
  176.         notifyPriceIntervalString = stringBuffer.toString();  
  177.         mHandler.removeMessages(MSG_SHOW_PRICE_INTERVAL);  
  178.         mHandler.sendEmptyMessageDelayed(MSG_SHOW_PRICE_INTERVAL, 3000);  
  179.           
  180.     }  
  181.       
  182.     private void sendPriceUpdateWarning(int pfType, int newPrice){  
  183.         String title = getResources().getString(R.string.notify_title_newprice);  
  184.           
  185.         StringBuffer stringBuffer = new StringBuffer();  
  186.         stringBuffer.append(getPfName(pfType) + "最新成交价:" + String.valueOf(newPrice));  
  187.           
  188.         sendNotifycation(title, stringBuffer.toString());  
  189.           
  190.         TipHelper.Vibrate(this, 2000);  
  191.     }  
  192.       
  193.     private void sendLastPrice(int pfType, int newPrice){  
  194.         String title = getResources().getString(R.string.notify_title_lastprice);  
  195.           
  196.         StringBuffer stringBuffer = new StringBuffer();  
  197.         stringBuffer.append(getPfName(pfType) + "最新成交价:" + String.valueOf(newPrice));  
  198.           
  199.         sendNotifycation(title, stringBuffer.toString());  
  200.     }  
  201.       
  202.       
  203.     private void sendNotifycation(String title, String content){  
  204.         NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);                 
  205.         Notification n = new Notification(R.drawable.icon, content, System.currentTimeMillis());               
  206.         n.flags = Notification.FLAG_AUTO_CANCEL;                  
  207.         Intent i = new Intent(this, MainActivity.class);  
  208.         i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);             
  209.         //PendingIntent  
  210.         PendingIntent contentIntent = PendingIntent.getActivity(  
  211.                 this,  
  212.                 R.string.app_name,   
  213.                 i,   
  214.                 PendingIntent.FLAG_UPDATE_CURRENT);  
  215.                            
  216.         n.setLatestEventInfo(  
  217.                 this,  
  218.                 title,   
  219.                 content,   
  220.                 contentIntent);  
  221.         nm.notify(R.string.app_name, n);  
  222.           
  223.           
  224.     }  
  225.       
  226.     private String getPfName(int type){  
  227.         String[] names = getResources().getStringArray(R.array.platiform_name);  
  228.         return names[type];  
  229.     }  
  230.   
  231.   
  232.     @Override  
  233.     public void onEvent(String eventID) {  
  234.         log.e("eventID = " + eventID);  
  235.           
  236.         TCAgent.onEvent(this, eventID);  
  237.     }  
  238.   
  239.     @Override  
  240.     public void onEvent(String eventID, HashMap<String, String> map) {  
  241.         log.e("eventID = " + eventID);  
  242.       
  243.         TCAgent.onEvent(this, eventID, "", map);  
  244.     }  
  245.       
  246.     public static void onPause(Activity context){  
  247.   
  248.         TCAgent.onPause(context);  
  249.     }  
  250.       
  251.     public static void onResume(Activity context){  
  252.       
  253.         TCAgent.onResume(context);  
  254.     }  
  255.       
  256.     public static void onCatchError(Context context){  
  257.       
  258.         TCAgent.setReportUncaughtExceptions(true);  
  259.     }  
  260. }  

其它详细代码小伙伴们下project看吧github下载链接:https://github.com/geniusgithub/BtcMonitor

温馨提示
如果您觉得本文有用,请关注窝
新浪微博:android火星人

github主页:https://github.com/geniusgithub

小伙伴们的支持是蓝老师持续创造的动力(支持请给一个“顶”)


more brilliant,Please pay attention to my CSDN blog -->http://blog.csdn.net/geniuseoe2012


0 0
原创粉丝点击