package UICtrl;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.view.animation.Interpolator;
import android.graphics.Paint;
import android.graphics.PointF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.RelativeLayout; /**
* Created by wangchaomac on 2017/10/18.
*/
public class AroundCircleBall extends RelativeLayout { private BallView mBall; //角度
private float mBallRadius; //小球的颜色
private int mBallColor; private int mCircleRadius; private int mCircleColor; private int mDuration; //笔头描宽
private float mStrokeWidth; private int mInterpolator; private final int ACCELERATE_DECELERATE_INTERPOLATOR = 1; private final int LINEAR_INTERPOLATOR = 2; private final int ACCELERATE = 3; private final int DECELERATE = 4; ObjectAnimator mRotateAnim; PointF mCircleCenterPoint = new PointF(mCircleRadius + mBallRadius, mCircleRadius + mBallRadius);
Paint mPaint = new Paint(); public AroundCircleBall(Context context, AttributeSet attrs){ super(context, attrs);
// TODO Auto-generated constructor stub
init(context); } private void init(Context context){
init();
}
private void init() { mBall = new BallView(getContext());
mBall.setRadius(mBallRadius);
mBall.setBallColor(mBallColor);
//小球的初始位置在圆环的最底部
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.leftMargin = (int) (mCircleRadius);
params.topMargin = (int) (mCircleRadius * 2);
mBall.setLayoutParams(params);
addView(mBall); }
@Override
protected void onDraw(Canvas canvas) {
//绘制圆
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(mStrokeWidth);
mPaint.setColor(mCircleColor);
canvas.drawCircle(mCircleCenterPoint.x, mCircleCenterPoint.y, mCircleRadius, mPaint);
super.onDraw(canvas);
} protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//测量控件宽高
int width = (int) (getPaddingLeft() + mCircleRadius * 2 + mBallRadius * 2 + getPaddingRight());
int height = (int) (getPaddingTop() + mCircleRadius * 2 + mBallRadius * 2 + getPaddingBottom());
setMeasuredDimension(width, height);
} private void initRotateAnim(){
mRotateAnim = ObjectAnimator.ofFloat(mBall, "rotation", 0f, 360f);
//计算小球旋转的中心点(此点的左边是在小球自身的坐标系中)
float pivotX = mBall.getRadius();
float pivotY = mBall.getRadius() - mCircleRadius;
mBall.setPivotX(pivotX);
mBall.setPivotY(pivotY);
mRotateAnim.setDuration(mDuration);
mRotateAnim.setInterpolator(getInterpolator());
mRotateAnim.setRepeatCount(-1);
mRotateAnim.setStartDelay(500);
} private Interpolator getInterpolator(){
Interpolator interpolator = null;
switch (mInterpolator){
case ACCELERATE_DECELERATE_INTERPOLATOR: //先加上后减速
interpolator = new AccelerateDecelerateInterpolator();
break;
case LINEAR_INTERPOLATOR: //匀速
interpolator = new LinearInterpolator();
break;
case ACCELERATE: //加速
interpolator = new AccelerateInterpolator();
break;
case DECELERATE: //减速
interpolator = new DecelerateInterpolator();
break;
}
return interpolator;
} /**
* 启动旋转动画
*/ public void startRotate(){
if (mRotateAnim != null){
mRotateAnim.start();
}
} /**
* 暂停旋转动画
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void pauseRotate(){
if (mRotateAnim != null && mRotateAnim.isRunning()){
mRotateAnim.pause();
}
} /**
* 取消旋转动画
*/
public void cancelRotate(){
if (mRotateAnim != null){
mRotateAnim.cancel();
}
}
}
package UICtrl;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.View; /**
* Created by wangchaomac on 2017/10/18.
*/
public class BallView extends View { private int mBallColor = Color.BLACK;
private float mRadius = 10f;
private PointF mCenterPoint;
private Paint mPaint; public BallView(Context context) {
this(context, null);
} public BallView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} private void init(){
mPaint = new Paint();
mPaint.setAntiAlias(true);
mCenterPoint = new PointF(mRadius, mRadius);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = (int) (getPaddingLeft() + 2*mRadius + getPaddingRight());
int height = (int) (getPaddingTop() + 2*mRadius + getPaddingBottom());
setMeasuredDimension(width, height);
} @Override
protected void onDraw(Canvas canvas) {
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(mBallColor);
canvas.drawCircle(mCenterPoint.x, mCenterPoint.y, mRadius, mPaint);
} public void setBallColor(int mBallColor) {
this.mBallColor = mBallColor;
} public void setRadius(float radius) {
this.mRadius = radius;
mCenterPoint.set(radius, radius);
} public float getRadius() {
return mRadius;
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources><!--AroundCircleBall自定义属性-->
<declare-styleable name="AroundCircleBall">
<!--绘制圆形轨迹的线宽-->
<attr name="circle_width" format="dimension"></attr>
<!--绘制圆形轨迹的颜色-->
<attr name="circle_color" format="color"></attr>
<!--圆形轨迹的半径-->
<attr name="circle_radius" format="dimension"></attr>
<!--小球的颜色-->
<attr name="ball_color" format="color"></attr>
<!--小球的半径-->
<attr name="ball_radius" format="dimension"></attr>
<!--设置旋转一周需要的时间(单位为秒)-->
<attr name="rotate_duration" format="integer"></attr>
<!--设置旋转动画的补间器-->
<attr name="rotate_interpolator" format="enum">
<enum name="accelerate_decelerate" value="1"/>
<enum name="linear" value="2"/>
<enum name="accelerate" value="3"/>
<enum name="decelerate" value="4"/>
</attr>
</declare-styleable></resources>

androidcode的更多相关文章

  1. 编译android 4.4.2

    1.获取Android源码 (1)下载repo 在用户目录下创建一个bin文件夹来存放repo,并把该路径设置到环境变量中 mkdir ~/bin PATH=~/bin:$PATH 下载repo脚本 ...

  2. android 通过eclipse混淆代码 打包 + proguard 总结

    android应用程序的混淆打包 1 . 在工程文件project.properties中加入下proguard.config=proguard.cfg , 如下所示: target=android- ...

  3. Android Studio添加应用作为依赖时报错Error:Dependency MonthText:xlistview:unspecified on project app resolves to an APK archive which is not supported as a compilation dependency. File: 及其解决方案

    Error:Dependency MonthText:xlistview:unspecified on project app resolves to an APK archive which is ...

  4. android 混淆配置

    proguard 原理Java代码编译成二进制class 文件,这个class 文件也可以反编译成源代码 ,除了注释外,原来的code 基本都可以看到.为了防止重要code 被泄露,我们往往需要混淆( ...

  5. vue中使用swiper-slide时,循环轮播失效?

    前言 vue 项目中使用时,组件swiper-slide 如果用v-for循环的话,loop:true 就不能无缝轮播,每次轮播到最后一张就停止了??? 正文 代码如下: <swiper :op ...

  6. android 混淆 与 反编译

    1, 文件 project.properties 修改: target=android-14proguard.config=${sdk.dir}/tools/proguard/proguard-and ...

  7. Android中怎样调用拨打电话?

    Android系统原本就为手机设计,所以,在android系统中的不论什么App中,仅仅要愿意,拨打指定电话很方便. 核心就是使用Intent跳转,指定请求Action为Intent.ACTION_C ...

  8. Android开发学习之反编译APK文件

    反编译的目的在于学习一些优秀的Android应用程序代码. 在进行反编译之前,需要准备好下面的软件工具(这些文件都放在同一文件下): 这些工具的下载地址:http://down.51cto.com/d ...

  9. android发送短信样例

    Android应用开发中我们经常须要发送手机短信.这对于android平台来说,是最简单只是的功能了,无需太多代码,也无需自己定义代码,仅仅须要调用android提供的消息管理类SmsManager就 ...

随机推荐

  1. Android的崩溃类型总结

    看了篇腾讯的沙龙分享,觉得很不错,在这里分享给大家 crash的大致划分:

  2. V1-bug Alpha阶段测试报告

    发现的Bug Bug现象 Bug原因 是否解决 访问到错误的视图 路由正则写的太过宽泛 是 主题太长时超过页面宽度,导致超过顶部的宽度 / 否 无法使用域名访问服务器 后端没有在配置文件的ALLOWE ...

  3. 使用chart.js時取消懸浮在圖表頂部的'undefined'標識

    解決方法:在options中設置legend項中display屬性為false options: { scales: { yAxes: [{ ticks: { beginAtZero: true } ...

  4. Python+selenium实现登录脚本

    import unittestfrom selenium import webdriverfrom time import sleepclass LoginCase(unittest.TestCase ...

  5. IE的haslayout的触发

    原文连接:http://www.cnblogs.com/yunxuange/archive/2012/09/19/2693886.html layout是Windows上的IE浏览器产生许多bug的根 ...

  6. 收集整理mysql数据库设计规范与原则

    1. 数据库命名规范 采用26个英文字母(区分大小写)和0-9的自然数(经常不需要)加上下划线'_'组成;命名简洁明确(长度不能超过30个字符);例如:user, stat, log, 也可以wifi ...

  7. 6.006 Introduction to Algorithms

    课程信息 6.006 Introduction to Algorithms

  8. JAVA实现多线程处理批量发送短信、APP推送

    /** * 推送消息 APP.短信 * @param message * @throws Exception */ public void sendMsg(Message message) throw ...

  9. window.location.href.substr(window.location.href.length - 6)

    if (window.location.href.substr(window.location.href.length - 6) == "flag=1") { var tOptio ...

  10. Eigen库矩阵运算使用方法

    Eigen库矩阵运算使用方法 Eigen这个类库,存的东西好多的,来看一下主要的几个头文件吧: ——Core 有关矩阵和数组的类,有基本的线性代数(包含 三角形 和 自伴乘积 相关),还有相应对数组的 ...