十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
android中怎么自定义手表效果,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
成都创新互联于2013年创立,是专业互联网技术服务公司,拥有项目做网站、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元中山做网站,已为上家服务,为中山各地企业和个人服务,联系电话:18982081108
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中怎么自定义手表效果的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!