您的位置:首页 > 移动开发 > Android开发

android自定义loding view(下)

2016-04-21 19:55 489 查看
github:https://github.com/1181631922/OrliteDemo





好来看一下view,原来是单个写的,但是抽象一下其实差不多,因为推理的时候就是按照现在这种for循环的方式计算的,还有就是现在是八个圆,如果更改个数需要进行重新计算,大家可以自己计算一下,如果项目需要有类似的话,这里博主就省去计算过程了

package com.fanyafeng.orlitedemo.myview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
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 com.fanyafeng.orlitedemo.util.MyUtils;

/**
* Created by 365rili on 16/4/20.
*/
public class CircleLoadingView extends View {

private Paint circleContour;
private double durSpace = 0;

private CharSequence message;
private StaticLayout messageLayout;
private static TextPaint textPaint;

private int circleContourColor;
private int messageColor;

public int getCircleContourColor() {
return circleContourColor;
}

public void setCircleContourColor(int circleContourColor) {
this.circleContourColor = circleContourColor;
invalidate();
}

public int getMessageColor() {
return messageColor;
}

public void setMessageColor(int messageColor) {
this.messageColor = messageColor;
setMessage(getMessage());
}

public CharSequence getMessage() {
return message;
}

public void setMessage(CharSequence message) {
this.message = message;
if (message != null && !message.equals("")) {
textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
if (getMessageColor() != 0) {
textPaint.setColor(getMessageColor());
} else {
textPaint.setColor(Color.parseColor("#3498DB"));
}
textPaint.setTextSize(14 * MyUtils.getDensity(getContext()));
textPaint.setStyle(Paint.Style.FILL);
messageLayout = new StaticLayout(message, textPaint, MyUtils.getScreenWidth(getContext()) / 4, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
invalidate();
}

}

public CircleLoadingView(Context context) {
super(context);
}

public CircleLoadingView(Context context, AttributeSet attrs) {
super(context, attrs);
circleContour = new Paint(Paint.ANTI_ALIAS_FLAG);
}

public CircleLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float circleContourStrokeWidth = 5;
circleContour.setAntiAlias(true);
if (getCircleContourColor() != 0) {
circleContour.setColor(getCircleContourColor());
} else {
circleContour.setColor(Color.parseColor("#3498DB"));
}
//        circleContour.setStyle(Paint.Style.STROKE);
circleContour.setStrokeWidth(circleContourStrokeWidth);

float startX = getLeft();
float endX = getRight();
//        Log.d("TAG","得到的endX的数值:"+endX);
float startY = getTop();
float endY = getBottom();
float height = getHeight();
float width = getWidth();
//        轮廓的半径
float circleContourRadius = (endY > endX ? (endX - startX) / 2 - circleContourStrokeWidth / 2 : (endY - startY) / 2 - circleContourStrokeWidth / 2);
float circleCountourX;
float circleCountourY;
if (endX > endY) {
circleCountourX = width / 2;
circleCountourY = height / 2;
} else {
circleCountourX = circleCountourY = width / 2;
}

//        canvas.drawCircle(circleCountourX, circleCountourY, circleContourRadius, circleContour);

//        画一条垂直的线
//        canvas.drawLine(circleCountourX, circleCountourY, circleCountourX, circleCountourY - circleContourRadius, circleContour);

//        根据公式推导,每个间隔所占的角度为:11.25度,最小的圆的度数为7.5度,每个圆以7.5度等差增长,一共需要画8个圆
//        第一个:sin3.75*第一个大圆的半径   sin7.5  sin11.25 sin15 sin18.75 sin22.5 sin26.25 sin30
//        内接大圆的直径为外接大圆半径的三分之二
float radius = circleContourRadius / 3;
//        loading圆们距离圆心的距离
float circleLength = 2 * radius;
//        每一度所对应的数值
double everyRadiusLength = 2 * Math.PI / 360;

//        Log.d("TAG", "圆心距离:" + circleLength + "每一度的距离:" + everyRadiusLength);
/*
//      每个间隔的角度
double spaceAngle = 11.25;
//      画最后一个要旋转的圆(大圆)
//      canvas.drawCircle(circleCountourX, circleCountourY - radius * 2, (float) eighthRadius, circleContour);
double eighthAngle = durSpace + 30;//30
//        Log.d("TAG","第八:"+eighthAngle);//30
double eighthX = circleCountourX - circleLength * Math.sin(eighthAngle * everyRadiusLength);
double eighthY = circleCountourY - circleLength * Math.cos(eighthAngle * everyRadiusLength);
double eighthRadius = circleLength * Math.sin(30 * everyRadiusLength);
canvas.drawCircle((float) eighthX, (float) eighthY, (float) eighthRadius, circleContour);

double seventhAngle = durSpace + 30 * 2 + spaceAngle + 26.25;//97.5
//        Log.d("TAG","第七:"+seventhAngle);//97.5
double seventhX = circleCountourX - circleLength * Math.sin(seventhAngle * everyRadiusLength);
double seventhY = circleCountourY - circleLength * Math.cos(seventhAngle * everyRadiusLength);
double seventhRadius = circleLength * Math.sin(26.25 * everyRadiusLength);
canvas.drawCircle((float) seventhX, (float) seventhY, (float) seventhRadius, circleContour);

double sixthAngle = durSpace + 30 * 2 + spaceAngle + 26.25 * 2 + spaceAngle + 22.5;
//        Log.d("TAG","第六:"+sixthAngle);//157.5
double sixthX = circleCountourX - circleLength * Math.sin(sixthAngle * everyRadiusLength);
double sixthY = circleCountourY - circleLength * Math.cos(sixthAngle * everyRadiusLength);
double sixthRadius = circleLength * Math.sin(22.5 * everyRadiusLength);
canvas.drawCircle((float) sixthX, (float) sixthY, (float) sixthRadius, circleContour);

double fifthAngle = durSpace + 30 * 2 + spaceAngle + 26.25 * 2 + spaceAngle + 22.5 * 2 + spaceAngle + 18.75;
//        Log.d("TAG","第五:"+fifthAngle);//210
double fifthX = circleCountourX - circleLength * Math.sin(fifthAngle * everyRadiusLength);
double fifthY = circleCountourY - circleLength * Math.cos(fifthAngle * everyRadiusLength);
double fifthRadius = circleLength * Math.sin(18.75 * everyRadiusLength);
canvas.drawCircle((float) fifthX, (float) fifthY, (float) fifthRadius, circleContour);

double forthAngle = durSpace + 30 * 2 + spaceAngle + 26.25 * 2 + spaceAngle + 22.5 * 2 + spaceAngle + 18.75 * 2 + spaceAngle + 15;
//        Log.d("TAG","第四:"+forthAngle);//255
double forthX = circleCountourX - circleLength * Math.sin(forthAngle * everyRadiusLength);
double forthY = circleCountourY - circleLength * Math.cos(forthAngle * everyRadiusLength);
double forthRadius = circleLength * Math.sin(15 * everyRadiusLength);
canvas.drawCircle((float) forthX, (float) forthY, (float) forthRadius, circleContour);

double thirdAngle = durSpace + 30 * 2 + spaceAngle + 26.25 * 2 + spaceAngle + 22.5 * 2 + spaceAngle + 18.75 * 2 + spaceAngle + 15 * 2 + spaceAngle + 11.25;
//        Log.d("TAG","第三:"+thirdAngle);//292.5
double thirdX = circleCountourX - circleLength * Math.sin(thirdAngle * everyRadiusLength);
double thirdY = circleCountourY - circleLength * Math.cos(thirdAngle * everyRadiusLength);
double thirdRadius = circleLength * Math.sin(11.25 * everyRadiusLength);
canvas.drawCircle((float) thirdX, (float) thirdY, (float) thirdRadius, circleContour);

double secondAngle = durSpace + 30 * 2 + spaceAngle + 26.25 * 2 + spaceAngle + 22.5 * 2 + spaceAngle + 18.75 * 2 + spaceAngle + 15 * 2 + spaceAngle + 11.25 * 2 + spaceAngle + 7.5;
//        Log.d("TAG","第二:"+secondAngle);//322.5
double secondX = circleCountourX - circleLength * Math.sin(secondAngle * everyRadiusLength);
double secondY = circleCountourY - circleLength * Math.cos(secondAngle * everyRadiusLength);
double secondRadius = circleLength * Math.sin(7.5 * everyRadiusLength);
canvas.drawCircle((float) secondX, (float) secondY, (float) secondRadius, circleContour);

//      求出第一个圆的x和y的位置
double firstAngle = durSpace + 30 * 2 + spaceAngle + 26.25 * 2 + spaceAngle + 22.5 * 2 + spaceAngle + 18.75 * 2 + spaceAngle + 15 * 2 + spaceAngle + 11.25 * 2 + spaceAngle + 7.5 * 2 + spaceAngle + 3.75;
//        Log.d("TAG","第一:"+firstAngle);//345
double firstX = circleCountourX - circleLength * Math.sin(firstAngle * everyRadiusLength);
double firstY = circleCountourY - circleLength * Math.cos(firstAngle * everyRadiusLength);
double firstRadius = circleLength * Math.sin(3.75 * everyRadiusLength);
canvas.drawCircle((float) firstX, (float) firstY, (float) firstRadius, circleContour);

/*
*/

//        相差7.5
double initAngle = 345;
double initRadius = 3.75;//相差3.75
for (int i = 0; i < 8; i++) {
double temp=0;
double total=0;
for (int j = 0; j <= i; j++) {
temp=j*7.5;
total+=temp;
}
double itemAngle = durSpace + (initAngle - (total+i*15));
double itemX = circleCountourX - circleLength * Math.sin(itemAngle * everyRadiusLength);
double itemY = circleCountourY - circleLength * Math.cos(itemAngle * everyRadiusLength);
double itemRadius = circleLength * Math.sin((initRadius + i * 3.75) * everyRadiusLength);
canvas.drawCircle((float) itemX, (float) itemY, (float) itemRadius, circleContour);
}

//        Log.d("TAG", "第一个圆半径:" + firstRadius + "第二个圆半径:" + secondRadius + "第三个圆半径:" + thirdRadius + "第四个圆半径:" + forthRadius);
//        Log.d("TAG", "第五个圆半径:" + fifthRadius + "第六个圆半径:" + sixthRadius + "第七个圆半径:" + seventhRadius + "第八个圆半径:" + eighthRadius);
//        同心圆
//        canvas.drawCircle(circleCountourX, circleCountourY, circleLength, circleContour);
canvas.save();
canvas.translate(0, 2 * circleContourRadius + 50);
messageLayout.draw(canvas);
canvas.restore();

postDelayed(refresh, 37);
}

private Runnable refresh = new Runnable() {
@Override
public void run() {
durSpace -= 3;
invalidate();
}
};
}
以上是修改后的,原来的代码注释了,没有删除,大家可以自己试着修改一下,内存和cpu处理感觉不好,大家有建议可以提,我自己也想一下是不是有好的方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: