| | |
| | | |
| | | import android.content.Context; |
| | | import android.content.res.TypedArray; |
| | | import android.graphics.Bitmap; |
| | | import android.graphics.Canvas; |
| | | import android.graphics.Color; |
| | | import android.graphics.Paint; |
| | | import android.graphics.Rect; |
| | | import android.graphics.RectF; |
| | | import android.graphics.drawable.BitmapDrawable; |
| | | import android.graphics.*; |
| | | import android.graphics.drawable.Drawable; |
| | | import android.util.AttributeSet; |
| | | import android.util.Log; |
| | | import android.view.MotionEvent; |
| | | import android.view.View; |
| | | |
| | | import androidx.annotation.ColorInt; |
| | | import androidx.annotation.Nullable; |
| | | |
| | | import com.runt.open.mvvm.R; |
| | | import com.runt.open.mvvm.util.DimensionUtils; |
| | | |
| | |
| | | String titleText,rightText; |
| | | @ColorInt int titleColor,rightTextColor; |
| | | float titleSize,rightTextSize,rightPadding; |
| | | Paint textPaint,rightTextPaint,drawPaint; |
| | | Paint textPaint,rightTextPaint; |
| | | int viewWidth,viewHeight; |
| | | OnClickListener leftClick,rightClick; |
| | | int touchStartX,touchStartY; |
| | |
| | | if(rightTint != -1) { |
| | | setTint(rightDra,rightTint); |
| | | } |
| | | textPaint = new Paint(); |
| | | textPaint.setAntiAlias(true); // 是否抗锯齿 |
| | | |
| | | textPaint = initPaint(titleColor,titleSize,4); |
| | | |
| | | rightTextPaint = initPaint(rightTextColor,rightTextSize,4); |
| | | |
| | | } |
| | | |
| | | private Paint initPaint(int color,float size,int stroke){ |
| | | Paint paint = new Paint(); |
| | | paint.setAntiAlias(true); // 是否抗锯齿 |
| | | //mTextPaint.setAlpha(50); // 设置alpha不透明度,范围为0~255 |
| | | textPaint.setColor(titleColor); |
| | | textPaint.setTextSize(titleSize); |
| | | paint.setColor(color); |
| | | paint.setTextSize(size); |
| | | // 设置画笔属性 |
| | | textPaint.setStyle(Paint.Style.FILL);//画笔属性是实心圆 |
| | | paint.setStyle(Paint.Style.FILL);//画笔属性是实心圆 |
| | | // paint.setStyle(Paint.Style.STROKE);//画笔属性是空心圆 |
| | | textPaint.setStrokeWidth(4);//设置画笔粗细 |
| | | |
| | | |
| | | rightTextPaint = new Paint(); |
| | | rightTextPaint.setAntiAlias(true); // 是否抗锯齿 |
| | | //mTextPaint.setAlpha(50); // 设置alpha不透明度,范围为0~255 |
| | | rightTextPaint.setColor(rightTextColor); |
| | | rightTextPaint.setTextSize(rightTextSize); |
| | | // 设置画笔属性 |
| | | rightTextPaint.setStyle(Paint.Style.FILL);//画笔属性是实心圆 |
| | | // paint.setStyle(Paint.Style.STROKE);//画笔属性是空心圆 |
| | | rightTextPaint.setStrokeWidth(4);//设置画笔粗细 |
| | | |
| | | paint.setStrokeWidth(stroke);//设置画笔粗细 |
| | | return paint; |
| | | } |
| | | |
| | | @Override |
| | |
| | | super.onDraw(canvas); |
| | | Log.e("TitleBarView","onDraw mRect:"+mRect); |
| | | if(leftDra != null){ |
| | | final Bitmap bitmap = ((BitmapDrawable) leftDra).getBitmap(); |
| | | float top = mRect.top+(viewHeight-bitmap.getHeight()*2f)/2; |
| | | float left = mRect.left-bitmap.getWidth()*0.5f; |
| | | leftClickRect = new RectF(left,top,left + (bitmap.getWidth()*2f),top + bitmap.getHeight()*2f); |
| | | canvas.drawBitmap(bitmap,mRect.left,mRect.top+(viewHeight-bitmap.getHeight())/2,null); |
| | | //点击的区域 |
| | | leftClickRect = drawBitmap(leftDra,canvas,getPaddingLeft()); |
| | | |
| | | } |
| | | if(rightDra != null){ |
| | | final Bitmap bitmap = ((BitmapDrawable) rightDra).getBitmap(); |
| | | float chaTop = (viewHeight-bitmap.getHeight())/2; |
| | | float top = mRect.top+chaTop; |
| | | float left = mRect.right-bitmap.getWidth(); |
| | | rightClickRect = new RectF(left-bitmap.getWidth()/2,top-bitmap.getHeight()/2,left+bitmap.getWidth()*2f,top+bitmap.getHeight()*1.5f); |
| | | canvas.drawBitmap(bitmap,left,top,null); |
| | | //点击的区域 |
| | | rightClickRect = drawBitmap(rightDra,canvas,getPaddingRight()+viewWidth); |
| | | } |
| | | if(titleText != null){ |
| | | final int textWidth = getTextWidth(textPaint, titleText); |
| | | final int textHeight = getTextHeight(textPaint); |
| | | final Paint.FontMetrics fontMetrics = textPaint.getFontMetrics(); |
| | | canvas.drawText(titleText,mRect.left+((viewWidth-textWidth)/2),mRect.top+((viewHeight-textHeight)/2)+(0-fontMetrics.ascent),textPaint); |
| | | final float[] textHeight = getTextHeight(textPaint); |
| | | float top = (getHeight()-textHeight[0])/2-textPaint.getFontMetrics().ascent; |
| | | canvas.drawText(titleText,mRect.left+((viewWidth-textWidth)/2),top,textPaint); |
| | | } |
| | | if(rightText != null){ |
| | | final int textWidth = getTextWidth(rightTextPaint, rightText); |
| | | final int textHeight = getTextHeight(rightTextPaint); |
| | | final Paint.FontMetrics fontMetrics = rightTextPaint.getFontMetrics(); |
| | | final float[] textHeight = getTextHeight(rightTextPaint); |
| | | float chaleft = textWidth*0.5f; |
| | | float chaTop = textHeight[0]*0.5f; |
| | | float left = mRect.right-textWidth; |
| | | float top = mRect.top+((viewHeight-textHeight)/2)+(0-fontMetrics.ascent); |
| | | rightClickRect = new RectF(left-textWidth/2,top-textHeight,left+textWidth*2f,top+textHeight/2); |
| | | float top = (getHeight()-textHeight[0])/2-textPaint.getFontMetrics().ascent; |
| | | rightClickRect = new RectF(left-chaleft,top-chaTop,left +chaleft*3,top + chaTop*3); |
| | | canvas.drawText(rightText,left,top,rightTextPaint); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 绘制图片 |
| | | * @param drawable |
| | | * @param canvas |
| | | * @return 点击的区域 |
| | | */ |
| | | private RectF drawBitmap(Drawable drawable,Canvas canvas,float left){ |
| | | final Bitmap bitmap = drawableToBitmap(drawable); |
| | | if(left>viewWidth){//右侧 |
| | | left -= bitmap.getWidth(); |
| | | } |
| | | float chaTop = bitmap.getHeight()*0.5f; |
| | | float chaleft = bitmap.getWidth()*0.5f; |
| | | //绘制的位置 |
| | | float top = (getHeight()-bitmap.getHeight())/2; |
| | | canvas.drawBitmap(bitmap,left,top,null); |
| | | return new RectF(left-chaleft,top-chaTop,left +chaleft*3,top + chaTop*3); |
| | | } |
| | | @Override |
| | | public boolean onTouchEvent(MotionEvent event) { |
| | |
| | | } |
| | | |
| | | //第二个参数是一个数组.传进去个长度跟字符串长度相同的float数组,方法调用后,里边塞的是每个字符的长度. |
| | | public int getTextHeight(Paint paint) { |
| | | public float[] getTextHeight(Paint paint) { |
| | | Paint.FontMetrics fm = paint.getFontMetrics(); |
| | | float height1 = fm.descent - fm.ascent;//文字的高度 |
| | | float height2 = fm.bottom - fm.top + fm.leading;//行高 |
| | | return (int) height2; |
| | | return new float[]{height1,height2}; |
| | | } |
| | | |
| | | private void setTint(Drawable drawable, @ColorInt int color){ |
| | |
| | | drawable.setTint(color); |
| | | } |
| | | } |
| | | |
| | | |
| | | private Bitmap drawableToBitmap(Drawable drawable){ |
| | | int w = drawable.getIntrinsicWidth(); |
| | | int h = drawable.getIntrinsicHeight(); |
| | | Bitmap.Config config = |
| | | drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 |
| | | : Bitmap.Config.RGB_565; |
| | | Bitmap bitmap = Bitmap.createBitmap(w,h,config); |
| | | //注意,下面三行代码要用到,否在在View或者surfaceview里的canvas.drawBitmap会看不到图 |
| | | Canvas canvas = new Canvas(bitmap); |
| | | drawable.setBounds(0, 0, w, h); |
| | | drawable.draw(canvas); |
| | | return bitmap; |
| | | } |
| | | } |