自定义View


View生命周期相关方法:
onFinishInflate() 当View中所有的子控件均被映射成xml后触发
onMeasure( int , int ) 确定所有子元素的大小
onLayout( boolean , int , int , int , int ) 当View分配所有的子元素的大小和位置时触发
onSizeChanged( int , int , int , int ) 当view的大小发生变化时触发
onDraw(Canvas) view渲染内容的细节
onKeyDown( int , KeyEvent) 有按键按下后触发
onKeyUp( int , KeyEvent) 有按键按下后弹起时触发
onTrackballEvent(MotionEvent) 轨迹球事件
onTouchEvent(MotionEvent) 触屏事件
onFocusChanged( boolean , int , Rect) 当View获取或失去焦点时触发
onWindowFocusChanged( boolean ) 当窗口包含的view获取或失去焦点时触发
onAttachedToWindow() 当view被附着到一个窗口时触发
onDetachedFromWindow() 当view离开附着的窗口时触发,Android123提示该方法和 onAttachedToWindow() 是相反的。
onWindowVisibilityChanged( int ) 当窗口中包含的可见的view发生变化时触发
综上所述:View 的关键生命周期为 [改变可见性] --> 构造View --> onFinishInflate --> onAttachedToWindow --> onMeasure --> onSizeChanged --> onLayout --> onDraw --> onDetackedFromWindow

自定义view实现风扇

public class Coust2View extends View implements Runnable{

    Paint paintR= new Paint();
    Paint paintB= new Paint();
    Paint paintY = new Paint();
    Paint paintG = new Paint();
    int height;
    int width;

    int startB;
    int startR;
    int startY;
    public Coust2View(Context context, AttributeSet attrs) {
        super(context, attrs);

        initPaint();
    }

    private void initPaint() {

        paintR.setStrokeWidth(10);
        paintR.setStyle(Paint.Style.FILL);
        paintR.setColor(Color.RED);
        paintR.setAntiAlias(true);

        paintB.setStrokeWidth(10);
        paintB.setStyle(Paint.Style.FILL);
        paintB.setColor(Color.BLUE);
        paintB.setAntiAlias(true);

        paintY.setStrokeWidth(10);
        paintY.setStyle(Paint.Style.FILL);
        paintY.setColor(Color.YELLOW);
        paintY.setAntiAlias(true);

        paintG.setStrokeWidth(10);
        paintG.setStyle(Paint.Style.FILL);
        paintG.setColor(Color.GRAY);
        paintG.setAntiAlias(true);


    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.width=w;
        this.height=h;

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //圆
        RectF rectF1 = new RectF(width/2-50,height/2-125,width/2+50,height/2-75);
        canvas.drawOval(rectF1,paintG);
        //线
        canvas.drawLine(width/2,height/2,width/2,height/2-100,paintG);

        //设置扇形的圆形区域
        RectF rectF = new RectF(-(width/4),-(width/4),(width/4),(width/4));
        //sweepAngle扇形扫过的弧度
        canvas.translate(width/2,height/2);
        canvas.drawArc(rectF,startB,30,true,paintB);
        canvas.drawArc(rectF,startR,30,true,paintR);
        canvas.drawArc(rectF,startY,30,true,paintY);

    }

    @Override
    public void run() {
        while (true){
            startR+=1;
            startB=startR+120;
            startY=startB+120;

            postInvalidate();

            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Activity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Coust2View coustView = findViewById(R.id.myview);

    new Thread(coustView).start();
    }
}

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<com.example.a123.day4lianxi2.Coust2View
    android:id="@+id/myview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

效果
在这里插入图片描述

圆变化大小

自定义View

 public class CoustView extends View implements Runnable{

Context context;
int width;
int height;
Paint paint = new Paint();
int raduis;
public CoustView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context=context;
    initPaint();
}

private void initPaint() {
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

    width = windowManager.getDefaultDisplay().getWidth();
    height = windowManager.getDefaultDisplay().getHeight();

    paint.setAntiAlias(true);
    paint.setColor(Color.RED);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(10);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

   canvas.drawCircle(width/2,height/2,raduis,paint);

}


@Override
public void run() {
    while (true){
        if (raduis<500){
            raduis+=20;
        }else{
            raduis=20;
        }

        postInvalidate();

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
}

Activity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CoustView coustView = findViewById(R.id.myview);

    new Thread(coustView).start();
}
}

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<com.example.a123.day4lianxi2.CoustView
    android:id="@+id/myview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

效果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值