canvas教程

Android时钟

字号+ 作者:H5之家 来源:H5之家 2015-10-30 19:22 我要评论( )

Android时钟

1. Android时钟效果图:

Android 时钟效果图

2. 时钟所需资源:


(1)时钟表盘:

Android 时钟表盘


(2)时钟时针:


(3)时钟分针和秒针:

3. 代码实现:

package com.android.clockview; import java.util.Calendar; import java.util.TimeZone; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; public class ClockView extends Activity { private final int FP = ViewGroup.LayoutParams.FILL_PARENT; private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; private QAnalogClock clock1; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout lLayout = new LinearLayout(this); lLayout.setLayoutParams(new LinearLayout.LayoutParams(FP, FP)); lLayout.setOrientation(LinearLayout.VERTICAL); clock1 = new QAnalogClock(this, "GMT+8:00"); lLayout.addView(clock1, new LinearLayout.LayoutParams(WC, WC)); setContentView(lLayout); } class QAnalogClock extends View { Bitmap mBmpDial; Bitmap mBmpHour; Bitmap mBmpMinute; Bitmap mBmpSecond; BitmapDrawable bmdHour; BitmapDrawable bmdMinute; BitmapDrawable bmdSecond; BitmapDrawable bmdDial; Paint mPaint; Handler tickHandler; int mWidth; int mHeigh; int mTempWidth; int mTempHeigh; int centerX; int centerY; int availableWidth = 100; int availableHeight = 100; private String sTimeZoneString; public QAnalogClock(Context context, String sTime_Zone) { super(context); sTimeZoneString = sTime_Zone; mBmpHour = BitmapFactory.decodeResource(getResources(), R.drawable.android_clock_hour); bmdHour = new BitmapDrawable(mBmpHour); mBmpMinute = BitmapFactory.decodeResource(getResources(), R.drawable.android_clock_minute); bmdMinute = new BitmapDrawable(mBmpMinute); mBmpSecond = BitmapFactory.decodeResource(getResources(), R.drawable.android_clock_minute); bmdSecond = new BitmapDrawable(mBmpSecond); mBmpDial = BitmapFactory.decodeResource(getResources(), R.drawable.android_clock_dial); bmdDial = new BitmapDrawable(mBmpDial); mWidth = mBmpDial.getWidth(); mHeigh = mBmpDial.getHeight(); centerX = availableWidth / 2; centerY = availableHeight / 2; mPaint = new Paint(); mPaint.setColor(Color.BLUE); run(); } public void run() { tickHandler = new Handler(); tickHandler.post(tickRunnable); } private Runnable tickRunnable = new Runnable() { public void run() { postInvalidate(); tickHandler.postDelayed(tickRunnable, 1000); } }; protected void onDraw(Canvas canvas) { super.onDraw(canvas); Calendar cal = Calendar.getInstance(TimeZone .getTimeZone(sTimeZoneString)); int hour = cal.get(Calendar.HOUR); int minute = cal.get(Calendar.MINUTE); int second = cal.get(Calendar.SECOND); float hourRotate = hour * 30.0f + minute / 60.0f * 30.0f; float minuteRotate = minute * 6.0f; float secondRotate = second * 6.0f; boolean scaled = false; if (availableWidth < mwidth="" ||="" availableheight="">< mheigh)="" {="" scaled="true;" float="" scale="Math.min((float)" availablewidth="" (float)="" mwidth,="" (float)="" availableheight="" (float)="" mheigh);="" canvas.save();="" canvas.scale(scale,="" scale,="" centerx,="" centery);="" }="" bmddial.setbounds(centerx="" -="" (mwidth="" 2),="" centery="" -="" (mheigh="" 2),="" centerx="" +="" (mwidth="" 2),="" centery="" +="" (mheigh="" 2));="" bmddial.draw(canvas);="" mtempwidth="bmdHour.getIntrinsicWidth();" mtempheigh="bmdHour.getIntrinsicHeight();" canvas.save();="" canvas.rotate(hourrotate,="" centerx,="" centery);="" bmdhour.setbounds(centerx="" -="" (mtempwidth="" 2),="" centery="" -="" (mtempheigh="" 2),="" centerx="" +="" (mtempwidth="" 2),="" centery="" +="" (mtempheigh="" 2));="" bmdhour.draw(canvas);="" canvas.restore();="" mtempwidth="bmdMinute.getIntrinsicWidth();" mtempheigh="bmdMinute.getIntrinsicHeight();" canvas.save();="" canvas.rotate(minuterotate,="" centerx,="" centery);="" bmdminute.setbounds(centerx="" -="" (mtempwidth="" 2),="" centery="" -="" (mtempheigh="" 2),="" centerx="" +="" (mtempwidth="" 2),="" centery="" +="" (mtempheigh="" 2));="" bmdminute.draw(canvas);="" canvas.restore();="" mtempwidth="bmdSecond.getIntrinsicWidth();" mtempheigh="bmdSecond.getIntrinsicHeight();" canvas.rotate(secondrotate,="" centerx,="" centery);="" bmdsecond.setbounds(centerx="" -="" (mtempwidth="" 2),="" centery="" -="" (mtempheigh="" 2),="" centerx="" +="" (mtempwidth="" 2),="" centery="" +="" (mtempheigh="" 2));="" bmdsecond.draw(canvas);="" if="" (scaled)="" {="" canvas.restore();="" }="" }="" }="" }="">

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Android中View的实时刷新

    Android中View的实时刷新

    2016-01-17 10:48

  • 《Android提高篇》2.SurfaceView的基本使用

    《Android提高篇》2.SurfaceView的基本使用

    2015-11-11 11:49

  • Android画图PATH的使用

    Android画图PATH的使用

    2015-10-08 17:16

网友点评