美文网首页
Android自定义View可以转动的小圆球球

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

作者: EasyLife90 | 来源:发表于2017-03-31 11:29 被阅读0次

    废话少说 直接上码:

    自定义view部分。

    ``` java

    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中定义属性

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

    相关文章

      网友评论

          本文标题:Android自定义View可以转动的小圆球球

          本文链接:https://www.haomeiwen.com/subject/msnmottx.html