Android自定义View可以转动的小圆球球

废话少说 直接上码:

自定义view部分。

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.drawable.Drawable;

import android.os.Handler;

import android.os.Looper;

import android.os.Message;

import android.text.Layout;

import android.text.StaticLayout;

import android.text.TextPaint;

import android.util.AttributeSet;

import android.util.Log;

import android.view.View;

import java.lang.reflect.Method;

/**

  • TODO: document your custom view class.

*/

public class CircleView extends View {

//大圆属性

private boolean mGradientAble = false;//颜色渐变

private int mBigCircleRadius;//大圆半径

private int mBigCircleColor;//大圆颜色

private boolean mRotationFunction = false;//是否显示小圈转动

private boolean mBigHollow = false;//是否空心

private int mPaintSize = 4;//大圆画笔大小

// 小圆属性

private int mSmallCircleColor;//小圆颜色

private int mSmallCircleRadius;//小圆半径

private boolean mSmallHollow = false;//是否空心

private int mSmallPaintSize = 2;//小圆画笔大小

private Paint mSmallCirclePaint;//小圆画笔

private Paint mBigCirclePaint;//大圆画笔

private int mBigCircleX, mBigCircleY, mSmallCircleX, mSmallCircleY;

private String mContentStr;//字内容

private int mTextColor = Color.GRAY;//字的颜色

private int mTextSize;//字大小

private int mWrapNumber;//换行字数

private boolean mRunSmallBallLogic = true;

private int mRunDegree = 0;

private int mTimeSleep = 10;

private int mRunDegreeAdd=2;

private final int UPDATE_VIEW_MSG=1;

private TextPaint mTextPaint;

private float mTextWidth;

private float mTextHeight;

private Handler mHandler = new Handler(Looper.getMainLooper()) {

@Override

public void dispatchMessage(Message msg) {

super.dispatchMessage(msg);

if (msg.what == UPDATE_VIEW_MSG) {

calculatCenterPoint();

}

}

};

/**

  • 计算圆点中心

*/

private void calculatCenterPoint(){

double cos = Math.cos(mRunDegree * Math.PI / 180);

double sin = Math.sin(mRunDegree * Math.PI / 180);

mSmallCircleX = (int) (mBigCircleX + mBigCircleRadius * cos);

mSmallCircleY = (int) (mBigCircleY - mBigCircleRadius * sin);

mRunDegree += mRunDegreeAdd;

invalidate();

mHandler.sendEmptyMessageDelayed(UPDATE_VIEW_MSG, mTimeSleep);

}

public CircleView(Context context) {

super(context);

init(null, 0);

}

public CircleView(Context context, AttributeSet attrs) {

super(context, attrs);

init(attrs, 0);

}

public CircleView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init(attrs, defStyle);

}

private void init(AttributeSet attrs, int defStyle) {

// Load attributes

final TypedArray a = getContext().obtainStyledAttributes(

attrs, R.styleable.CircleView, defStyle, 0);

mGradientAble = a.getBoolean(R.styleable.CircleView_mGradientAble, false);

mBigCircleRadius = (int) a.getDimension(R.styleable.CircleView_mBigCircleRadius, 0);//大圆半径

mBigCircleColor = a.getColor(R.styleable.CircleView_mBigCircleColor, Color.GRAY);//大圆颜色

mRotationFunction = a.getBoolean(R.styleable.CircleView_mRotationFunction, false);//是否显示小圈转动

mBigHollow = a.getBoolean(R.styleable.CircleView_mBigHollow, true);//是否空心

mPaintSize = (int) a.getDimension(R.styleable.CircleView_mPaintSize, 4);//大圆画笔大小

// 小圆属性

mSmallCircleColor = a.getColor(R.styleable.CircleView_mSmallCircleColor, Color.GRAY);//大圆颜色;//小圆颜色

mSmallCircleRadius = (int) a.getDimension(R.styleable.CircleView_mSmallCircleRadius, 0);//小圆半径

mSmallHollow = a.getBoolean(R.styleable.CircleView_mSmallHollow, false);//是否空心

mSmallPaintSize = (int) a.getDimension(R.styleable.CircleView_mSmallPaintSize, 2);//小圆画笔大小

mContentStr = a.getString(R.styleable.CircleView_mContentStr);//字内容

mTextColor = a.getColor(R.styleable.CircleView_mTextColor, Color.GRAY);//字的颜色

mTextSize = (int) a.getDimension(R.styleable.CircleView_mTextSize, 10);//字大小

mWrapNumber = a.getInt(R.styleable.CircleView_mWrapNumber, 2);//字大小

a.recycle();

mSmallCirclePaint = new Paint();

mBigCirclePaint = new Paint();

mTextPaint = new TextPaint();

setPaint();

}

private void logicSmallBall() {

// mRunSmallBallLogic = true;

// new Thread() {

// @Override

// public void run() {

// super.run();

// while (mRunSmallBallLogic) {

// mSmallCircleX = (int) (mBigCircleX + (mBigCircleRadius + mPaintSize / 2f) * Math.cos(mRunDegree));

// mSmallCircleY = (int) (mBigCircleY - (mBigCircleRadius + mPaintSize / 2f) * Math.sin(mRunDegree));

// mRunDegree++;

// try {

// Thread.sleep(mTimeSleep);

// } catch (InterruptedException e) {

// e.printStackTrace();

// }

// mHandler.sendEmptyMessage(1);

// }

// }

// }.start();

mHandler.removeMessages(1);

mHandler.sendEmptyMessageDelayed(1, mTimeSleep);

}

