这是activity—main.xml里的
<LinearLayout
android:id="@+id/wang"
android:layout_width="match_parent"
android:layout_height="match_parent"></LinearLayout>
这是MainActivity.java里的
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
LinearLayout layout = (LinearLayout)findViewById(R.id.wang);
final CustomView view=new CustomView(this);
layout.addView(view);
}
}
这是CustomView.java里的
public class CustomView extends View {
private int x,y,nextX,nextY;
public CustomView(Context context) {
super(context);
}
public CustomView(Context context,int x,int y,int nextX,int nextY){
super(context);
this.x=x;
this.y=y;
this.nextX=nextX;
this.nextY=nextY;
init();
}
private void init(){
Paint paint = new Paint(); //设置一个笔刷大小是3的黄色的画笔
paint.setColor(Color.RED);
paint.setStrokeJoin(Paint.Join.ROUND);//设置线段连接处样式
paint.setStrokeCap(Paint.Cap.ROUND);//设置线冒样式,取值有Cap.ROUND(圆形线冒)、Cap.SQUARE(方形线冒)、Paint.Cap.BUTT(无线冒)
paint.setStrokeWidth(10f);
paint.setAntiAlias(true);
ValueAnimator valueAnimatorX =ValueAnimator.ofInt(x,nextX);
valueAnimatorX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
public void onAnimationUpdate(ValueAnimator animation){
nextX=x;
invalidate();
}
});
ValueAnimator valueAnimatorY=ValueAnimator.ofInt(y,nextY);
valueAnimatorY.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
public void onAnimationUpdate (ValueAnimator animation){
nextY = y + 10;
invalidate();
}
});
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(2000);
animatorSet.start();
}
Paint paint;
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//canvas.drawColor(Color.WHITE);
// canvas.drawLine(500 , 70, 200, 500, paint);//from x,y to x,y
canvas.drawLine(nextX,nextY,200,500,paint);
}
}
不知道为什么不对 感谢大家看一下