欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义加载loading view动画组件

程序员文章站 2024-03-07 19:40:45
在github上找的一个有点酷炫的loading动画https://github.com/fichardu/circleprogress  我写写使用步骤&...

在github上找的一个有点酷炫的loading动画https://github.com/fichardu/circleprogress 

我写写使用步骤 

自定义view(circleprogress )的代码 

package com.hysmarthotel.view;

import com.hysmarthotel.roomcontrol.r;
import com.hysmarthotel.util.easeinoutcubicinterpolator;

import android.animation.timeinterpolator;
import android.content.context;
import android.content.res.typedarray;
import android.graphics.canvas;
import android.graphics.paint;
import android.graphics.point;
import android.util.attributeset;
import android.view.view;
import android.view.animation.animationutils;

public class circleprogress extends view {

 private static final int red = 0xffe5282c;
 private static final int yellow = 0xff1f909a;
 private static final int blue = 0xfffc9e12;
 private static final int color_num = 3;
 private int[] colors;
 private timeinterpolator minterpolator = new easeinoutcubicinterpolator();

 private final double degree = math.pi / 180;
 private paint mpaint;
 private int mviewsize;
 private int mpointradius;
 private long mstarttime;
 private long mplaytime;
 private boolean mstartanim = false;
 private point mcenter = new point();

 private arcpoint[] marcpoint;
 private static final int point_num = 15;
 private static final int delta_angle = 360 / point_num;
 private long mduration = 3600;

 public circleprogress(context context) {
  super(context);
  init(null, 0);
 }

 public circleprogress(context context, attributeset attrs) {
  super(context, attrs);
  init(attrs, 0);
 }

 public circleprogress(context context, attributeset attrs, int defstyle) {
  super(context, attrs, defstyle);
  init(attrs, defstyle);
 }

 private void init(attributeset attrs, int defstyle) {
  marcpoint = new arcpoint[point_num];

  mpaint = new paint();
  mpaint.setantialias(true);
  mpaint.setstyle(paint.style.fill);

  typedarray a = getcontext().obtainstyledattributes(attrs, r.styleable.circleprogress, defstyle, 0);
  int color1 = a.getcolor(r.styleable.circleprogress_color1, red);
  int color2 = a.getcolor(r.styleable.circleprogress_color2, yellow);
  int color3 = a.getcolor(r.styleable.circleprogress_color3, blue);
  a.recycle();

  colors = new int[]{color1, color2, color3};
 }

 @override
 protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
  int defaultsize = getresources().getdimensionpixelsize(r.dimen.default_circle_view_size);
  int width = getdefaultsize(defaultsize, widthmeasurespec);
  int height = getdefaultsize(defaultsize, heightmeasurespec);
  mviewsize = math.min(width, height);
  setmeasureddimension(mviewsize, mviewsize);
  mcenter.set(mviewsize / 2, mviewsize / 2);

  calpoints(1.0f);
 }

 @override
 protected void ondraw(canvas canvas) {
  canvas.save();
  canvas.translate(mcenter.x, mcenter.y);

  float factor = getfactor();
  canvas.rotate(36 * factor);
  float x, y;
  for (int i = 0; i < point_num; ++i) {
   mpaint.setcolor(marcpoint[i].color);
   float itemfactor = getitemfactor(i, factor);
   x = marcpoint[i].x - 2 * marcpoint[i].x * itemfactor;
   y = marcpoint[i].y - 2 * marcpoint[i].y * itemfactor;
   canvas.drawcircle(x, y, mpointradius, mpaint);
  }

  canvas.restore();

  if (mstartanim) {
   postinvalidate();
  }
 }

 private void calpoints(float factor) {
  int radius = (int) (mviewsize / 3 * factor);
  mpointradius = radius / 12;

  for (int i = 0; i < point_num; ++i) {
   float x = radius * -(float) math.sin(degree * delta_angle * i);
   float y = radius * -(float) math.cos(degree * delta_angle * i);

   arcpoint point = new arcpoint(x, y, colors[i % color_num]);
   marcpoint[i] = point;
  }
 }


 private float getfactor() {
  if (mstartanim) {
   mplaytime = animationutils.currentanimationtimemillis() - mstarttime;
  }
  float factor = mplaytime / (float) mduration;
  return factor % 1f;
 }

 private float getitemfactor(int index, float factor) {
  float itemfactor = (factor - 0.66f / point_num * index) * 3;
  if (itemfactor < 0f) {
   itemfactor = 0f;
  } else if (itemfactor > 1f) {
   itemfactor = 1f;
  }
  return minterpolator.getinterpolation(itemfactor);
 }

 public void startanim() {
  mplaytime = mplaytime % mduration;
  mstarttime = animationutils.currentanimationtimemillis() - mplaytime;
  mstartanim = true;
  postinvalidate();
 }

 public void reset() {
  stopanim();
  mplaytime = 0;
  postinvalidate();

 }

 public void stopanim() {
  mstartanim = false;
 }

 public void setinterpolator(timeinterpolator interpolator) {
  minterpolator = interpolator;
 }

 public void setduration(long duration) {
  mduration = duration;
 }

 public void setradius(float factor) {
  stopanim();
  calpoints(factor);
  startanim();
 }

 static class arcpoint {
  float x;
  float y;
  int color;

  arcpoint(float x, float y, int color) {
   this.x = x;
   this.y = y;
   this.color = color;
  }
 }

}

easeinoutcubicinterpolator是自定义view(circleprogress )中要是用的一个工具 

package com.hysmarthotel.util;

import android.animation.timeinterpolator;


public class easeinoutcubicinterpolator implements timeinterpolator {

 @override
 public float getinterpolation(float input) {
  if ((input *= 2) < 1.0f) {
   return 0.5f * input * input * input;
  }
  input -= 2;
  return 0.5f * input * input * input + 1;
 }

}

在activity中的调用(还有一些其他用法可以自己看看github上的源代码) 

mprogressview = (circleprogress)findviewbyid(r.id.progress_vie);
mprogressview.startanim(); //开始
mprogressview.stopanim(); //结束
mprogressview.setradius(factor); //半径
mprogressview.reset(); //复原

在xml文件中的布局 

<?xml version="1.0" encoding="utf-8"?>
<absolutelayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:circleprogress="http://schemas.android.com/apk/res/com.hysmarthotel.roomcontrol"  //这个地方记得要加 //包名
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@drawable/bg1" >
 
 <com.hysmarthotel.view.circleprogress                   //类名
  android:id="@+id/progress_vie"
  android:layout_x="350.5px"
  android:layout_y="150.0px"
  android:layout_width="1140.0px"
  android:layout_height="700.0px"
  circleprogress:color1="@android:color/holo_red_light"   //这些参数就是通过xmlns:circleprogress,和attrs文件相关联的     circleprogress:color2="@android:color/holo_green_light"     circleprogress:color3="@android:color/holo_blue_light" />

自己在values目录中新建的attrs文件,这是与自定义view中自定义参数相关的

 <declare-styleable name="circleprogress">
  <attr name="color1" format="reference|color"/>
  <attr name="color2" format="reference|color"/>
  <attr name="color3" format="reference|color"/>
 </declare-styleable> 

自己在values目录中新建的dimens文件,这个只是几个颜色参数

 <?xml version="1.0" encoding="utf-8"?>
<resources>
 <dimen name="activity_horizontal_margin">16dp</dimen>
 <dimen name="activity_vertical_margin">16dp</dimen>
 <dimen name="default_circle_view_size">200dp</dimen>
</resources>

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