@Override

protected void onDetachedFromWindow() {

super.onDetachedFromWindow();

}

//设置画笔

public void setPaint() {

mSmallCirclePaint.setAntiAlias(true);//去锯齿

mSmallCirclePaint.setColor(mSmallCircleColor);

mSmallCirclePaint.setStrokeWidth(mSmallPaintSize);

mBigCirclePaint.setAntiAlias(true);

mBigCirclePaint.setColor(mBigCircleColor);

mBigCirclePaint.setStrokeWidth(mPaintSize);

mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);

mTextPaint.setTextAlign(Paint.Align.CENTER);

if (mBigHollow) {

mBigCirclePaint.setStyle(Paint.Style.STROKE);

} else {

mBigCirclePaint.setStyle(Paint.Style.FILL);

}

if (mSmallHollow) {

mSmallCirclePaint.setStyle(Paint.Style.STROKE);

} else {

mSmallCirclePaint.setStyle(Paint.Style.FILL);

}

invalidateTextPaintAndMeasurements();

}

private void invalidateTextPaintAndMeasurements() {

mTextPaint.setTextSize(mTextSize);

mTextPaint.setColor(mTextColor);

mTextWidth = mTextPaint.measureText(mContentStr);

Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();

mTextHeight = fontMetrics.bottom;

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

canvas.save();

StaticLayout layout = new StaticLayout(mContentStr, mTextPaint, 10000, Layout.Alignment.ALIGN_NORMAL, 1, 0, false);

canvas.translate(mBigCircleX, mBigCircleY - 3 * mTextHeight);//从100,100开始画

layout.draw(canvas);

canvas.restore();

canvas.drawCircle(mBigCircleX, mBigCircleY, mBigCircleRadius, mBigCirclePaint);

if (mRotationFunction) {

canvas.drawCircle(mSmallCircleX, mSmallCircleY, mSmallCircleRadius, mSmallCirclePaint);

}

// Log.e("TAG", "mBigCircleRadius:" + mBigCircleRadius + " mTextHeight:" + mTextHeight + " mContentStr" + mContentStr.length());

}

@Override

public void onWindowFocusChanged(boolean hasWindowFocus) {

super.onWindowFocusChanged(hasWindowFocus);

if (hasWindowFocus) {

mBigCircleX = this.getWidth() / 2;

mBigCircleY = this.getHeight() / 2;

if (mBigCircleRadius == 0) {

mBigCircleRadius = this.getWidth() / 2 - 2 * mPaintSize;

}

if (mRotationFunction) {//显示旋转小球

mHandler.removeMessages(UPDATE_VIEW_MSG);

calculatCenterPoint();

}

invalidate();

}

}

public void setmRunDegreeAdd(int mRunDegreeAdd) {

this.mRunDegreeAdd = mRunDegreeAdd;

}

public void setmTimeSleep(int mTimeSleep) {

this.mTimeSleep = mTimeSleep;

}

public void setmTextSize(int mTextSize) {

this.mTextSize = mTextSize;

mTextPaint.setTextSize(mTextSize);

}

public void setmTextColor(int mTextColor) {

this.mTextColor = mTextColor;

mTextPaint.setColor(mTextColor);

}

public void setmContentStr(String mContentStr) {

this.mContentStr = mContentStr;

}

public void setmGradientAble(boolean mGradientAble) {

this.mGradientAble = mGradientAble;

}

public void setmBigCircleRadius(int mBigCircleRadius) {

this.mBigCircleRadius = mBigCircleRadius;

}

public void setmBigCircleColor(int mBigCircleColor) {

this.mBigCircleColor = mBigCircleColor;

mBigCirclePaint.setColor(mBigCircleColor);

}

public void setmRotationFunction(boolean mRotationFunction) {

this.mRotationFunction = mRotationFunction;

}

public void setmBigHollow(boolean mBigHollow) {

this.mBigHollow = mBigHollow;

}

public void setmPaintSize(int mPaintSize) {

this.mPaintSize = mPaintSize;

mBigCirclePaint.setStrokeWidth(mPaintSize);

}

public void setmSmallCircleColor(int mSmallCircleColor) {

this.mSmallCircleColor = mSmallCircleColor;

mSmallCirclePaint.setColor(mSmallCircleColor);

}

public void setmSmallCircleRadius(int mSmallCircleRadius) {

this.mSmallCircleRadius = mSmallCircleRadius;

}

public void setmSmallHollow(boolean mSmallHollow) {

this.mSmallHollow = mSmallHollow;

}

public void setmSmallPaintSize(int mSmallPaintSize) {

this.mSmallPaintSize = mSmallPaintSize;

mSmallCirclePaint.setStrokeWidth(mSmallPaintSize);

}

public void setmTextWidth(float mTextWidth) {

this.mTextWidth = mTextWidth;

}

public void setmTextHeight(float mTextHeight) {

this.mTextHeight = mTextHeight;

}

}```


需要在attrs中定义属性

为什么粘不了代码:?????

[图片上传失败...(image-2f7c0-1514190955681)]

[图片上传失败...(image-3e06c3-1514190955681)]

[图片上传失败...(image-316091-1514190955681)]

[图片上传失败...(image-5a9c9f-1514190955681)]

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,607评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,047评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,496评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,405评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,400评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,479评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,883评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,535评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,743评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,544评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,612评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,309评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,881评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,891评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,136评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,783评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,316评论 2 342

推荐阅读更多精彩内容