本篇文章给大家分享的是有关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(mWidth public 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://cdkjz.cn/article/iissee.html