Android clock effect

This example shares the specific code of Android clock effect for your reference. The specific contents are as follows

Effect display:

Function introduction:

If you want to change a background image, you can click the button in the lower left corner to switch the background image. If you don't want to see the date at the top right, you can click it and it will be hidden. If you want to view it again, please click the switch wallpaper button in the lower left corner, and it will be displayed again.

Demo download address:

Click here to jump to: androidclockdemo

Partial code display:

Mainactivity section:

Realize switching, obtain events and display functions.

public class MainActivity extends AppCompatActivity {
  private int[] imageIds = new int[]{
      R.drawable.bac_1,R.drawable.bac_2,R.drawable.bac_3,R.drawable.bac_4
  };
  private int num = 1;     //num用于确定背景图
  private boolean flagI = true;//i 用于控制日期显隐
  private ImageView imageView;
  private TextView textView;
  private TextView textViewDate;
  private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");// HH:mm:ss
      //获取当前时间
      Date date = new Date(System.currentTimeMillis());
      textView.setText("" + simpleDateFormat.format(date));
      simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");// HH:mm:ss
      textViewDate.setText("" + simpleDateFormat.format(date));
    }
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    transparency();//系统状态栏透明
    textView = (TextView) findViewById(R.id.txt);
    imageView = (ImageView) findViewById(R.id.background);
    textViewDate = (TextView) findViewById(R.id.date);
    refreshTime();
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  }
  //事件刷新线程
  private void refreshTime(){
    new Thread(){//每秒更新时间
      @Override
      public void run() {
        while (true){
          Message meg = new Message();
          handler.sendMessage(meg);
          try {
            sleep(1000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }.start();
  }
  //日期显隐点击事件
  public void show(View view){
    if(flagI) {
      textViewDate.setVisibility(View.GONE);
      flagI = false;
    }//重新显现方法在背景按钮上
  }
  //悬浮按钮 更换背景
  public void change(View view){
    imageView.setImageResource(imageIds[num++]);
    num %= 4;
    textViewDate.setVisibility(View.VISIBLE);
    flagI = true;
  }
  //设置系统菜单为透明
  private void transparency(){
    if (Build.VERSION.SDK_INT >= 21) {
      View decorView = getWindow().getDecorView();
      decorView.setsystemUIVisibility(View.SYstem_UI_FLAG_LAYOUT_FULLSCREEN | View.SYstem_UI_FLAG_LAYOUT_STABLE);
      getWindow().setStatusBarColor(Color.TRANSPARENT);
    }
  }
}

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>