119
魔獸
高防比特幣界麵
比特幣(BitCoin)是一種P2P形式的數字貨幣。點對點的傳輸意味著一個去中心化的支付係統。比特幣不依靠特定貨幣機構發行,它依據特定算 法,通過大量的計算產生,比特幣經濟使用整個P2P網絡中眾多節點構成的分布式數據庫來確認並記錄所有的交易行為。P2P的去中心化特性與算法本身可以確 保無法通過大量製造比特幣來人為操控幣值。基於密碼學的設計可以使比特幣隻能被真實的擁有者轉移或支付。這同樣確保了貨幣所有權與流通交易的匿名性。比特幣是一種網絡虛擬貨幣,數量有限,具有極強的稀缺性。該貨幣係統在前4年內隻有不超過1050萬個,之後的總數量將被永久限製在2100萬個之內。--前言
UI係列教程第九課:高仿比特幣監控大師
童鞋們好久不見了,最近藍老師太忙了一直沒有時間更新UI教程,讓小夥伴們久等了今天藍老童師就重磅推出一款比特幣監控app,希望大家能夠喜歡
不羅嗦先上效果圖

不過LZ對此沒有興趣所以沒有參與,前段時間看到一款名為比特幣大師的app
不過用戶體驗不是很好而且還有廣告,為了還用戶一片淨土,遂決定山寨一款類似應用以娛大眾
工程簡介:
UI界麵比較麻煩的就是委托信息和最近成交的長度條的處理
其實就是每次得到數據源後對每個項進行計算比例值
然後在adapter裏處理view的weight比重
其它一些邏輯處理隻要做好相應的類封裝並使用設計模式將它們組合起來即可
下麵附上一些code fragment
委托信息adapter實現
- package com.geniusgithub.bcoinmonitor.adapter;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import com.geniusgithub.bcoinmonitor.R;
- import com.geniusgithub.bcoinmonitor.model.BaseType;
- import com.geniusgithub.bcoinmonitor.model.PublicType.ConinDetail;
- import com.geniusgithub.bcoinmonitor.util.CommonLog;
- import com.geniusgithub.bcoinmonitor.util.LogFactory;
- public class DetailListViewAdapter extends BaseAdapter{
- private static final CommonLog log = LogFactory.createLog();
- private ConinDetail data = new ConinDetail();
- private Context mContext;
- public DetailListViewAdapter(Context context, ConinDetail data)
- {
- mContext = context;
- refreshData(data);
- }
- public void refreshData(ConinDetail data)
- {
- if (data != null){
- this.data = data;
- }else{
- this.data = new ConinDetail();
- }
- notifyDataSetChanged();
- }
- @Override
- public int getCount() {
- int size1 = data.mAsksList.size();
- int size2 = data.mBidsList.size();
- return Math.min(size1, size2);
- }
- @Override
- public Object getItem(int pos) {
- return data;
- }
- @Override
- public long getItemId(int arg0) {
- return 0;
- }
- @Override
- public View getView(int pos, View view, ViewGroup parent) {
- ViewHolder viewHolder = null;
- if (view == null)
- {
- view = LayoutInflater.from(mContext).inflate(R.layout.detail_listitem_layout, null);
- viewHolder = new ViewHolder();
- viewHolder.ivBuy = view.findViewById(R.id.v_buy_amount);
- viewHolder.ivBuyEmpty = view.findViewById(R.id.v_buy_amount_empty);
- viewHolder.tvBuyAmount = (TextView) view.findViewById(R.id.tv_buy_amount);
- viewHolder.tvBuyPrice = (TextView) view.findViewById(R.id.tv_buy_price);
- viewHolder.tvSellPrice = (TextView) view.findViewById(R.id.tv_sell_price);
- viewHolder.tvSellAmount = (TextView) view.findViewById(R.id.tv_sell_amount);
- viewHolder.ivSell = view.findViewById(R.id.v_sell_amount);
- viewHolder.ivSellEmpty = view.findViewById(R.id.v_sell_amount_empty);
- view.setTag(viewHolder);
- }else{
- viewHolder = (ViewHolder) view.getTag();
- }
- ConinDetail object = (ConinDetail) getItem(pos);
- BaseType.ConinInst buyInst = getBuyInst(pos);
- BaseType.ConinInst sellInst = getSellInst(pos);
- try {
- String value1 = String.valueOf(buyInst.mCount);
- viewHolder.tvBuyAmount.setText(value1);
- String value2 = String.valueOf(buyInst.mPrice);
- viewHolder.tvBuyPrice.setText(value2);
- String value3 = String.valueOf(sellInst.mPrice);
- viewHolder.tvSellPrice.setText(value3);
- String value4 = String.valueOf(sellInst.mCount);
- viewHolder.tvSellAmount.setText(value4);
- updateRateView(pos, viewHolder);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return view;
- }
- private void updateRateView(int pos, ViewHolder viewHolder){
- BaseType.ConinInst buyInst = getBuyInst(pos);
- BaseType.ConinInst sellInst = getSellInst(pos);
- double rateBuy = buyInst.mRate;
- double rateBuyEmpty = 1- rateBuy;
- double rateSell = sellInst.mRate;
- double rateSellEmpty = 1- rateSell;
- viewHolder.ivBuy.setLayoutParams(getParamByWeight(1-rateBuy));
- viewHolder.ivBuyEmpty.setLayoutParams(getParamByWeight(1-rateBuyEmpty));
- viewHolder.ivSell.setLayoutParams(getParamByWeight(1-rateSell));
- viewHolder.ivSellEmpty.setLayoutParams(getParamByWeight(1-rateSellEmpty));
- }
- private LinearLayout.LayoutParams getParamByWeight(double weight){
- int value = (int) (weight * 100);
- if (value < 1){
- value = 1;
- }
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.FILL_PARENT,
- LinearLayout.LayoutParams.FILL_PARENT, value);
- return params;
- }
- private BaseType.ConinInst getBuyInst(int pos){
- return data.mBidsList.get(pos);
- }
- private BaseType.ConinInst getSellInst(int pos){
- return data.mAsksList.get(pos);
- }
- static class ViewHolder {
- public View ivBuy;
- public View ivBuyEmpty;
- public TextView tvBuyAmount;
- public TextView tvBuyPrice;
- public TextView tvSellPrice;
- public TextView tvSellAmount;
- public View ivSell;
- public View ivSellEmpty;
- }
- }
application全局類實現
- package com.geniusgithub.bcoinmonitor;
- import java.util.HashMap;
- import android.app.Activity;
- import android.app.Application;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Handler;
- import android.os.Message;
- import com.geniusgithub.bcoinmonitor.activity.MainActivity;
- import com.geniusgithub.bcoinmonitor.datacenter.ConinMarketManager;
- import com.geniusgithub.bcoinmonitor.datacenter.IPriceObser;
- import com.geniusgithub.bcoinmonitor.datastore.LocalConfigSharePreference;
- import com.geniusgithub.bcoinmonitor.model.IBCointType;
- import com.geniusgithub.bcoinmonitor.util.CommonLog;
- import com.geniusgithub.bcoinmonitor.util.LogFactory;
- import com.geniusgithub.bcoinmonitor.util.TipHelper;
- import com.tendcloud.tenddata.TCAgent;
- public class MonitorApplication extends Application implements IPriceObser, ItatisticsEvent{
- private static final CommonLog log = LogFactory.createLog();
- private static final int MSG_SHOW_PRICE_INTERVAL = 0x0001;
- private static MonitorApplication mInstance;
- private ConinMarketManager mConinMarketManager;
- private Handler mHandler;
- private boolean mEnterMain = false;
- private int mWarnType = IBCointType.BTC_CHINA;
- private int mLowPrice = 0;
- private int mHightPrice = 0;
- private int mComparePF1 = IBCointType.BTC_CHINA;
- private int mComparePF2 = IBCointType.BTC_CHINA;
- private int mPriceInterval = 0;
- private int mNotifyPriceType = -1;
- public synchronized static MonitorApplication getInstance(){
- return mInstance;
- }
- @Override
- public void onCreate() {
- super.onCreate();
- log.e("MonitorApplication onCreate!!!");
- mInstance = this;
- mConinMarketManager = ConinMarketManager.getInstance(this);
- mConinMarketManager.registerPriceObser(this);
- mWarnType = LocalConfigSharePreference.getPriceWarningType(this);
- mLowPrice = LocalConfigSharePreference.getLowPrice(this);
- mHightPrice = LocalConfigSharePreference.getHightPrice(this);
- mComparePF1 = LocalConfigSharePreference.getComparePF1(this);
- mComparePF2 = LocalConfigSharePreference.getComparePF2(this);
- mPriceInterval = LocalConfigSharePreference.getPriceInterval(this);
- mNotifyPriceType = LocalConfigSharePreference.getPriceNoticeType(this);
- mHandler = new Handler(){
- @Override
- public void handleMessage(Message msg) {
- switch(msg.what){
- case MSG_SHOW_PRICE_INTERVAL:
- String title = getResources().getString(R.string.notify_title_compare);
- sendNotifycation(title, notifyPriceIntervalString);
- TipHelper.Vibrate(MonitorApplication.this, 2000);
- break;
- }
- }
- };
- startBackgroundService();
- }
- private void startBackgroundService(){
- Intent intent = new Intent();
- intent.setClass(this, BackgroundService.class);
- startService(intent);
- }
- public void setStatus(boolean flag){
- mEnterMain = flag;
- }
- public boolean getEnterFlag(){
- return mEnterMain;
- }
- public void exitProcess(){
- log.e("MonitorApplication exitProcess!!!");
- Intent intent = new Intent();
- intent.setClass(this, BackgroundService.class);
- stopService(intent);
- System.exit(0);
- }
- public void setWarnPlatiformInfo(int type, int lowPrice, int hightPrice){
- mWarnType = type;
- mLowPrice = lowPrice;
- mHightPrice = hightPrice;
- log.e("setNoticeInfo type = " + type + ", lowPrice = " + lowPrice + ", hightPrice = " + hightPrice);
- }
- public void setCompareInfo(int comparePF1, int comparePF2, int priceInterval){
- mComparePF1 = comparePF1;
- mComparePF2 = comparePF2;
- mPriceInterval = priceInterval;
- log.e("setCompareInfo mComparePF1 = " + mComparePF1 + ", mComparePF2 = " + mComparePF2 + ", mPriceInterval = " + mPriceInterval);
- }
- public void setNotifyPlatiformInfo(int type){
- mNotifyPriceType = type;
- log.e("setNotifyPlatiformInfo type = " + mNotifyPriceType);
- }
- @Override
- public void onPriceUpdate(int type, String price) {
- if (mNotifyPriceType != -1 && mNotifyPriceType == type){
- double priDouble = Double.valueOf(price);
- int iprice = (int)priDouble;
- sendLastPrice(mNotifyPriceType, iprice);
- }
- if (type == mComparePF1 || type == mComparePF2){
- if (mComparePF1 != mComparePF2 && mPriceInterval != 0){
- int price1 = mConinMarketManager.getLastPrice(mComparePF1);
- int price2 = mConinMarketManager.getLastPrice(mComparePF2);
- if (price1 != 0 && price2 != 0){
- if (Math.abs(price1 - price2) >= mPriceInterval){
- log.e("price1 = " + price1 + ", price2 = " + price2 + ", mPriceInterval = " + mPriceInterval + ", warning!!!");
- sendPriceCompareWarning(mComparePF1, mComparePF2, Math.abs(price1 - price2));
- }
- }
- }
- }
- if (type == mWarnType){
- double priDouble = Double.valueOf(price);
- int priInter = (int)priDouble;
- if (priInter >= mLowPrice && priInter <= mHightPrice){
- log.e("priInter = " + priInter + ", mLowPrice = " + mLowPrice + ", mHightPrice = " + mHightPrice + ", warning!!!");
- sendPriceUpdateWarning(mWarnType, priInter);
- }
- }
- }
- private String notifyPriceIntervalString = "";
- private void sendPriceCompareWarning(int comparePF1,int comparePF2, int priceInterval){
- StringBuffer stringBuffer = new StringBuffer();
- stringBuffer.append(getPfName(comparePF1) + "和" + getPfName(comparePF2) + ", 價格差在" + String.valueOf(priceInterval));
- notifyPriceIntervalString = stringBuffer.toString();
- mHandler.removeMessages(MSG_SHOW_PRICE_INTERVAL);
- mHandler.sendEmptyMessageDelayed(MSG_SHOW_PRICE_INTERVAL, 3000);
- }
- private void sendPriceUpdateWarning(int pfType, int newPrice){
- String title = getResources().getString(R.string.notify_title_newprice);
- StringBuffer stringBuffer = new StringBuffer();
- stringBuffer.append(getPfName(pfType) + "最新成交價:" + String.valueOf(newPrice));
- sendNotifycation(title, stringBuffer.toString());
- TipHelper.Vibrate(this, 2000);
- }
- private void sendLastPrice(int pfType, int newPrice){
- String title = getResources().getString(R.string.notify_title_lastprice);
- StringBuffer stringBuffer = new StringBuffer();
- stringBuffer.append(getPfName(pfType) + "最新成交價:" + String.valueOf(newPrice));
- sendNotifycation(title, stringBuffer.toString());
- }
- private void sendNotifycation(String title, String content){
- NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
- Notification n = new Notification(R.drawable.icon, content, System.currentTimeMillis());
- n.flags = Notification.FLAG_AUTO_CANCEL;
- Intent i = new Intent(this, MainActivity.class);
- i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
- //PendingIntent
- PendingIntent contentIntent = PendingIntent.getActivity(
- this,
- R.string.app_name,
- i,
- PendingIntent.FLAG_UPDATE_CURRENT);
- n.setLatestEventInfo(
- this,
- title,
- content,
- contentIntent);
- nm.notify(R.string.app_name, n);
- }
- private String getPfName(int type){
- String[] names = getResources().getStringArray(R.array.platiform_name);
- return names[type];
- }
- @Override
- public void onEvent(String eventID) {
- log.e("eventID = " + eventID);
- TCAgent.onEvent(this, eventID);
- }
- @Override
- public void onEvent(String eventID, HashMap<String, String> map) {
- log.e("eventID = " + eventID);
- TCAgent.onEvent(this, eventID, "", map);
- }
- public static void onPause(Activity context){
- TCAgent.onPause(context);
- }
- public static void onResume(Activity context){
- TCAgent.onResume(context);
- }
- public static void onCatchError(Context context){
- TCAgent.setReportUncaughtExceptions(true);
- }
- }
其它詳細代碼小夥伴們下project看吧github下載鏈接:https://github.com/geniusgithub/BtcMonitor
溫馨提示
如果您覺得本文有用,請關注窩
新浪微博:android火星人
github主頁:https://github.com/geniusgithub
小夥伴們的支持是藍老師持續創造的動力(支持請給一個“頂”)

more brilliant,Please pay attention to my CSDN blog -->https://blog.csdn.net/geniuseoe2012
最後更新:2017-04-03 12:56:01