Lance老师UI系列教程第九课->高仿比特币监控大师

比特币(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实现

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 -->http://blog.csdn.net/geniuseoe2012


上一课:Lance老师UI系列教程第八课->新浪新闻SlidingMenu界面的实现

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页