发布于 2017-10-09 20:54:08 | 17 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要为大家详细介绍了Android自定义view绘制圆环占比动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一、实现效果图

二、核心代码

1.自定义MyProgressView.java


package com.czhappy.effectdemo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.czhappy.effectdemo.R;
import com.czhappy.effectdemo.utils.ScreenUtil;


public class MyProgressView extends View {

 // 画实心圆的画笔
 private Paint mCirclePaint;
 // 画圆环的画笔
 private Paint mRingDefaultPaint;
 // 已用环的画笔
 private Paint mUsePaint;
 // 画圆环的画笔
 private Paint mRingPaint;
 // 画白线的画笔
 private Paint mLinePaint;
 // 画字体的画笔
 private Paint mTextPaint;
 // 圆形颜色
 private int mCircleColor;
 // 圆环颜色
 private int mRingColor;
 // 半径
 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;
 // 实际展示总进度
 private int mShowProgress;
 // 已用流量
 private String usedFlow;

 private Context mContext;

 private Handler circleHandler = new Handler(){

  public void handleMessage(Message msg) {
   super.handleMessage(msg);
   if(msg.what == 1){
    int temp = (Integer)msg.obj;
    setProgress(temp);
   }
  };
 };

 public MyProgressView(Context context, AttributeSet attrs) {
  super(context, attrs);
  mContext = context;
  // 获取自定义的属性
  initAttrs(context, attrs);
  initVariable();
 }

 private void initAttrs(Context context, AttributeSet attrs) {
  TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs,
    R.styleable.TasksCompletedView, 0, 0);
  mRadius = typeArray.getDimension(R.styleable.TasksCompletedView_radius, 80);
  mStrokeWidth = typeArray.getDimension(R.styleable.TasksCompletedView_strokeWidth, 10);
  mCircleColor = typeArray.getColor(R.styleable.TasksCompletedView_circleColor, 0xFFFFFFFF);
  mRingColor = typeArray.getColor(R.styleable.TasksCompletedView_ringColor, 0xFFFFFFFF);

  mRingRadius = mRadius + mStrokeWidth / 2;
 }

 private void initVariable() {
  //画圆画笔设置
  mCirclePaint = new Paint();
  mCirclePaint.setAntiAlias(true);//防锯齿
  mCirclePaint.setColor(mCircleColor);
  mCirclePaint.setStyle(Paint.Style.FILL);

  //“使用”字画笔设置
  mUsePaint = new Paint();
  mUsePaint.setAntiAlias(true);
  mUsePaint.setStyle(Paint.Style.FILL);
  mUsePaint.setColor(getResources().getColor(R.color.ticket_color));
  mUsePaint.setTextSize(ScreenUtil.sp2px(mContext, 10));

  //圆环画笔设置
  mRingDefaultPaint = new Paint();
  mRingDefaultPaint.setAntiAlias(true);
  mRingDefaultPaint.setColor(getResources().getColor(R.color.default_ring_color));
  mRingDefaultPaint.setStyle(Paint.Style.STROKE);
  mRingDefaultPaint.setStrokeWidth(mStrokeWidth);

  //已使用多少圆环画笔设置
  mRingPaint = new Paint();
  mRingPaint.setAntiAlias(true);
  mRingPaint.setColor(mRingColor);
  mRingPaint.setStyle(Paint.Style.STROKE);
  mRingPaint.setStrokeWidth(mStrokeWidth);

  mTextPaint = new Paint();
  mTextPaint.setAntiAlias(true);
  mTextPaint.setStyle(Paint.Style.FILL);
  mTextPaint.setColor(Color.BLACK);
  mTextPaint.setTextSize(ScreenUtil.sp2px(mContext, 22));

  mLinePaint = new Paint();
  mLinePaint.setColor(Color.WHITE);


  //获取字体高度
  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 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, 360, false, mRingDefaultPaint);
  //已使用多少圆弧
  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 / 9, mTextPaint);

  Rect _pb = new Rect();
  String sup = "已用";
  mUsePaint.getTextBounds(sup, 0, sup.length(), _pb);
  int perX = mXCenter - _pb.width() / 2;
  canvas.drawText(sup, perX, mYCenter / 2, mUsePaint);

  if (!TextUtils.isEmpty(usedFlow)) {
   mUsePaint.getTextBounds(usedFlow, 0, usedFlow.length(), _pb);
   perX = mXCenter - _pb.width() / 2;
   canvas.drawText(usedFlow, perX, (float) (mYCenter + mYCenter / 1.7), mUsePaint);
  }

  //画横线图片
  Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.circle_bottom_bg);
  perX = mXCenter - bitmap.getWidth() / 2;
  canvas.drawBitmap(bitmap, perX, (float) (mYCenter + mYCenter / 5), mLinePaint);
 }

 /**
  * 设置当前进度
  * @param progress
  */
 public void setProgress(int progress) {
  mProgress = progress;
  postInvalidate();
 }

 /**
  * 实际展示总进度
  * @param progress
  */
 public void setmShowProgress(int progress) {
  mShowProgress = progress;
  new Thread(new CircleThread()).start();
 }

 public void setUsedFlow(String usedFlow) {
  this.usedFlow = usedFlow;
 }


 private class CircleThread implements Runnable{

  int m=0;
  int i=0;

  @Override
  public void run() {
   // TODO Auto-generated method stub
   while(!Thread.currentThread().isInterrupted()){
    try {
     Thread.sleep(50);
     m++;
     Message msg = new Message();
     msg.what = 1;
     if(i < mShowProgress){
      i += m;
     }else{
      i = mShowProgress;
      return;
     }
     msg.obj = i;
     circleHandler.sendMessage(msg);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }

 }
}

2.FlowActivity.java


package com.czhappy.effectdemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.czhappy.effectdemo.R;
import com.czhappy.effectdemo.view.MyProgressView;

/**
 * Description:
 * User: chenzheng
 * Date: 2017/1/21 0021
 * Time: 17:17
 */
public class FlowActivity extends AppCompatActivity {

 private MyProgressView mTasksView;

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

  initView();
 }

 private void initView() {
  mTasksView = (MyProgressView) findViewById(R.id.flow_prgress_view);
 }

 public void beginAnim(View view){
  mTasksView.setUsedFlow("200.0M");
  mTasksView.setmShowProgress(60);
 }
}

3.activity_flow.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tc="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:padding="10dp"
 android:background="#fff"
 android:gravity="center_horizontal"
 android:orientation="vertical">

 <Button
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:onClick="beginAnim"
  android:text="开始动画"/>

 <com.czhappy.effectdemo.view.MyProgressView
  android:id="@+id/flow_prgress_view"
  android:layout_width="100dp"
  android:layout_height="100dp"
  tc:circleColor="@color/circle_color"
  tc:radius="44dp"
  tc:ringColor="@color/ring_color"
  tc:strokeWidth="6dp"
  android:layout_marginTop="10dp"/>

</LinearLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。



相关阅读 :
Android自定义view绘制圆环占比动画
Android自定义View实现圆环交替效果
Android中自定义View实现圆环等待及相关的音量调节效果
自定义滑动按钮为例图文剖析Android自定义View绘制
Android使用自定义View绘制渐隐渐现动画
Android自定义View绘制随机生成图片验证码
Android自定义View仿微博运动积分动画效果
Android自定义View之酷炫圆环(二)
Android自定义View之酷炫数字圆环
Android自定义view圆并随手指移动
Android自定义View实现shape图形绘制
Android自定义View系列之Path绘制仿支付宝支付成功动画
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务