标签:
上代码比较直观
package com.weidingqiang.customchinacar.views.mapview; 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.Rect; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import com.weidingqiang.customchinacar.R; /** * Created by weidingqiang on 15/11/4. */ public class MapTipView extends View { //TAG标记 private static final String TAG = MapTipView.class.getSimpleName(); //加载状态 public static final int STATE_LOADING = 1; //加载完成状态 public static final int STATE_LOADED = 2; //加载完时间的状态 public static final int STATE_SHOWTIME = 3; //当前状态 private int _current = STATE_LOADING; //加载中的文字 private static final String loadingtext = "正在获取地址.."; //加载中的文字字体颜色 private int loadingColor = getResources().getColor(R.color.gray); //加载中的字体大小 private int loadingSize = 24; //加载中的字体宽度 private int loadingTextWidth; //加载中的字体宽度 private int loadingTextHeight; //paint private Paint paint; //获取文字所占用的矩形 private Rect rect; //向右的箭头 private Bitmap arrowRight; //---------------------------------------------------------------------------------------------// //状态 STATE_LOADED 的 属性 //位置 private String _address1,_address2; private int _address1Color = getResources().getColor(R.color.gold); private int _address2Color = getResources().getColor(R.color.gray); private int _address1Size = 28; private int _address2Size = 24; private int _address1Width,_address1Height; private int _address2Width,_address2Height; private static final String label1 = "我在"; private static final String label2 = "上车"; private int _labelColor = getResources().getColor(R.color.black); private int _labelSize = 26; private int _labelWidth,_labelHeight; private Bitmap mBackGround; public MapTipView(Context context) { this(context, null); } public MapTipView(Context context, AttributeSet attrs) { super(context, attrs); paint = new Paint(); rect = new Rect(); arrowRight = BitmapFactory.decodeResource(getResources(),R.drawable.arrow_right_gray_new); mBackGround = BitmapFactory.decodeResource(getResources(),R.drawable.map_tip_bg); } /** * 重写之前先了解MeasureSpec的specMode,一共三种类型: EXACTLY:一般是设置了明确的值或者是MATCH_PARENT AT_MOST:表示子布局限制在一个最大值内,一般为WARP_CONTENT UNSPECIFIED:表示子布局想要多大就多大,很少使用 http://blog.csdn.net/lmj623565791/article/details/24252901 * @param widthMeasureSpec * @param heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int _width = 0; int _height = 0; // mPaint.setTextSize(mTitleTextSize); // mPaint.getTextBounds(mTitle, 0, mTitle.length(), mBounds); // float textWidth = mBounds.width(); // int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight()); // width = desired; switch (_current){ case STATE_LOADING: paint.setTextSize(loadingSize); paint.getTextBounds(loadingtext,0,loadingtext.length(),rect); loadingTextWidth = rect.width(); loadingTextHeight = rect.height(); //需要文字的宽度加上箭头的宽度 int desiredW = (int) (getPaddingLeft() + loadingTextWidth +arrowRight.getWidth()+ getPaddingRight()); //int desiredW = (int) (getPaddingLeft() + loadingTextWidth + getPaddingRight()); int desiredH = (int) (getPaddingTop() + loadingTextHeight + getPaddingBottom()); _width = desiredW; _height = desiredH; break; case STATE_LOADED: //1.计算 “我在“ 宽高 paint.setTextSize(_labelSize); paint.getTextBounds(label1,0,label1.length(),rect); _labelHeight = rect.height(); _labelWidth = rect.width(); //2.计算 address1 宽高 paint.setTextSize(_address1Size); paint.getTextBounds(_address1,0,_address1.length(),rect); _address1Height = rect.height(); _address1Width = rect.width(); //3.计算 address2 宽高 paint.setTextSize(_address2Size); paint.getTextBounds(_address2,0,_address2.length(),rect); _address2Height = rect.height(); _address2Width = rect.width(); //4.综合计算 int maxWidth = Math.max(_labelWidth*2+_address1Width,_address2Width); _width = (int) (getPaddingLeft() + maxWidth +arrowRight.getWidth()+ getPaddingRight()); _height = (int) (getPaddingTop() + _address1Height +_address2Height+ getPaddingBottom()); break; case STATE_SHOWTIME: break; } setMeasuredDimension(_width,_height); } @Override protected void onDraw(Canvas canvas) { //重新设置组件宽高 ViewGroup.LayoutParams layoutParams = getLayoutParams(); layoutParams.height = getMeasuredHeight(); layoutParams.width = getMeasuredWidth(); setLayoutParams(layoutParams); switch (_current){ //只显示加载中 case STATE_LOADING: //画文字 paint.setColor(loadingColor); paint.setTextSize(loadingSize); int _x = getWidth() / 2 - loadingTextWidth / 2; int _y = getHeight() / 2 + loadingTextHeight / 2 - 10;//10 为偏移量 canvas.drawText(loadingtext,_x , _y, paint); //画图片 int _bitmapx = _x + loadingTextWidth + 12;//2 为偏移量 int _bitmapy = getHeight() / 2 - arrowRight.getHeight() / 2 - 6;//10 为偏移量; canvas.drawBitmap(arrowRight,_bitmapx,_bitmapy,paint); break; case STATE_LOADED: //画 “我在” paint.setColor(_labelColor); paint.setTextSize(_labelSize); int _label1x = getPaddingLeft(); int _label1y = getPaddingTop() + 10; canvas.drawText(label1,_label1x,_label1y,paint); //画 地点 paint.setColor(_address1Color); paint.setTextSize(_address1Size); int _address1x = _label1x + _labelWidth; int _address1y = _label1y; canvas.drawText(_address1,_address1x,_address1y,paint); //画 上车 paint.setColor(_labelColor); paint.setTextSize(_labelSize); int _label2x = _address1x+_address1Width; int _label2y = _address1y; canvas.drawText(label2,_label2x,_label2y,paint); //画 路口 paint.setColor(_address2Color); paint.setTextSize(_address2Size); int _address2x = _label1x ; int _address2y = _label1y+ _labelHeight + 10; canvas.drawText(_address2,_address2x,_address2y,paint); //画图片 int _bitmap2x = getWidth() - arrowRight.getWidth() - 14;//2 为偏移量 int _bitmap2y = getHeight() / 2 - arrowRight.getHeight() / 2 - 6;//10 为偏移量; canvas.drawBitmap(arrowRight,_bitmap2x,_bitmap2y,paint); break; case STATE_SHOWTIME: break; } } public void changeState(int state){ this.changeState(state,"",""); } public void changeState(int state,String address1,String address2){ this.changeState(state,address1,address2,""); } private int measure = 100; public void changeState(int state,String address1,String address2,String time){ _current = state; _address1 = address1; _address2 = address2; //改变宽高 才能执行 onMeasure measure++; this.measure(measure,measure); //调用ondraw方法 postInvalidate(); } }
标签:
原文地址:http://www.cnblogs.com/weidingqiang/p/4939286.html