根据用户输入创建模拟时钟

时间:2014-05-03 13:54:07

标签: java android clock

我正在开发一个带有数字输入的Java / Android程序(例如,小时= 7,分钟= 30,上午7:30), 然后在模拟时钟上表示它。 (时针指向7分钟和指针指向30分钟)

如何做到这一点?我需要很多图像文件吗? 我不知道怎么开始......非常感谢你。

以下是我在“结果显示”页面上为Android编写的一些代码。

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ResultPage extends Activity {
    public static int hourMark;
    public static int hourMarkPM;
    public static int minuteMark;
    public static int time;
    public static String paddingMinute;   
    public static int age;
    public static String lifeClockDisplay;

    public static void calculation(int a){
        time = a * 18;
        minuteMark = (a * 18) % 60;
        hourMark = (time - minuteMark)/60;
        hourMarkPM = hourMark - 12;
        if (minuteMark < 10){
            paddingMinute = "0" + String.valueOf(minuteMark);
        }
        else{
            paddingMinute = String.valueOf(minuteMark);
        }
    }
    public static void resultInputCalc() 
    {   //AM
        if (hourMark >= 0 && hourMark < 12)
        {
            if (hourMark == 0)
            {
                lifeClockDisplay = "12:" + paddingMinute + "AM";
            }
            else 
            {
                lifeClockDisplay = hourMark + ":" + paddingMinute + "AM" ;
            }
        }
        //PM
        if (hourMark >= 12 && hourMark < 24)
        {
            lifeClockDisplay = hourMarkPM + ":" + paddingMinute + "PM" ;
        }
        //Midnight
        if (hourMark == 24)
        {
            lifeClockDisplay = hourMarkPM + ":" + paddingMinute + "AM" ;
        }
    }
    public static String comments() {
        if (hourMark == 0)
        {
        return "Are you a baby? Did you open this app by accident? ;) ";
        }
         // Very early morning
        else if (hourMark > 0 && hourMark < 6)
        { 
        return "Everyone else is pretty much sleeping."
                + "Utilize this time well by reading and studying! No need to be in any hurry.";
        }
         // Early morning 
        else if (hourMark >= 6 && hourMark < 9)
        {
          return "Morning has just started. It's a good time to plan out the rest of the day."
                + "You have your whole day ahead of you. No need to be in a hurry. Just wake up and do your thing.";
        }
         // Late morning
        else if (hourMark >= 9 && hourMark < 12)
        {
        return "It's still morning. You have time to plan out your afternoon and night. You have a plenty of time left. ";
        }
         // Noon
        else if (hourMark == 12)
        {
          return "It's lunch time. When you look back at the morning, what did you learn? How can you make afternoon and night better?";
        }
         // Afternoon
        else if (hourMark > 12 && hourMark <= 18) 
        {
        return "This is time that everyone else is probably working. What will you choose to work on? How will you plan out your night?";
        }
         // Night
        else if (hourMark > 18 && hourMark < 24){
        return "How do you want to spend the rest of the night? Aren't there still many things you want to do?";
        }
         // Midnight
        else {
        return "How do you want to spend the rest of the night? Aren't there still many things you want to do?";
        }    
    }


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

        TextView resultIntroView = (TextView) findViewById(R.id.result_intro_text);
        resultIntroView.setText("Your current age is " + getIntent().getExtras().getString("theAge") + " years-old and " + "your life clock is pointing to ...");

        TextView intTestView = (TextView) findViewById(R.id.clock_display_view);
        age = Integer.parseInt(getIntent().getExtras().getString("theAge")); //here is the user-input for age saved
        calculation(age);
        resultInputCalc();
        intTestView.setText(lifeClockDisplay);      
        TextView commentsView = (TextView) findViewById(R.id.result_comments);
        commentsView.setText(comments());

    }

}

1 个答案:

答案 0 :(得分:0)

这有点宽泛。你有没有尝试过任何东西?

您不需要图像文件,因为您可以使用Java的JPanel paintComponent()来处理图形。

这是我如何实施分针,从这里你可以自己计算出时针。没有代码,您可以在http://docs.oracle.com/javase/7/docs/api/index.html

找到所需的一切
  • 1小时是60分钟,因此我将找出每分钟手应该跨越的角度:spanPerMinute = 360 / 60 = 6
  • 手所跨越的度数将为minuteHandSpan = inputMinutes * spanPerMinute
  • Java图形在9点钟位置放置0°,因此我的有效图形跨度为javaMinuteHandSpan = minuteHandSpan + 90

同时适用于时针,您可以添加一些数学运算,以便它在几小时之间移动,具体取决于该小时的过去时间(例如,手在1点到2点之间,下午1:30)。