这篇文章将为大家详细讲解有关怎么在Android中自定义控件实现时钟效果,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、微信小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了睢阳免费建站欢迎大家使用!
自定义控件的代码:
public class ClockView extends View{ private Paint circlePaint,dialPaint,numberPaint; //view 的宽高 private float mWidth,mHeight; //圆的半径 private float circleRadius; //圆心X,Y坐标 private float circleX,circleY; private int second,minute; private double hour; private Handler handler = new Handler(Looper.getMainLooper()){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what==0){ invalidate(); } } }; public ClockView(Context context, AttributeSet attrs) { super(context, attrs); initPaint(); } private void initPaint(){ //刻盘圆,小时刻度,时针和分针的画笔 circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG); circlePaint.setColor(Color.BLACK); circlePaint.setStyle(Paint.Style.STROKE); circlePaint.setStrokeWidth(10); //分钟刻度的画笔 dialPaint = new Paint(Paint.ANTI_ALIAS_FLAG); dialPaint.setColor(Color.BLACK); dialPaint.setStrokeWidth(5); //数字的画笔 numberPaint = new Paint(Paint.ANTI_ALIAS_FLAG); numberPaint.setColor(Color.BLACK); numberPaint.setStrokeWidth(5); numberPaint.setTextSize(30); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mWidth = getMeasuredWidth(); mHeight = getMeasuredHeight(); if(mWidthpublic class Point {
private float x;
private float y;public Point(float x, float y) { this.x = x; this.y = y; } public float getX() { return x; } public void setX(float x) { this.x = x; } public float getY() { return y; } public void setY(float y) { this.y = y; }Acitivity:
public class ClockActivity extends Activity{ private ClockView clockView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.clock_layout); clockView = (ClockView) findViewById(R.id.clock); } @Override protected void onResume() { super.onResume(); clockView.startClock(); } @Override protected void onStop() { super.onStop(); clockView.stopClock(); } }xml布局:
关于怎么在Android中自定义控件实现时钟效果就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
标题名称:怎么在Android中自定义控件实现时钟效果
文章分享:http://cdweb.net/article/igseoe.html