Android:水波上升控件

来源:互联网 发布:centos 重命名文件夹 编辑:程序博客网 时间:2024/05/16 01:36
import com...util.DipUtil;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.PorterDuff.Mode;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.View;

/**

**圆内水波上升特效控件

**/

public class WaveView extends View implements Runnable {
private Paint wavePaint = new Paint();
private Paint circlePaint = new Paint();
private Paint textPaint = new Paint();
private Paint textPaint2 = new Paint();
private Paint circlePaint2 = new Paint();
private boolean isRun;
private int radius;
private int angle;
private int currentLevel, maxLevel;
private long used, quota;
private int width, height;
private PorterDuffXfermode xfermode = new PorterDuffXfermode(Mode.SRC_IN);
private double lineY = 400;
private Context context;


public WaveView(Context context) {
super(context);
this.context = context;
}


public WaveView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}


public WaveView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
}


@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (width == 0 || height == 0) {
height = getHeight();
width = getWidth();
currentLevel = height;
radius = width > height ? width / 2 : height / 2;
}
}


@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
drawWave(canvas);
drawText(canvas);
drawCircle(canvas);
}


private void drawWave(Canvas canvas) {
int saveFlags = Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG;
canvas.saveLayer(0, 0, width, height, null, saveFlags);
circlePaint.setAntiAlias(true);
circlePaint.setColor(Color.parseColor("#bedbeb"));
canvas.drawCircle(radius, radius, radius - DipUtil.dip2px(context, 18), circlePaint);
wavePaint.setXfermode(xfermode);
wavePaint.setAlpha(150);
wavePaint.setColor(Color.rgb(89, 186, 231));
for (int i = 0; i < width; i++) {
if (isRun) {
lineY = 20 * Math.sin((i + angle) * Math.PI / 180) + currentLevel;
}
canvas.drawLine(i, (int) lineY, i + 1, height, wavePaint);
}
if (isRun) {
if (currentLevel <= maxLevel) {
stop();
} else {
currentLevel = currentLevel - 1;
}
}
}


private void drawText(Canvas canvas) {
textPaint.setAntiAlias(true);
textPaint.setColor(Color.WHITE);
textPaint.setTextSize(DipUtil.dip2px(context, 28));
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
canvas.drawText((int) ((float) (height - currentLevel) / height * 100) + "%", width / 2 - DipUtil.dip2px(context, 20), height / 2 + DipUtil.dip2px(context, 3), textPaint);
textPaint2.setAntiAlias(true);
textPaint2.setColor(Color.WHITE);
textPaint2.setTextSize(DipUtil.dip2px(context, 14));
textPaint2.setTypeface(Typeface.DEFAULT_BOLD);
canvas.drawText(convert(used) + "/" + convert(quota), width / 2 - DipUtil.dip2px(context, 50), height / 2 + DipUtil.dip2px(context, 20), textPaint2);
}


private String convert(long size) {
long kb = 1024;
long mb = kb * 1024;
long gb = mb * 1024;
if (size >= gb) {
return String.format("%.1f GB", (float) size / gb);
} else if (size >= mb) {
float f = (float) size / mb;
return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
} else if (size >= kb) {
float f = (float) size / kb;
return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
} else
return String.format("%d B", size);
}


private void drawCircle(Canvas canvas) {
circlePaint2.setAntiAlias(true);
circlePaint2.setColor(Color.WHITE);
circlePaint2.setStyle(Paint.Style.STROKE);
circlePaint2.setStrokeWidth(DipUtil.dip2px(context, 7));
canvas.drawCircle(radius, radius, radius - DipUtil.dip2px(context, 7), circlePaint2);
}


@Override
public void run() {
while (isRun) {
angle++;
if (angle >= 360) {
angle = 0;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}


private void stop() {
isRun = false;
currentLevel = maxLevel;
angle = 0;
}

/**

*供外部调用,传入当前值与总值,如50*1024*1024,100*1024*1024

**/

public void show(long used, long quota) {
this.used = used;
this.quota = quota;
float percentage = (float) ((long) (((float) used / quota) * 100)) / 100;
maxLevel = (int) (height * (1 - percentage));
currentLevel = height;
angle = 0;
isRun = true;
new Thread(this).start();
}


}



0 0
原创粉丝点击