当前位置: 代码迷 >> Android >> (Android)画个钟见见
  详细解决方案

(Android)画个钟见见

热度:103   发布时间:2016-05-01 13:57:19.0
(Android)画个钟看看

画个钟,用Android

package net.tianyu.android.clock;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.Matrix;import android.graphics.Paint;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.widget.RelativeLayout;public class AndroidClock extends Activity {	private HourView hourView;	private Handler hourHandler;	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		RelativeLayout absLayout = new RelativeLayout(this);		hourView = new HourView(this);		hourHandler = new Handler();		hourHandler.post(updateHour);		absLayout.addView(hourView, new RelativeLayout.LayoutParams(416, 416));		setContentView(absLayout);	}	private Runnable updateHour = new Runnable() {		public void run() {			hourView.update();			hourHandler.postDelayed(updateHour, 1000);		}	};	class HourView extends View {		Matrix mHourMatrix = new Matrix();		Matrix mMinuteMatrix = new Matrix();		Bitmap mBmpHour;		Bitmap mBmpMinute;		Bitmap mBmpBackground;		int mWidth = 173;		int mHeight = mWidth;		int centerX = 145;		int centerY = 200;		public HourView(Context context) {			super(context);			mBmpHour = BitmapFactory.decodeResource(getResources(), R.drawable.hour);			mBmpMinute = BitmapFactory.decodeResource(getResources(), R.drawable.minute);			mBmpBackground = BitmapFactory.decodeResource(getResources(), R.drawable.background);		}		public void update() {			postInvalidate();		}		protected void onDraw(Canvas canvas) {			super.onDraw(canvas);			Paint p = new Paint();			p.setColor(Color.RED);			Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+9:00"));			int hour = cal.get(Calendar.HOUR);			int minute = cal.get(Calendar.MINUTE);			float hourRotate = 270.0f + hour * 30.0f + minute / 60.0f * 30.0f;			float minuteRotate = 270.0f + minute * 6.0f;			canvas.drawBitmap(mBmpBackground, 0, 0, p);			mHourMatrix.postRotate(hourRotate, 17, 7);			mMinuteMatrix.postRotate(minuteRotate, 17, 7);			mHourMatrix.postTranslate(centerX, centerY);			mMinuteMatrix.postTranslate(centerX, centerY);			canvas.drawBitmap(mBmpHour, mHourMatrix, p);			canvas.drawBitmap(mBmpMinute, mMinuteMatrix, p);			mHourMatrix.postTranslate(-1 * centerX, -1 * centerY);			mMinuteMatrix.postTranslate(-1 * centerX, -1 * centerY);			mHourMatrix.postRotate(-1.0f * hourRotate, 17, 7);			mMinuteMatrix.postRotate(-1.0f * minuteRotate, 17, 7);		}	}}
  相关解决方案