main

package com.example.demomouth;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.example.demomouth.custom.CircleView;

public class MainActivity extends AppCompatActivity {

    private TextView text_view;
    private CircleView circle_view;
    private int mCurrentProgress = 0;
    private int mTotalProgress = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        new Thread(new ProgressRunable()).start();
        //旋转
        ObjectAnimator rotationX = ObjectAnimator.ofFloat(text_view, "rotationX", new float[]{0f, -360f});
        //缩放
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(text_view, "scaleX", new float[]{0.5f, 3f, 0.5f});
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(text_view, "scaleY", new float[]{0.5f, 3f, 0.5f});
        //透明
        ObjectAnimator alpha = ObjectAnimator.ofFloat(text_view, "alpha", new float[]{0.3f,1f});

        AnimatorSet set = new AnimatorSet();
        set.play(rotationX).with(scaleX).with(scaleY).with(alpha);

        set.setDuration(10000);
        set.start();

    }

    private void initView() {
        text_view = (TextView) findViewById(R.id.text_view);
        circle_view = (CircleView) findViewById(R.id.circle_view);
    }

    private class ProgressRunable implements Runnable {
        @Override
        public void run() {
            while (mCurrentProgress < mTotalProgress) {
                mCurrentProgress += 1;
                circle_view.setProgress(mCurrentProgress);
                try {
                    Thread.sleep(200);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (mCurrentProgress == 100){
                Intent intent = new Intent(MainActivity.this,ShowActivity.class);
                startActivity(intent);
                finish();
            }
        }
    }
}

布局

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tc="http://schemas.android.com/apk/res/com.example.demomouth"
    tools:ignore="ResAuto">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="青花星空"
        android:textColor="#000"
        android:textSize="30sp"
        android:layout_centerInParent="true"/>

    <com.example.demomouth.custom.CircleView
        android:id="@+id/circle_view"
        android:layout_width="match_parent"
        android:layout_height="223dp"
        tc:circleColor="@color/white"
        tc:radius="50dip"
        tc:ringBgColor="@color/white2"
        tc:ringColor="@color/colorRed"
        tc:strokeWidth="10dip"
        android:layout_alignParentBottom="true"
        />

</RelativeLayout>

class

package com.example.demomouth.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.demomouth.R;

/**
 * Created by  on 2018/11/21.
 */

public class CircleView extends View {
    // 画实心圆的画笔
    private Paint mCirclePaint;
    // 画圆环的画笔
    private Paint mRingPaint;
    // 画圆环的画笔背景色
    private Paint mRingPaintBg;
    // 画字体的画笔
    private Paint mTextPaint;
    // 圆形颜色
    private int mCircleColor;
    // 圆环颜色
    private int mRingColor;
    // 圆环背景颜色
    private int mRingBgColor;
    // 半径
    private float mRadius;
    // 圆环半径
    private float mRingRadius;
    // 圆环宽度
    private float mStrokeWidth;
    // 圆心x坐标
    private int mXCenter;
    // 圆心y坐标
    private int mYCenter;
    // 字的长度
    private float mTxtWidth;
    // 字的高度
    private float mTxtHeight;
    // 总进度
    private int mTotalProgress = 100;
    // 当前进度
    private int mProgress;


    public CircleView(Context context) {
        this(context,null);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 获取自定义的属性
        initAttrs(context, attrs);
        initVariable();

    }
    //属性
    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.CircleView, 0, 0);
        mRadius = typeArray.getDimension(R.styleable.CircleView_radius, 80);
        mStrokeWidth = typeArray.getDimension(R.styleable.CircleView_strokeWidth, 10);
        mCircleColor = typeArray.getColor(R.styleable.CircleView_circleColor, 0xFFFFFFFF);
        mRingColor = typeArray.getColor(R.styleable.CircleView_ringColor, 0xFFFFFFFF);
        mRingBgColor = typeArray.getColor(R.styleable.CircleView_ringBgColor, 0xFFFFFFFF);

        mRingRadius = mRadius + mStrokeWidth / 2;
    }
    //初始化画笔
    private void initVariable() {
        //内圆
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(mCircleColor);
        mCirclePaint.setStyle(Paint.Style.FILL);

        //外圆弧背景
        mRingPaintBg = new Paint();
        mRingPaintBg.setAntiAlias(true);
        mRingPaintBg.setColor(mRingBgColor);
        mRingPaintBg.setStyle(Paint.Style.STROKE);
        mRingPaintBg.setStrokeWidth(mStrokeWidth);


        //外圆弧
        mRingPaint = new Paint();
        mRingPaint.setAntiAlias(true);
        mRingPaint.setColor(mRingColor);
        mRingPaint.setStyle(Paint.Style.STROKE);
        mRingPaint.setStrokeWidth(mStrokeWidth);
        //mRingPaint.setStrokeCap(Paint.Cap.ROUND);//设置线冒样式,有圆 有方

        //中间字
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setColor(mRingColor);
        mTextPaint.setTextSize(mRadius / 2);

        Paint.FontMetrics fm = mTextPaint.getFontMetrics();
        mTxtHeight = (int) Math.ceil(fm.descent - fm.ascent);
    }
    //画图
    @Override
    protected void onDraw(Canvas canvas) {
        mXCenter = getWidth() / 2;
        mYCenter = getHeight() / 2;

        //内圆
        canvas.drawCircle(mXCenter, mYCenter, mRadius, mCirclePaint);

        //外圆弧背景
        RectF oval1 = new RectF();
        oval1.left = (mXCenter - mRingRadius);
        oval1.top = (mYCenter - mRingRadius);
        oval1.right = mRingRadius * 2 + (mXCenter - mRingRadius);
        oval1.bottom = mRingRadius * 2 + (mYCenter - mRingRadius);
        canvas.drawArc(oval1, 0, 360, false, mRingPaintBg); //圆弧所在的椭圆对象、圆弧的起始角度、圆弧的角度、是否显示半径连线

        //外圆弧
        if (mProgress > 0 ) {
            RectF oval = new RectF();
            oval.left = (mXCenter - mRingRadius);
            oval.top = (mYCenter - mRingRadius);
            oval.right = mRingRadius * 2 + (mXCenter - mRingRadius);
            oval.bottom = mRingRadius * 2 + (mYCenter - mRingRadius);
            canvas.drawArc(oval, -90, ((float)mProgress / mTotalProgress) * 360, false, mRingPaint); //

            //字体
            String txt = mProgress + "分";
            mTxtWidth = mTextPaint.measureText(txt, 0, txt.length());
            canvas.drawText(txt, mXCenter - mTxtWidth / 2, mYCenter + mTxtHeight / 4, mTextPaint);
        }
    }
    //设置进度
    public void setProgress(int progress) {
        mProgress = progress;
        postInvalidate();//重绘
    }

}

绘制时钟的Demo      https://blog.csdn.net/To_be_Designer/article/details/48500801

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