鄙人比较菜,都是一些小功能,也没有优化代码什么的,做的一个小功能,好了先放效果图
内容大概就是如图所示,实现的方式很简单,先说一下思路,然后在上代码,思路很简单,请各位不要见笑,我是把单个的条形图作为一个自定义的布局文件,如图
这个自定义布局文件很简单,上面就一个textview,用来统计当前的条形统计的数量值,PS这里统计的不是统计的条形图的高度。
然后在定义一个imageview,通过线程循环来修改imageview的高度来达到统计条增长的效果,最后定义的一个textview来定义该统计条的类型,让后写一个继承RelativeLayout,实例化这个类的时候,就加载这个自定义的布局文件,在对外公布一个方法,在方法里面通过线程的死循环,通过累加来改变imageview的高度,同时更新条形统计值,好了不废话了上代码。
自定义的布局文件
自定义继承类RelativeLayout
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class ScoreView extends RelativeLayout {
private Context Context;
private TextView ScoreCount;
private ImageView ScoreImg;
private TextView ScoreType;
private LayoutParams ImgParams;
private float ImgHight;// 获取绘制条形图的高度
private float AverageValue;// 获取当前题目数量的平均值
private final int SendCountValue = 1;//发送统计条代表的数值
private final int SendImgValue = 2;//发送统计条的高度
public ScoreView(Context context, AttributeSet attrs) {
super(context, attrs);
Context = context;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View ScoreView = inflater.inflate(R.layout.item_score, this);
ScoreCount = (TextView) ScoreView.findViewById(R.id.score_count);
ScoreImg = (ImageView) ScoreView.findViewById(R.id.score_img);
ImgParams = (LayoutParams) ScoreImg.getLayoutParams();
ScoreType = (TextView) ScoreView.findViewById(R.id.score_type);
}
// 进度条当前的数值,进度条代表的总数值,进度条总高度,条形条底部类型
public void SetData(int count, int total, int hight, String type) {
ScoreCount.setText("0");
ImgParams.height = 0;
ScoreImg.setLayoutParams(ImgParams);
ScoreType.setText(type);
AverageValue = (total / (float) hight);//获取进度条当前高度代表的数值的平均值
ImgHight = hight * (count / (float) total);
LoadRun();
}
//内部统一时间内不同平率通知更新进度条的高度和进度条的代表的数值
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case SendCountValue:
ScoreCount.setText(String.format("%.0f", msg.obj));
break;
case SendImgValue:
ImgParams.height = (Integer) msg.obj;
ScoreImg.setLayoutParams(ImgParams);
break;
default:
break;
}
};
};
//在进入activity就开始更新值
private void LoadRun() {
new Thread(new Runnable() {
@Override
public void run() {
Boolean StopImgValue = false;
float datatime1 = 0;
int datatime2 = 0;
while (StopImgValue == false) {
try {
Thread.sleep(10);
datatime1 += AverageValue;
datatime2 += 1;
handler.sendMessage(handler.obtainMessage(SendCountValue, datatime1));
handler.sendMessage(handler.obtainMessage(SendImgValue, datatime2));
} catch (InterruptedException e) {
e.printStackTrace();
}
if (datatime2 >= ImgHight)
StopImgValue = true;
}
}
}).start();
}
}
然后在你需要的activity里面开始调用就好了
import android.app.Activity;
import android.os.Bundle;
public class ScoreActivity extends Activity {
private ScoreView ScoreView1;
private ScoreView ScoreView2;
private ScoreView ScoreView3;
private ScoreView ScoreView4;
private ScoreView ScoreView5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score);
InitView();
InitMethod();
}
private void InitView() {
ScoreView1 = (ScoreView) findViewById(R.id.scoreView1);
ScoreView2 = (ScoreView) findViewById(R.id.scoreView2);
ScoreView3 = (ScoreView) findViewById(R.id.scoreView3);
ScoreView4 = (ScoreView) findViewById(R.id.scoreView4);
ScoreView5 = (ScoreView) findViewById(R.id.scoreView5);
}
private void InitMethod() {
ScoreView1.SetData(190,200,400,"语言");
ScoreView2.SetData(70,200,400,"艺术");
ScoreView3.SetData(150,200,400,"健康");
ScoreView4.SetData(50,200,400,"社会");
ScoreView5.SetData(100,200,400,"科学");
}
}
然后就可以达到最开的效果了
——Saber