观心静

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

效果图:

 

代码:

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毫秒刷新

    }
}

 

posted on 2018-07-03 11:53  观心静  阅读(278)  评论(0编辑  收藏  举报