مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

mohsenkhan

عضویت از 1397/02/12

مشکل در نمایش سورس ساعت در اندرویداستدیو

  • یکشنبه 31 تیر 1397
  • 23:10
تشکر میکنم

سلام

من کد برای نمایش یک ساعت در یک برنامه  رو در اندرویداستدیو نوشتم ولی موقع نمایش در layout  به صورت عکس ضمیمه شده نشان داده میشه

مشکل کجاست؟؟؟؟؟؟؟

اینم از کد ها:

 

package ir.dorsa.dorsaworld;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.icu.util.Calendar;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * Created by Cezar on 7/22/2018.
 */

public class Activity_Clock extends View {

    private int height, width = 0;
    private int padding = 0;
    private int fontSize = 0;
    private int numeralSpacing = 0;
    private int handTruncation, hourhandTruncation = 0;
    private int radius = 0;
    private Paint paint;
    private boolean isInit;
    private int[] numbers ={1,2,3,4,5,6,7,8,9,10,11,12};
    private Rect rect = new Rect();



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

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

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

    private void initClock(){
        width = getWidth();
        height = getHeight();
        padding = numeralSpacing + 50;
        fontSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 13, getResources().getDisplayMetrics());
        int min = Math.min(height, width);
        radius = min / 2 - padding;
        handTruncation = min / 20;
        paint = new Paint();
        isInit = true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (!isInit){
            initClock();
        }
        canvas.drawColor(Color.BLACK);
        drawCircle(canvas);
        drawCenter(canvas);
        drawNumeral(canvas);
        drawHands(canvas);

        postInvalidateDelayed(500);
        invalidate();
    }
    private void drawHand(Canvas canvas, double loc, boolean isHour){
        double angle = Math.PI * loc / 30 * Math.PI / 2;
        int handRadius = isHour ? radius - handTruncation - hourhandTruncation : radius - handTruncation;
        canvas.drawLine(width / 2, height / 2, (float) (width / 2 + Math.cos(angle) * handRadius), (float) (height / 2 + Math.sin(angle) * handRadius),paint);

    }

    private void drawHands(Canvas canvas) {

        Calendar c  = Calendar.getInstance();
        float hour = c.get(Calendar.HOUR_OF_DAY);
        hour = hour > 12 ? hour - 12 : hour;
        drawHand(canvas, (hour + c.get(Calendar.MINUTE) / 60) * 5f, true);
        drawHand(canvas, c.get(Calendar.MINUTE), false);
        drawHand(canvas, c.get(Calendar.SECOND), false);


    }

    private void drawNumeral(Canvas canvas) {
        paint.setTextSize(fontSize);

        for (int number : numbers){
            String tmp = String.valueOf(number);
            paint.getTextBounds(tmp, 0,tmp.length(), rect);
            double angle = Math.PI / 6 * (number - 3);
            int x = (int) (width / 2 + Math.cos(angle) * radius - rect.width() / 2);
            int y = (int) (height / 2 + Math.sin(angle) * radius + rect.height() / 2);
            canvas.drawText(tmp, x, y, paint);
            
        }
    }

    private void drawCenter(Canvas canvas) {
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(width / 2, height / 2, 12,paint);

    }

    private void drawCircle(Canvas canvas) {
        paint.reset();
        paint.setColor(getResources().getColor(android.R.color.white));
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        canvas.drawCircle(width / 2, height / 2 ,radius + padding - 10,paint);

    }

}

پاسخ های این پرسش

تعداد پاسخ ها : 1 پاسخ
کاربر سایت

نرجس اسماعیلی

عضویت از 1393/01/20

  • دوشنبه 1 مرداد 1397
  • 10:35

سلام عکسی ضمیمه نشده لطفا یه بار دیگه ضمیمه کنید

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)