Skip to content

Instantly share code, notes, and snippets.

@holmeszyx
Created September 10, 2014 15:28
Show Gist options
  • Save holmeszyx/721d11769c2a7bb4523b to your computer and use it in GitHub Desktop.
Save holmeszyx/721d11769c2a7bb4523b to your computer and use it in GitHub Desktop.
一个刻度盘的demo
package z.hol.viewtest.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
/**
* 刻度view
* Created by holmes on 9/10/14.
*/
public class DialProgressView extends View{
private Rect mDialDect = new Rect();
private int mRadius;
private float nickAngle;
private Point mCentre = new Point();
private int mDialLength;
private Paint mPaint;
private float mDialAngleOffset;
public DialProgressView(Context context) {
super(context);
init(context, null);
}
public DialProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public DialProgressView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs){
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
int width = 400;
int height = 350;
mDialLength = 20;
mDialDect.set(100, 100, 100 + width, 100 + height);
mRadius = width >> 1;
mCentre.x = (mDialDect.width() >> 1) + mDialDect.left;
mCentre.y = (mDialDect.height() >> 1) + mDialDect.top;
computeNickAngle();
}
private void computeNickAngle(){
float remin = 270.0f;
mDialAngleOffset = remin / (float) 100;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawDial(canvas);
}
private void drawDial(Canvas canvas){
canvas.save();
float offset = 180.0f - 90.0f / 2;
canvas.rotate(-offset, mCentre.x, mCentre.y);
mPaint.setStrokeWidth(2);
mPaint.setColor(Color.WHITE);
int currentX = mCentre.x;
int currentY = mDialDect.top;
int dx = currentX;
int dy = currentY + mDialLength;
for (int i = 0; i < 100; i ++){
float da = 0.0f;
if (i > 0){
da = mDialAngleOffset;
}
canvas.rotate(da, mCentre.x, mCentre.y);
canvas.drawLine(currentX, currentY, dx, dy, mPaint);
}
canvas.restore();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment