效果图:
代码:
package com.example.lenovo.mydemo.myViewDemo; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Typeface; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.util.Log; import android.view.View; import com.example.lenovo.mydemo.R; /** * Created by lenovo on 2018/7/2. */ public class MyView_1 extends View { private final String TAG = "MyView_1"; private Paint mPaint; private int i = 0; public MyView_1(Context context) { super(context); } public MyView_1(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public MyView_1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint = new Paint(); mPaint .setStrokeWidth(20); mPaint.setColor(Color.RED); canvas.drawLine(0,100,i,100,mPaint); if (i==500){ i=0; }else { i=i+10; } postInvalidateDelayed(100);//延时100毫秒刷新 } }
本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/9708599.html
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。