사용자 정의view 구현 계기판, 시계

11539 단어
단순 난폭한 코드
 WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    int mWidth = wm.getDefaultDisplay().getWidth();
    int mHeight = wm.getDefaultDisplay().getHeight();
   /* public int mWidth = getWidth();
    public int mHeight = getHeight();*/

    public YiBiaoPan(Context context) {
        super(context);
    }

    public YiBiaoPan(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public YiBiaoPan(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //   
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth / 2, paint);
        Log.e("adress",mWidth+","+mHeight);
        canvas.drawLine(mWidth/2,mHeight/2,mWidth,mHeight,paint);
        //    
        Paint paint1 = new Paint();
        paint.setStrokeWidth(3);
        for (int i = 0; i < 24; i++) {
            if (i == 0 || i == 6 || i == 12 || i == 18) {
                paint1.setStrokeWidth(5);
                paint1.setTextSize(30);
                canvas.drawLine(mWidth / 2, mHeight / 2 - mWidth / 2, mWidth / 2, mHeight / 2 - mWidth / 2 + 60, paint1);
                String degree = String.valueOf(i);
                canvas.drawText(
                        degree,
                        mWidth / 2 - paint1.measureText(degree) / 2,
                        mHeight / 2 - mWidth / 2 + 90,
                        paint1
                );
            } else {
                paint1.setStrokeWidth(3);
                paint1.setTextSize(15);
                canvas.drawLine(mWidth / 2, mHeight / 2 - mWidth / 2, mWidth / 2, mHeight / 2 - mWidth / 2 + 30, paint1);
                String degree = String.valueOf(i);
                canvas.drawText(
                        degree,
                        mWidth / 2 - paint1.measureText(degree) / 2,
                        mHeight / 2 - mWidth / 2 + 60,
                        paint1
                );
            }
            canvas.rotate(15, mWidth / 2, mHeight / 2);
        }
        //   
        Paint paintHour = new Paint();
        paintHour.setStrokeWidth(20);
        Paint paintMinute = new Paint();
        paintMinute.setStrokeWidth(10);
        canvas.save();
        canvas.translate(mWidth/2,mHeight/2);
        canvas.drawLine(0,0,100,100,paintHour);
        canvas.drawLine(0,0,100,200,paintMinute);
        canvas.restore();
    }

좋은 웹페이지 즐겨찾기