public class MyWhereLineDegree extends View {
public MyWhereLineDegree(Context context) {
super(context);
init();
}
public MyWhereLineDegree(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public MyWhereLineDegree(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ArrayList<Integer> datas = new ArrayList<Integer>();
public ArrayList<Integer> colors = new ArrayList<Integer>();
public int max = 0;
public int min = 0;
// 这是默认的
private int suggestW = 200;
private int suggestH = 200;
private Paint paint;
public ObjectAnimator animator;
@SuppressLint("NewApi")
public void init() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeCap(Cap.ROUND);
paint.setColor(Color.YELLOW);
paint.setStrokeWidth(10);
datas.add(7); //模拟温度
datas.add(5); //
datas.add(5);
datas.add(6);
datas.add(7);
datas.add(6);
max = Collections.max(datas);
min = Collections.min(datas);
Random random = new Random(System.currentTimeMillis());
colors.add(0xff << 24 | random.nextInt(0xffffff));
colors.add(0xff << 24 | random.nextInt(0xffffff));
colors.add(0xff << 24 | random.nextInt(0xffffff));
colors.add(0xff << 24 | random.nextInt(0xffffff));
colors.add(0xff << 24 | random.nextInt(0xffffff));
colors.add(0xff << 24 | random.nextInt(0xffffff));
colors.add(0xff << 24 | random.nextInt(0xffffff));
animator = ObjectAnimator.ofInt(new Object(), "mimi", 0,100);
animator.setDuration(5000);
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
animValue = (Integer) animation.getAnimatedValue();
invalidate();
}
});
handler.postDelayed(new Runnable() {
@Override
public void run() {
animator.start();
}
}, 1000);
}
public Handler handler = new Handler();
public int animValue = 0;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
}
private int measureWidth(int measureSpec) {
int result;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = (int) (suggestW + getPaddingLeft()
+ getPaddingRight());
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
/**
* Determines the height of this view
*
* @param measureSpec A measureSpec packed into an int
* @return The height of the view, honoring constraints from measureSpec
*/
private int measureHeight(int measureSpec) {
int result;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = suggestH + getPaddingTop()
+ getPaddingBottom();
if (specMode == MeasureSpec.AT_MOST) {
// Respect AT_MOST value if that was what is called for by
// measureSpec
result = Math.min(result, specSize);
}
}
return result;
}
public int screenW = 0;
public int screenH = 0;
public int marginLeft = 50;
public int marginRight = 50;
public int marginTop = 10;
public int marginBottom = 10;
public Path path = new Path();
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLUE);
screenW = getMeasuredWidth();
screenH = getMeasuredHeight();
paint.setStyle(Style.STROKE);
paint.setStrokeCap(Cap.ROUND);
paint.setColor(Color.YELLOW);
paint.setStrokeWidth(20);
if(animValue > 0){
path.reset();
int size = datas.size();
int tempSize = (int)(((float)animValue/100f)* size);
int sumW = screenW - marginLeft - marginRight;
int sumH = screenH - marginTop - marginBottom;
int perW = sumW / (size -1);
int perH = sumH / (max - min);
for(int i=0;i<tempSize;i++){
int x = marginLeft + i*perW;
int y = (max - datas.get(i))*perH + marginTop;
if(i==0){
path.moveTo(x, y);
}else{
path.lineTo(x, y);
}
canvas.drawPoint(x, y, paint);
}
paint.setStyle(Style.STROKE);
paint.setStrokeCap(Cap.ROUND);
paint.setColor(Color.CYAN);
paint.setStrokeWidth(5);
canvas.drawPath(path, paint);
}
}
}
2:布局为:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.example.bbbbe.MyWhereLineDegree
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_centerInParent="true"
android:text="@string/hello_world" />
</RelativeLayout>
3: 效果为: