如何在android中自定义一个手表效果
如何在android中自定义一个手表效果?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
10年积累的成都网站建设、成都网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有北流免费网站建设让你可以放心的选择与我们合作。
.布局
.
3.自定义view,显示
package com.example.administrator.testz; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.util.Calendar; /** * 优化方案: * 表盘课绘制一次 * 在分线程中进行加载 */ public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btnStart, btnStop; private ImageView mClockImageView; Bitmap.Config config = Bitmap.Config.ARGB_8888; int width = 500; int height = 500; private Calendar mCalendar; private int mHour, mMinute, mSecond; private float mDegrees; private float length; private boolean mIsRunning; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnStart = (Button) findViewById(R.id.btn_start); btnStop = (Button) findViewById(R.id.btn_stop); btnStop.setOnClickListener(this); btnStart.setOnClickListener(this); mClockImageView = (ImageView) findViewById(R.id.iv_clock); mClockImageView.setImageBitmap(drawClock()); } /** * 画表盘 */ private Bitmap drawClockFace() { Bitmap bm = Bitmap.createBitmap(width, height, config); Canvas canvas = new Canvas(bm); Paint paint = new Paint(); paint.setAntiAlias(true); //锯齿 paint.setStyle(Paint.Style.STROKE); // 空心 paint.setStrokeWidth(5); paint.setColor(Color.parseColor("#333333")); // 外层圆 canvas.drawCircle(width / 2, height / 2, width / 2, paint); // 內层圆 --》圆心 paint.setStyle(Paint.Style.FILL); canvas.drawCircle(width / 2, height / 2, 10, paint); // 循环画刻度(旋转画刻度) for (int i = 0; i < 12; i++) { if (i % 3 == 0) { paint.setStrokeWidth(10); canvas.drawLine(width / 2, 0, width / 2, 24, paint); canvas.rotate(30, width / 2, height / 2); } else { canvas.drawLine(width / 2, 0, width / 2, 10, paint); canvas.rotate(30, width / 2, height / 2); } } return bm; } private Bitmap drawClock() { Bitmap bm = drawClockFace(); Canvas canvas = new Canvas(bm); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.parseColor("#333333")); mCalendar = Calendar.getInstance(); mHour = mCalendar.get(Calendar.HOUR); mMinute = mCalendar.get(Calendar.MINUTE); mSecond = mCalendar.get(Calendar.SECOND); //画小时指针 paint.setStrokeWidth(10); mDegrees = mHour * 30 + mMinute / 2; length = (width / 2) * 0.7f; canvas.save(); canvas.rotate(mDegrees, width / 2, height / 2); canvas.drawLine(width / 2, height / 2, width / 2, height - (height / 2) - length, paint); canvas.restore(); // canvas.rotate(360 - mDegrees, width / 2, height / 2); //画分钟指针 paint.setStrokeWidth(4); mDegrees = mMinute * 6 + mSecond / 10; length = (width / 2) * 0.78f; canvas.save(); canvas.rotate(mDegrees, width / 2, height / 2); canvas.drawLine(width / 2, height / 2, width / 2, height - (height / 2) - length, paint); canvas.restore(); // canvas.rotate(360 - mDegrees, width / 2, height / 2); //画分钟指针 paint.setStrokeWidth(2); mDegrees = mSecond * 6; length = (width / 2) * 0.92f; canvas.save(); canvas.rotate(mDegrees, width / 2, height / 2); canvas.drawLine(width / 2, height / 2, width / 2, height - (height / 2) - length, paint); canvas.restore(); return bm; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_start: mIsRunning = true; new ClockTask().execute(""); break; case R.id.btn_stop: mIsRunning = false; break; } } @Override protected void onDestroy() { super.onDestroy(); mIsRunning = false; } private class ClockTask extends AsyncTask
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
网页题目:如何在android中自定义一个手表效果
URL分享:http://scyanting.com/article/gejddp.html