用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; } }
图片旋转之后会有锯齿,引擎中没有做消除锯齿。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。