安卓编程-绘制模拟时钟[英] Android programming - drawing analog clock

本文是小编为大家收集整理的关于安卓编程-绘制模拟时钟的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我是Android编程的新手.如何制作一个程序,该程序将绘制模拟时钟,使其可以显示各种时区的时间,因为用户选择?

我已经在过去几个小时里谷歌入了,但没有找到这个问题的任何特定解决方案.我完全困惑了我.请帮助我.

推荐答案

嗨朋友,我已经解决了我的问题...... 我希望用u guys分享解决方案...

public class TimeShow extends Activity implements RadioGroup.OnCheckedChangeListener{
Context context;
TextView tv;
RadioButton rb1,rb2,rb3;
RadioGroup r1;
FrameLayout fl1;
LinearLayout l1;
Timer timer;
Date result;
ViewGroup.LayoutParams lp1;
Drawable bck;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    context=this;
    tv = (TextView)findViewById(R.id.TextView01);
    r1=(RadioGroup)findViewById(R.id.RadioGroup01);
    rb1=(RadioButton)findViewById(R.id.RadioButton01);
    rb2=(RadioButton)findViewById(R.id.RadioButton02);
    rb3=(RadioButton)findViewById(R.id.RadioButton03);
    fl1=(FrameLayout)findViewById(R.id.FrameLayout01);
    bck=this.getResources().getDrawable(R.drawable.clockface);
    fl1.setBackgroundDrawable(bck);
    lp1=fl1.getLayoutParams();
    r1.setOnCheckedChangeListener(this);
    result=null;
}

next是一种称为UpdateTime的方法,如下所示:

private void updateTime(String str) {
       // TODO Auto-generated method stub
       if(timer!=null)
       {
           timer.cancel();
           timer=null;
       }
       timer = new Timer();
       MyTime mt=new MyTime(this,str);
       timer.schedule(mt, 1, 1000);
    }

public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        if(rb1.isChecked() == true)
            updateTime("Asia/Kolkata");
        if(rb2.isChecked() == true)
            updateTime("Etc/GMT-6");
        if(rb3.isChecked() == true)
            updateTime("Asia/Karachi");
    }
    public class MyTime extends TimerTask {
        String tz;
        public MyTime(Context context,String str) {
            tz=str;
        }
        @Override
        public void run() {
            // TODO Auto-generated method stub
            try{
                Date date=new Date();
                date=getDateInTimeZone(date, tz);
                //System.out.println(date.toLocaleString());
                result=date;
                handler.sendEmptyMessage(0);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        private Date getDateInTimeZone(Date currentDate, String timeZoneId) {
            TimeZone tz = TimeZone.getTimeZone(timeZoneId);
            Calendar mbCal = new GregorianCalendar(tz);
            mbCal.setTimeInMillis(currentDate.getTime());
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, mbCal.get(Calendar.YEAR));
            cal.set(Calendar.MONTH, mbCal.get(Calendar.MONTH));
            cal.set(Calendar.DAY_OF_MONTH, mbCal.get(Calendar.DAY_OF_MONTH));
            cal.set(Calendar.HOUR_OF_DAY, mbCal.get(Calendar.HOUR_OF_DAY));
            cal.set(Calendar.MINUTE, mbCal.get(Calendar.MINUTE));
            cal.set(Calendar.SECOND, mbCal.get(Calendar.SECOND));
            cal.set(Calendar.MILLISECOND, mbCal.get(Calendar.MILLISECOND));


            return cal.getTime();
        }
    }
    Handler handler=new Handler(){
        public void handleMessage(Message msg) {
            tv.setText(result.toLocaleString());
            //System.out.println(result.getHours()+" "+result.getMinutes());
            fl1.removeAllViews();
            fl1.addView(new CustomClock(context, lp1.height/2, lp1.width/2, result));
        }
    };
}

和扩展视图的类CustomClock:

public class CustomClock extends View {
    private final float x;
    private final float y;
    private final int r=45;
    private final Date date;
    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public CustomClock(Context context, float x, float y, Date date) {
        super(context);
        this.x = x;
        this.y = y;
        this.date=date;        
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //canvas.drawCircle(x, y, r, mPaint);
        float sec=(float)date.getSeconds();
        float min=(float)date.getMinutes();
        float hour=(float)date.getHours()+min/60.0f;
        mPaint.setColor(0xFFFF0000);
        canvas.drawLine(x, y, (float)(x+(r-15)*Math.cos(Math.toRadians((hour / 12.0f * 360.0f)-90f))), (float)(y+(r-10)*Math.sin(Math.toRadians((hour / 12.0f * 360.0f)-90f))), mPaint);
        canvas.save();
        mPaint.setColor(0xFF0000FF);
        canvas.drawLine(x, y, (float)(x+r*Math.cos(Math.toRadians((min / 60.0f * 360.0f)-90f))), (float)(y+r*Math.sin(Math.toRadians((min / 60.0f * 360.0f)-90f))), mPaint);
        canvas.save();
        mPaint.setColor(0xFFA2BC13);
        canvas.drawLine(x, y, (float)(x+(r+10)*Math.cos(Math.toRadians((sec / 60.0f * 360.0f)-90f))), (float)(y+(r+15)*Math.sin(Math.toRadians((sec / 60.0f * 360.0f)-90f))), mPaint);
    }
}

谢谢所有朋友的帮助和建议....

其他推荐答案

souvik,

