自定义开源控件的绘制实例-——价格区间图
来源:互联网 发布:kbengine java 编辑:程序博客网 时间:2024/05/20 17:09
效果如图,废话不多说,直接上代码。
import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.graphics.Rect;import android.graphics.Typeface;import android.support.annotation.NonNull;import android.text.Layout;import android.text.Spannable;import android.text.SpannableString;import android.text.StaticLayout;import android.text.TextPaint;import android.text.style.AbsoluteSizeSpan;import android.text.style.ForegroundColorSpan;import android.text.style.StyleSpan;import android.util.AttributeSet;import android.view.View;import com.zhugefang.agent.R;/** * Created by Administrator on 2016/6/20. * 价格区间view */public class PriceIntervalView extends View { private int lowX, lowY, heightX, heightY, curX, curY; private Paint paint, pathPaint; private TextPaint textPaint; private float angle; private int radius = 10, bigRadius = 18; private int offset = 50; private String unit = "m²"; private int topPrice = 10, floorPrice = 0, presentPrice; public PriceIntervalView(Context context, AttributeSet attrs) { super(context, attrs); paint = new Paint(); pathPaint = new Paint(); textPaint = new TextPaint(); textPaint.setTextSize(context.getResources().getDimensionPixelSize(R.dimen.micro_micro)); textPaint.setStrokeWidth(3); paint.setAntiAlias(true); pathPaint.setAntiAlias(true); textPaint.setAntiAlias(true); textPaint.setColor(getResources().getColor(R.color.color_FF878787)); paint.setColor(getResources().getColor(R.color.color_FFFA891B)); pathPaint.setColor(Color.parseColor("#FFECD5")); } /** * 设置价格区间参数 * * @param topPrice 最高价格 * @param floorPrice 最低价格 * @param presentPrice 当前价格 */ public void setIntervalParams(int topPrice, int floorPrice, int presentPrice) { this.setIntervalParams(topPrice, floorPrice, presentPrice, "m²"); } public void setIntervalParams(int topPrice, int floorPrice, int presentPrice, String unit) { this.topPrice = topPrice; this.floorPrice = floorPrice; this.presentPrice = presentPrice; this.unit = unit; invalidate(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); angle = (getHeight() - offset) / (float) getWidth(); canvas.drawLine(0, getHeight(), getWidth(), offset, paint); lowX = getWidth() / 4; lowY = (int) (getHeight() - lowX * angle); heightX = getWidth() * 3 / 4; heightY = (int) (getHeight() - heightX * angle); Path path = new Path(); path.moveTo(lowX, lowY); path.lineTo(lowX, offset); path.lineTo(heightX, offset); path.lineTo(heightX, heightY); canvas.drawPath(path, pathPaint); canvas.drawCircle(lowX, lowY, radius, paint); canvas.drawCircle(heightX, heightY, radius, paint); String floorStr = floorPrice + "元/" + unit + "\r\n最低参考价"; String topStr = topPrice + "元/" + unit + "\r\n最高参考价"; SpannableString spannableString = getSpannableString(floorStr, (floorPrice + "").length(), (floorPrice + "元/" + unit).length(), getResources().getColor(R.color.color_FF333333), (int) textPaint.getTextSize()); StaticLayout layout = new StaticLayout(spannableString, textPaint, 300, Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true); SpannableString spannableString1 = getSpannableString(topStr, (topPrice + "").length(), (topPrice + "元/" + unit).length(), getResources().getColor(R.color.color_FF333333), (int) textPaint.getTextSize()); StaticLayout layout1 = new StaticLayout(spannableString1, textPaint, 300, Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true); canvas.save(); canvas.translate(lowX, lowY + 5); layout.draw(canvas); canvas.translate(heightX - lowX, heightY - lowY); layout1.draw(canvas); canvas.restore(); calculationCurLocation(canvas); } @NonNull private SpannableString getSpannableString(String floorStr, int first, int second, int color, int size) { SpannableString spannableString = new SpannableString(floorStr); spannableString.setSpan(new StyleSpan(Typeface.BOLD), 0, first, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); spannableString.setSpan(new AbsoluteSizeSpan((int) (size * 1.3)), 0, first, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); spannableString.setSpan(new ForegroundColorSpan(color), 0, second, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return spannableString; } private void calculationCurLocation(Canvas canvas) { if (presentPrice == 0) return; if (presentPrice < floorPrice) { curX = (int) (lowX - (heightX - lowX) * 0.1); curY = (int) (getHeight() - angle * curX); } else if (presentPrice <= topPrice) { int interval = topPrice - floorPrice; int curInterval = presentPrice - floorPrice; float proportion = curInterval / (float) interval; curX = (int) (lowX + (heightX - lowX) * proportion); curY = (int) (getHeight() - angle * curX); } else { curX = (int) (heightX + (heightX - lowX) * 0.1); curY = (int) (getHeight() - angle * curX); } paint.setColor(Color.parseColor("#99FA891B")); canvas.drawCircle(curX, curY, bigRadius, paint); paint.setColor(getResources().getColor(R.color.color_FFFA891B)); canvas.drawCircle(curX, curY, radius, paint); String text = presentPrice + "元/" + unit + "\r\n当前单价"; Rect bounds = new Rect(); String text1 = presentPrice + "元/" + unit; textPaint.getTextBounds(text1, 0, text1.length(), bounds); SpannableString spannableString = getSpannableString(text, (presentPrice + "").length(), (presentPrice + "元/" + unit).length(), getResources().getColor(R.color.color_FFFA891B), (int) textPaint.getTextSize()); StaticLayout layout = new StaticLayout(spannableString, textPaint, 300, Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true); int layoutHeight = layout.getHeight(); float measureText = textPaint.measureText(spannableString, 0, (presentPrice + "元/" + unit).length()); BitmapFactory.Options options = new BitmapFactory.Options(); options.outWidth = (int) (measureText + offset); options.outHeight = layoutHeight + offset; Bitmap tv_Bg = BitmapFactory.decodeResource(getResources(), R.drawable.interval_textview_bg, options).copy(Bitmap.Config.ARGB_8888, true); float bgX = curX - tv_Bg.getWidth() / 2; float bgY = curY - tv_Bg.getHeight() - bigRadius; canvas.drawBitmap(tv_Bg, bgX, bgY, paint); canvas.save(); canvas.translate(curX - measureText / 2 - 10, curY - bigRadius - layoutHeight - 10); layout.draw(canvas); canvas.restore(); }}
阅读全文
0 0
- 自定义开源控件的绘制实例-——价格区间图
- 自定义开源控件的绘制实例-——类似支付宝信用值的进度条
- 自定义控件之——心电图控件的绘制
- 实现滑动选择价格区间的自定义seekbar
- 自定义控件——绘制view
- 自定义绘制控件的方法
- MFC控件的自定义绘制
- Android自定义控件——自定义View绘制钟表
- Android 自定义价格日历控件
- Android 自定义价格日历控件
- ecshop价格范围区间用户自定义选择价格代码
- 在ECSHOP后台自定义价格区间…
- Android控件架构与自定义控件详解(一)——Android控件架构与View的绘制原理
- Android自定义控件之《折线图的绘制》
- 自定义控件——视图树绘制指示器
- View和自定义控件的绘制流程
- 【Android】自定义控件的绘制过程
- Android 自定义控件文字的居中绘制
- Python 直接赋值、浅拷贝和深度拷贝解析
- 合并石子(求最小代价)
- JavaIOUtils
- msgbuf实现mtext[1]可变长代码
- 十进制转二进制
- 自定义开源控件的绘制实例-——价格区间图
- MVC中创建的数据库实体获取数据的基本语法
- lambda表达式-java中的应用
- form表单
- idea安装阿里巴巴java规范插件的两种方式
- ie浏览器日期(时间)字符串的坑
- Hive之——整合MySQL存储元数据信息及基本操作示例
- jQuery 自定义事件以及命名空间
- hessian(二)--与spring整合