Back to project page markj.
The source code is released under:
GNU Lesser General Public License
If you think the Android project markj listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.markjmind.mobile.api.android.ui.graph; // w w w .j a v a 2 s . co m import org.json.JSONObject; import android.content.Context; import android.util.AttributeSet; import android.view.Gravity; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.AnimationUtils; import android.view.animation.TranslateAnimation; import android.widget.LinearLayout; /** * ???????? Bar ??? ???? ?????????.<br> * ?????? ??????? Bar? ??? ????. <br> * ??? Head, Body, Bottom?? ????????.<br> * Head? Bar??? ??? ??? ????? ??? Layout??? ????? ??.<br> * Body? ?????? ???? ?????? ?????? ???? ???? ????.<br> * Body?? ??? View? ???? ??? ?????? ??/Layout??? ???????.<br> * Bottom??? Bar ???? ??? ?????,??,layout??? ????? ?? ?????? <br> * ?? Bar??? Bottom? ??????? ????? ??? ??? ???? ???????? ????<br> * ???? ????? ?? ???.<br> * Bottom???? ???????? ????? BarLayout????? foot??? ?????? ??????. * * @author ??? * */ public class Bar extends LinearLayout{ private LayoutParams thisParams; private LinearLayout headLyt; private LinearLayout bodyLyt; private LayoutParams headParams; private LayoutParams bodyParams; private BarBody barBody; private Animation animation; // private ArrayList<BarElement> figures = new ArrayList<BarElement>(); /** * LinearLayout ????????? ????? * @param attrs */ public Bar(Context context, AttributeSet attrs) { super(context); init(); } /** * LinearLayout ????????? ????? * @param context */ public Bar(Context context) { super(context); init(); } /** * Bar??? Layout????? ??? ??. */ public void init(){ this.setOrientation(LinearLayout.VERTICAL); this.setGravity(Gravity.BOTTOM); thisParams = new LayoutParams(0, LayoutParams.MATCH_PARENT,1); thisParams.weight = 1; this.setLayoutParams(thisParams); this.removeAllViews(); headLyt = new LinearLayout(getContext()); bodyLyt = new LinearLayout(getContext()); headParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT); bodyParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); bodyParams.weight=1; headLyt.setLayoutParams(headParams); bodyLyt.setLayoutParams(bodyParams); barBody = new BarBody(getContext()); bodyLyt.addView(barBody); super.addView(headLyt); super.addView(bodyLyt); } /** * Bar??? Max ??? ????. * @param max ?? */ public void setMax(int max){ barBody.setMax(max); } /** * Bar??? Max ??? ????. * @return */ public int getMax(){ return barBody.getMax(); } /** * Bar??? ??? ???? bar? ????. * @param figure */ public Bar addFigure(int figure,int color){ barBody.addFigure(figure,color); return this; } /** * Bar??? ??? ???? bar? ????. * @param figure */ public Bar add(int figure,int color){ barBody.addFigure(figure,color); return this; } /** * Bar??? ??? ????. * @param figure */ public BarElement setFigure(int figure,int element_index){ return getElement(element_index).setFigure(figure); } /** * Bar??? ??? ????. * @param color ?? */ public void setBackgroundColor(int color){ barBody.setBackgroundColor(color); } /** * ??? Id? Bar??? ??? ????. * @param id ??? Id */ public void setBackgroundResource(int id){ barBody.setBackgroundResource(id); } /** * Bar ??????? ????? ????. * @param left ???? * @param top ????? * @param right ????? * @param bottom ???? */ public void setMargin(int left,int top,int right,int bottom){ barBody.setPadding(left, top, right, bottom); } public BarElement getElement(int index){ if(barBody.barMain.getChildCount()>0){ return (BarElement)barBody.barMain.getChildAt(barBody.barMain.getChildCount()-index-1); }else{ return null; } } /** * Bar??? ??? ???? ????? ????<br> * Bar??? ????,?????? ???? ?????????.<br> * ????? ?????? Bar? ???? ??? ?? <br> * ?? ?????? ???? ??. * @author ??? * */ class BarBody extends LinearLayout{ private LayoutParams stickParam; // private int totalFigure=0; public LinearLayout barMain; public LayoutParams barMainParam; private int max=100; /** * LinearLayout ????????? ?????. * @param attrs */ public BarBody(Context context, AttributeSet attrs) { super(context, attrs); init(); } /** * LinearLayout ????????? ?????. * @param context */ public BarBody(Context context) { super(context); init(); } /** * Layout??? ???? ????? ?????. */ public void init(){ //parent setMax(max); this.setOrientation(LinearLayout.VERTICAL); this.setGravity(Gravity.BOTTOM); stickParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); stickParam.weight = 1; this.setLayoutParams(stickParam); this.removeAllViews(); barMain = new LinearLayout(getContext()); barMain.setOrientation(LinearLayout.VERTICAL); barMainParam = new LayoutParams(LayoutParams.MATCH_PARENT, 0); barMain.setLayoutParams(barMainParam); this.addView(barMain); } /** * Bar??? Max ??? ????. * @param max ?? */ public void setMax(int max){ this.max = max; this.setWeightSum(this.max); } /** * Bar??? Max ?????? ????. * @return Max ??? */ public int getMax(){ return this.max; } /** * Bar??? ?????? ???? Bar? ????. * @param figure ??? */ public void addFigure(int figure, int color){ int totalFigure = getTotalFigure(); if(totalFigure<0){ totalFigure=0; } totalFigure = totalFigure+figure; barMain.setWeightSum(totalFigure); barMainParam.weight=totalFigure; barMain.setLayoutParams(barMainParam); BarElement barEl = new BarElement(getContext()); barEl.init(figure,color); // figures.add(barEl); barMain.addView(barEl,0); Animation anim = animation; if(anim==null){ anim =defalutAninmation(); } if(max!=0){ anim.setDuration(totalFigure*150/max*10); barMain.setAnimation(anim); } } public Animation defalutAninmation(){ AnimationSet set = new AnimationSet(true); Animation animation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.9f, Animation.RELATIVE_TO_SELF, 0.0f ); Animation animation2 = new AlphaAnimation(0.1f, 1.0f); set.addAnimation(animation2); set.addAnimation(animation); return set; } /** * Bar??? ?????? ????. * @return ??? */ public int getTotalFigure(){ return (int)barMain.getWeightSum(); } /** * Bar??? ??? ????. * @param color ?? */ public void setBackgroundColor(int color){ for(int i=0;i<barMain.getChildCount();i++){ getElement(i).setBackgroundColor(color); } } /** * ???Id? Bar??? ??? ????. * @param id ??? Id */ public void setBackgroundResource(int id){ for(int i=0;i<barMain.getChildCount();i++){ getElement(i).setBackgroundResource(id); } getElement(0).setBackgroundResource(id); } /** * Bar??? ??? ????. * @param color ?? */ public void setBackgroundColor(int color,int index){ getElement(index).setBackgroundColor(color); } /** * ???Id? Bar??? ??? ????. * @param id ??? Id */ public void setColorResource(int id, int index){ getElement(index).setBackgroundResource(id); } /** * Bar??? ????????? ????. * @param left ?? ?? * @param top ??? ?? * @param right ??? ?? * @param bottom ?? ?? */ public void setMargin(int left,int top,int right,int bottom){ this.setPadding(left, top, right, bottom); } } }