package com.baidu.baidumaps.track.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.baidu.BaiduMap.R;
import com.baidu.baidumaps.common.util.ScreenUtils;
import java.util.ArrayList;
/**
* 导航信息统计柱状图
* @author ouyangkun
*
*/
public class HistogramView extends LinearLayout {
/**
* 内部使用的分区数据结构
*/
public static class Item {
public int walkDistance;
public int carDistance;
public int month;
public Item(int month, int walkDistance, int carDistance) {
this.month = month;
this.walkDistance = walkDistance;
this.carDistance = carDistance;
}
@Override public String toString() {
return "Item{" +
"walkDistance=" + walkDistance +
", carDistance=" + carDistance +
", month=" + month +
'}';
}
}
private LayoutInflater mInflater;
private Context mContext;
public final static int ITEM_MAX_WIDTH = 350;
public final static int ITEM_HEIGHT = 25;
public HistogramView(Context context) {
super(context);
init(context);
}
public HistogramView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
mContext = context;
mInflater = LayoutInflater.from(context);
}
public void setHistogramItems(ArrayList<Item> items){
Log.e("tag", "items="+items);
if(items == null) {
return;
}
float max = getMaxDistance(items);
Log.e("tag", "max="+max);
for (int i = 0; i < items.size(); i++){
LinearLayout view = (LinearLayout) mInflater.inflate(R.layout.track_navi_histogram_item, this, false);
TextView tvMonth = (TextView)view.findViewById(R.id.tv_month);
TextView tvCarDistance = (TextView)view.findViewById(R.id.tv_car_distance);
TextView tvWalkDistance = (TextView)view.findViewById(R.id.tv_walk_distance);
Item item = items.get(i);
tvMonth.setText(item.month + "月");
tvCarDistance.setText(item.carDistance + "");
tvWalkDistance.setText(item.walkDistance + "");
tvCarDistance.setWidth(30);
float carRatio = item.carDistance / max;
LayoutParams carParams = new LayoutParams((int)(ITEM_MAX_WIDTH * carRatio), ITEM_HEIGHT);
tvCarDistance.setLayoutParams(carParams);
float walkRatio = item.walkDistance / max;
LayoutParams walkParams = new LayoutParams((int)(ITEM_MAX_WIDTH * walkRatio), ITEM_HEIGHT);
tvWalkDistance.setLayoutParams(walkParams);
addView(view);
}
//底部横线布局
LinearLayout linearLayout = new LinearLayout(mContext);
LayoutParams linearLayoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
linearLayoutParams.topMargin = -ScreenUtils.dip2px(10, mContext);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setLayoutParams(linearLayoutParams);
//横线
ImageView imageView = new ImageView(mContext);
LayoutParams imageParams = new LayoutParams(ITEM_MAX_WIDTH+10, ScreenUtils.dip2px(1, mContext));
imageParams.leftMargin = ScreenUtils.dip2px(30, mContext);
imageParams.gravity = Gravity.CENTER_VERTICAL;
imageView.setLayoutParams(imageParams);
imageView.setBackgroundColor(mContext.getResources().getColor(R.color.track_histogram_frame));
linearLayout.addView(imageView);
//文本km
TextView textView = new TextView(mContext);
LayoutParams textViewParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textViewParams.leftMargin = ScreenUtils.dip2px(5, mContext);
textView.setText("km");
textView.setLayoutParams(textViewParams);
linearLayout.addView(textView);
addView(linearLayout);
}
private float getMaxDistance(ArrayList<Item> items){
if(items == null) {
return 0;
}
float max = 0;
for (int i = 0; i < items.size(); i++){
Item item = items.get(i);
float temp = item.walkDistance + item.carDistance;
if(temp > max){
max = temp;
}
}
return max;
}
}