怎么在Android应用中添加一个倒计时功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
SweepView.java:
public class SweepView extends View { private static final int DEFAULT_WIDTH = 100; private static final int DEFAULT_HEIGHT = 100; private int mWidth; //这里并没卵用 private int mHeight; //这个也没卵用 private RectF rectF; private Paint paint; private int mColor = Color.RED;//默认颜色为红色 private float mSweep = 0; //扇形角度 public SweepView(Context context) { super(context); init(); } public SweepView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public SweepView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { paint = new Paint(); paint.setColor(mColor); //画笔颜色 paint.setStyle(Paint.Style.FILL); //填充 paint.setAntiAlias(true); //是否抗锯齿 } /** * 设置扇形颜色 * UIThred */ public void setColor(int color) { this.mColor = color; paint.setColor(mColor); //调用onDraw重绘 invalidate(); } /** * 设置扇形的区域0-360 * UIThred */ public void setSweep(float mSweep) { this.mSweep = mSweep; //调用onDraw重绘 invalidate(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int resultWidth = measureWidth(widthMeasureSpec); int resultHeight = measureHeight(heightMeasureSpec); setMeasuredDimension(resultWidth, resultHeight); } /** * 绘制的宽 * 其实里面的内容我不懂,好惭愧...我以后会弄懂的.....0.0 */ private int measureWidth(int widthMeasureSpec) { int size = MeasureSpec.getSize(widthMeasureSpec); int mode = MeasureSpec.getMode(widthMeasureSpec); int result; if (mode == MeasureSpec.EXACTLY) { result = size; } else { result = DEFAULT_WIDTH; if (mode == MeasureSpec.AT_MOST) { result = Math.min(size, DEFAULT_WIDTH); } } return result; } /** * 绘制的高 * 这里面的内容我也不懂,好惭愧...我以后会弄懂的.....0.0 */ private int measureHeight(int heightMeasureSpec) { int size = MeasureSpec.getSize(heightMeasureSpec); int mode = MeasureSpec.getMode(heightMeasureSpec); int result; if (mode == MeasureSpec.EXACTLY) { result = size; } else { result = DEFAULT_HEIGHT; if (mode == MeasureSpec.AT_MOST) { result = Math.min(size, DEFAULT_HEIGHT); } } return result; } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { this.mHeight = h; this.mWidth = w; rectF = new RectF(0, 0, w, h); super.onSizeChanged(w, h, oldw, oldh); } @Override protected void onDraw(Canvas canvas) { //画扇形 canvas.drawArc(rectF, -90, mSweep, true, paint); } }
写好自定义的View,显然我们要用它,所以布局文件中声明:(不过有一点要注意的是,如果想要控制它的位置及大小,这里要用ViewGroup来包裹,通过设置ViewGroup的位置及大小来控制它,至于为什么,我也很想知道0.0)
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.meijun.myapplication.MainActivity"> <RelativeLayout android:layout_centerInParent="true" android:layout_width="20dp" android:background="#00f" android:layout_height="20dp"> <com.example.meijun.myapplication.SweepView android:layout_width="wrap_content" android:id="@+id/sweepView" android:layout_height="wrap_content" /> </RelativeLayout> </RelativeLayout>
最后就是在代码里来绘制动画形态的,圆形扇形倒计时了:
MainActivity.java:
public class MainActivity extends AppCompatActivity { private SweepView sweepView; float angle = 0;//绘制的角度 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sweepView = (SweepView) findViewById(R.id.sweepView); sweepView.setColor(Color.WHITE); //设置画笔颜色 sweepView.setSweep(0); //初始绘制0度 new Thread(new Runnable() { @Override public void run() { while (angle <= 360) { //这里相当于绘制一个完整的圆,结合下面的3.6及50,也就是5秒钟的倒计时 angle += 3.6; runOnUiThread(new Runnable() { @Override public void run() { sweepView.setSweep(angle); } }); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } }
关于怎么在Android应用中添加一个倒计时功能问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。