您可以从Android系统中提供的"AnalAgclock"控件,拖放在屏幕上拖放AnalogClock控件,或者在main.xml中写一个新的标签施造卷.

本文地址:https://www.itbaoku.cn/post/102725.html

问题描述

I am a novice at Android programming. How can I make a program that will draw an analog clock such that it can show time for various timezones as the user selects?

I had already Googled for the last few hours, but didn't find any particular solution to this problem. What I got completely puzzled me out. Please help me.

推荐答案

Hi Friends I have did the solution to my problem...... I wish to share the solution with u guys...

public class TimeShow extends Activity implements RadioGroup.OnCheckedChangeListener{
Context context;
TextView tv;
RadioButton rb1,rb2,rb3;
RadioGroup r1;
FrameLayout fl1;
LinearLayout l1;
Timer timer;
Date result;
ViewGroup.LayoutParams lp1;
Drawable bck;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    context=this;
    tv = (TextView)findViewById(R.id.TextView01);
    r1=(RadioGroup)findViewById(R.id.RadioGroup01);
    rb1=(RadioButton)findViewById(R.id.RadioButton01);
    rb2=(RadioButton)findViewById(R.id.RadioButton02);
    rb3=(RadioButton)findViewById(R.id.RadioButton03);
    fl1=(FrameLayout)findViewById(R.id.FrameLayout01);
    bck=this.getResources().getDrawable(R.drawable.clockface);
    fl1.setBackgroundDrawable(bck);
    lp1=fl1.getLayoutParams();
    r1.setOnCheckedChangeListener(this);
    result=null;
}

Next is a method called updateTime as follows:

private void updateTime(String str) {
       // TODO Auto-generated method stub
       if(timer!=null)
       {
           timer.cancel();
           timer=null;
       }
       timer = new Timer();
       MyTime mt=new MyTime(this,str);
       timer.schedule(mt, 1, 1000);
    }

public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        if(rb1.isChecked() == true)
            updateTime("Asia/Kolkata");
        if(rb2.isChecked() == true)
            updateTime("Etc/GMT-6");
        if(rb3.isChecked() == true)
            updateTime("Asia/Karachi");
    }
    public class MyTime extends TimerTask {
        String tz;
        public MyTime(Context context,String str) {
            tz=str;
        }
        @Override
        public void run() {
            // TODO Auto-generated method stub
            try{
                Date date=new Date();
                date=getDateInTimeZone(date, tz);
                //System.out.println(date.toLocaleString());
                result=date;
                handler.sendEmptyMessage(0);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        private Date getDateInTimeZone(Date currentDate, String timeZoneId) {
            TimeZone tz = TimeZone.getTimeZone(timeZoneId);
            Calendar mbCal = new GregorianCalendar(tz);
            mbCal.setTimeInMillis(currentDate.getTime());
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, mbCal.get(Calendar.YEAR));
            cal.set(Calendar.MONTH, mbCal.get(Calendar.MONTH));
            cal.set(Calendar.DAY_OF_MONTH, mbCal.get(Calendar.DAY_OF_MONTH));
            cal.set(Calendar.HOUR_OF_DAY, mbCal.get(Calendar.HOUR_OF_DAY));
            cal.set(Calendar.MINUTE, mbCal.get(Calendar.MINUTE));
            cal.set(Calendar.SECOND, mbCal.get(Calendar.SECOND));
            cal.set(Calendar.MILLISECOND, mbCal.get(Calendar.MILLISECOND));


            return cal.getTime();
        }
    }
    Handler handler=new Handler(){
        public void handleMessage(Message msg) {
            tv.setText(result.toLocaleString());
            //System.out.println(result.getHours()+" "+result.getMinutes());
            fl1.removeAllViews();
            fl1.addView(new CustomClock(context, lp1.height/2, lp1.width/2, result));
        }
    };
}

And a class CustomClock that extends view:

public class CustomClock extends View {
    private final float x;
    private final float y;
    private final int r=45;
    private final Date date;
    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public CustomClock(Context context, float x, float y, Date date) {
        super(context);
        this.x = x;
        this.y = y;
        this.date=date;        
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //canvas.drawCircle(x, y, r, mPaint);
        float sec=(float)date.getSeconds();
        float min=(float)date.getMinutes();
        float hour=(float)date.getHours()+min/60.0f;
        mPaint.setColor(0xFFFF0000);
        canvas.drawLine(x, y, (float)(x+(r-15)*Math.cos(Math.toRadians((hour / 12.0f * 360.0f)-90f))), (float)(y+(r-10)*Math.sin(Math.toRadians((hour / 12.0f * 360.0f)-90f))), mPaint);
        canvas.save();
        mPaint.setColor(0xFF0000FF);
        canvas.drawLine(x, y, (float)(x+r*Math.cos(Math.toRadians((min / 60.0f * 360.0f)-90f))), (float)(y+r*Math.sin(Math.toRadians((min / 60.0f * 360.0f)-90f))), mPaint);
        canvas.save();
        mPaint.setColor(0xFFA2BC13);
        canvas.drawLine(x, y, (float)(x+(r+10)*Math.cos(Math.toRadians((sec / 60.0f * 360.0f)-90f))), (float)(y+(r+15)*Math.sin(Math.toRadians((sec / 60.0f * 360.0f)-90f))), mPaint);
    }
}

Thanks All of my Friends for your kind help and suggestions....

其他推荐答案

Souvik,

You can start with "AnalagClock" control available in the Android system, drag and drop the AnalogClock control on screen or write a new tag manuaaly in main.xml.