自定义View|自定义View之温度计

package microreader.caiyao.name.microreader.api; import com.spd.air.R; import android.R.integer; import android.annotation.SuppressLint; import android.content.Context; import android.drm.DrmStore.RightsStatus; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.Typeface; import android.graphics.Xfermode; import android.graphics.PorterDuff.Mode; import android.util.AttributeSet; import android.util.Log; import android.view.View; @SuppressLint("ResourceAsColor") public class WindView extends View { private int tem = 0; private static float maxTem = 35; private Xfermode mXfermode = new PorterDuffXfermode(Mode.CLEAR); private Paint mPaint; private Paint temPaint; private Paint paint; private Paint textPaint; // 外边框参数 int radios = 8; // 小圆半径 int r = 2 * radios; // 大圆半径 int height = 55; // 外边框线条长度 int left = 15; // 小圆中心点 int sCenterX; int sCenterY; // 大圆中心点 int lCenterX; int lCenterY; int innerSRadios = 5; int innerLRadios = r - 4; int innerHeight = 4 + height; int sRadiuTem; int lRadiuTem; int rectTem; private int innerLRadiuHeight; private int color; public WindView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint = new Paint(); color = Color.rgb(20, 156, 225); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(2); mPaint.setColor(color); paint = new Paint(); paint.setColor(color); paint.setAntiAlias(true); temPaint = new Paint(); temPaint.setAntiAlias(true); temPaint.setColor(color); textPaint=new Paint(); textPaint.setAntiAlias(true); textPaint.setColor(Color.WHITE); /*textPaint.setStyle(Paint.Style.STROKE); */ textPaint.setStrokeWidth(3); textPaint.setTextSize(25); sCenterX = left + radios; sCenterY = sCenterX; int lTop = sCenterY + height - (r - (int) (r * Math.cos(2 * Math.PI / 360 * 30))); lCenterX = left - radios + r; lCenterY = lTop + r; // 计算内部大圆最高温度,矩形最高温度,小圆最高温度;innerLRadiuHeight = innerLRadios + (int) (innerLRadios * Math.cos(2 * Math.PI / 360 * 30)); int innerRectHeight = innerHeight; int innerSRadiuHeight = innerSRadios; int innerTotalHeight = innerLRadiuHeight + innerRectHeight + innerSRadiuHeight; lRadiuTem = (int) (innerLRadiuHeight * maxTem / innerTotalHeight); sRadiuTem = (int) (innerSRadiuHeight * maxTem / innerTotalHeight); rectTem = (int) (innerRectHeight * maxTem / innerTotalHeight); Log.e("xlq", innerLRadiuHeight + ""); Log.e("xlq", lRadiuTem + ""); } public WindView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public WindView(Context context) { this(context, null); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 画外边框 RectF rect = new RectF(left, left, left + radios * 2, left + radios * 2); canvas.drawArc(rect, 0, -180, false, mPaint); canvas.drawLine(left, sCenterY, left, sCenterY + height, mPaint); canvas.drawLine(sCenterX + radios, sCenterY, sCenterX + radios, sCenterY + height, mPaint); RectF rect1 = new RectF(lCenterX - r, lCenterY - r, lCenterX + r, lCenterY + r); canvas.drawArc(rect1, -60, 300, false, mPaint); // 温度int mTem = 0; if (tem >= 0) { paint.setColor(color); int sc = canvas.saveLayer(lCenterX - innerLRadios, lCenterY - innerLRadios, lCenterX + innerLRadios, lCenterY + innerLRadios, null, Canvas.ALL_SAVE_FLAG); mTem = tem > lRadiuTem ? lRadiuTem : tem; int top = lCenterY + innerLRadios - (int) (mTem / (1.0f * lRadiuTem) * innerLRadiuHeight); canvas.drawCircle(lCenterX, lCenterY, innerLRadios, paint); paint.setXfermode(mXfermode); paint.setColor(Color.TRANSPARENT); canvas.drawRect(lCenterX - innerLRadios, lCenterY - innerLRadios, lCenterX + innerLRadios, top, paint); paint.setXfermode(null); canvas.restoreToCount(sc); } if (tem > lRadiuTem) {mTem = tem > lRadiuTem + rectTem ? lRadiuTem + rectTem : tem; int height = (int) ((mTem - lRadiuTem) / (1.0f * rectTem) * innerHeight); canvas.drawRect(sCenterX - innerSRadios, sCenterY + innerHeight - height, sCenterX + innerSRadios, sCenterY + innerHeight, temPaint); }if (tem > lRadiuTem + rectTem) { paint.setColor(color); int sc = canvas.saveLayer(sCenterX - innerSRadios, sCenterY - innerSRadios, sCenterX + innerSRadios, sCenterY + innerSRadios, null, Canvas.ALL_SAVE_FLAG); mTem = (int) (tem > maxTem ? maxTem : tem); int bottom = (int) ((mTem - rectTem - lRadiuTem) / (sRadiuTem * 1.0f) * innerSRadios); canvas.drawCircle(sCenterX, sCenterY, innerSRadios, paint); paint.setXfermode(mXfermode); paint.setColor(Color.TRANSPARENT); canvas.drawRect(sCenterX - innerSRadios, sCenterY - innerSRadios, sCenterX + innerSRadios, innerSRadios - bottom, paint); paint.setXfermode(null); canvas.restoreToCount(sc); }//字 canvas.drawText(tem+" ℃", sCenterX+innerLRadios+4, sCenterY+innerSRadios*2+8, textPaint); } public void setTemperature(int temperature) { tem = temperature; this.invalidate(); }}



效果图:

自定义View|自定义View之温度计
文章图片

【自定义View|自定义View之温度计】

    推荐阅读