21
2015
12

(十)hEngine—时钟

用hEngine做了一个时钟。

思路:

创建一个时钟(用HSprite),时钟内放表盘(HBitmap)、时针(HSprite)、分针(HSprite)、秒针(HSprite)。

以固定频率刷新,每次刷新时,获取系统时间,根据当前系统时间,设置时针、分针、秒针的rotation属性。

效果:

代码:

package com.example.hclock;

import java.util.Calendar;
import java.util.Date;

import com.hanyeah.HEngine;
import com.hanyeah.display.HBitmap;
import com.hanyeah.display.HSprite;
import com.hanyeah.display.HStage;
import com.hanyeah.events.HEvent;
import com.hanyeah.events.HIListener;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;

public class MainActivity extends Activity {

	private static HStage stage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        HEngine hengine=new HEngine(this);
        setContentView(hengine);
        stage=hengine.stage;
        stage.frameRate=30;
       
        stage.addEventListener(HEvent.STAGE_CREATED, new HIListener() {
			
			@Override
			public boolean execute(HEvent arg0) {
				// TODO Auto-generated method stub
				init();
				return false;
			}			
		});
    }
    private Bitmap bmp_minute_hand;
    private Bitmap bmp_secend_hand;
    private Bitmap bmp_hour_hand;
    private Bitmap bmp_pan;
    private HSprite pan;
    private HSprite hourHand;
    private HSprite minuteHand;
    private HSprite secendHand;
    private HSprite clock;
    private void init() {
		// TODO Auto-generated method stub
    	//准备位图
    	bmp_secend_hand=BitmapFactory.decodeResource(this.getResources(), R.drawable.secendhand);
    	bmp_minute_hand=BitmapFactory.decodeResource(this.getResources(), R.drawable.minutehand);
    	bmp_hour_hand=BitmapFactory.decodeResource(this.getResources(), R.drawable.hourhand);
    	bmp_pan=BitmapFactory.decodeResource(this.getResources(), R.drawable.pan);
    	
    	clock=new HSprite();
    	stage.addChild(clock);
    	clock.x=300;
    	clock.y=400;
    	
    	HBitmap bmpPan=new HBitmap(bmp_pan);
    	bmpPan.x=-550/2;
    	bmpPan.y=-550/2;
    	clock.addChild(bmpPan);
    	//秒针
    	HBitmap bmpSecendHand=new HBitmap(bmp_secend_hand);
    	secendHand=new HSprite();
    	secendHand.addChild(bmpSecendHand);
    	bmpSecendHand.x=-35;
    	bmpSecendHand.y=-20;
    	clock.addChild(secendHand);
    	
    	//分针
    	HBitmap bmpMinuteHand=new HBitmap(bmp_minute_hand);
    	minuteHand=new HSprite();
    	minuteHand.addChild(bmpMinuteHand);
    	bmpMinuteHand.x=-28;
    	bmpMinuteHand.y=-16;
    	clock.addChild(minuteHand);
    	//时针
    	HBitmap bmpHourHand=new HBitmap(bmp_hour_hand);
    	hourHand=new HSprite();
    	hourHand.addChild(bmpHourHand);
    	bmpHourHand.x=-15.5;
    	bmpHourHand.y=-10;
    	clock.addChild(hourHand);
    	
    	stage.addEventListener(HEvent.ENTER_FRAME, new HIListener() {
			
			@Override
			public boolean execute(HEvent arg0) {
				// TODO Auto-generated method stub
				enterFameHandler();
				return false;
			}
		});
	}
    private void enterFameHandler() {
		// TODO Auto-generated method stub
    	Date date=new Date();
    	float hours=date.getHours();
    	float minute=date.getMinutes();
    	float secends=date.getSeconds();
    	secendHand.rotation=secends*360/60-90;
    	minuteHand.rotation=(minute+secends/60)*360/60-90;
    	hourHand.rotation=(hours+minute/60)*360/12-90;
	}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

图片旋转之后会有锯齿,引擎中没有做消除锯齿。


源码打包下载

« 上一篇下一篇 »

相关文章:

canvas窗口自适应  (2021-6-24 9:0:7)

flash在课件开发中的优势  (2021-5-26 8:45:37)

安卓原生控件做时钟  (2017-3-3 16:48:11)

安卓利用反射调用@Hide隐藏函数  (2017-3-3 10:2:2)

安卓FileDescriptor  (2017-2-21 11:28:53)

安卓zxing实现二维码扫描  (2017-2-20 13:42:56)

DataURL与File,Blob,canvas对象之间的互相转换的Javascript  (2016-11-25 14:58:41)

安卓webview中调试js脚本  (2016-8-2 14:21:51)

安卓Camera实现3d效果  (2015-12-23 13:40:0)

(九)hEngine—画波形图  (2015-12-21 12:4:46)

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。