带标记的ImageView,图片可以设置成圆角

这个功能可以很方便的为一张图片设置一个标记,这在促销的时候很是有效 

Xml代码   收藏代码
  1. <com.mb.bgfitting.view.SimpleTagImageView  
  2.         xmlns:app="http://schemas.android.com/apk/res-auto"  
  3.         android:id="@+id/item_0"  
  4.         android:layout_width="80dp"  
  5.         android:layout_height="80dp"   
  6.         android:contentDescription="@null"  
  7.         app:simple_tag_textSize="@dimen/font_middle"  
  8.         app:simple_corner_distance="20dp"  
  9.         app:simple_tag_text="50%off"  
  10.         app:simple_tag_background_color="#AA27CDC0"  
  11.         app:simple_tag_orientation="left_top"  
  12.         app:simple_tag_width="16dip"  
  13.         app:simple_tag_textColor="@android:color/white"  
  14.         app:simple_tag_enable="true"  
  15.         app:simple_tag_round_radius="8dp"  
  16.         />  


源码: 
Java代码   收藏代码
  1. import android.content.Context;  
  2. import android.content.res.TypedArray;  
  3. import android.graphics.Bitmap;  
  4. import android.graphics.BitmapShader;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Matrix;  
  7. import android.graphics.Paint;  
  8. import android.graphics.Path;  
  9. import android.graphics.Rect;  
  10. import android.graphics.RectF;  
  11. import android.graphics.Shader;  
  12. import android.graphics.drawable.BitmapDrawable;  
  13. import android.graphics.drawable.Drawable;  
  14. import android.text.TextUtils;  
  15. import android.util.AttributeSet;  
  16. import android.util.Log;  
  17. import android.widget.ImageView;  
  18.   
  19. public class SimpleTagImageView extends ImageView {  
  20.   
  21.     public static final String TAG = "SimpleTagImageView";  
  22.   
  23.     public  static final byte LEFT_TOP = 0x00;  
  24.   
  25.     public  static final byte RIGHT_TOP = 0x01;  
  26.   
  27.     public  static final byte LEFT_BOTTOM = 0x02;  
  28.   
  29.     public  static final byte RIGHT_BOTTOM = 0x03;  
  30.   
  31.     private static final float THE_SQUARE_ROOT_OF_2 = (float) Math.sqrt(2);  
  32.   
  33.     private static final int DEFAULT_TAG_WIDTH = 20;  
  34.   
  35.     private static final int DEFAULT_CORNER_DISTANCE = 20;  
  36.   
  37.     private static final int DEFAULT_TAG_BACKGROUND_COLOR = 0x9F27CDC0;  
  38.   
  39.     private static final int DEFAULT_TAG_TEXT_SIZE = 15;  
  40.   
  41.     private static final int DEFAULT_TAG_TEXT_COLOR = 0xFFFFFFFF;  
  42.   
  43.     private float mCornerDistance;  
  44.   
  45.     private float mTagWidth;  
  46.   
  47.     private int mTagBackgroundColor;  
  48.   
  49.     private Path mPath;  
  50.   
  51.     private Paint mPaint;  
  52.   
  53.     private String mTagText;  
  54.   
  55.     private int mTagTextSize;  
  56.   
  57.     private Paint mTextPaint;  
  58.   
  59.     private Rect mTagTextBound;  
  60.   
  61.     private int mTagTextColor;  
  62.   
  63.     private float mDensity;  
  64.   
  65.     private int mTagOrientation;  
  66.   
  67.     private MyPoint startPoint;  
  68.   
  69.     private MyPoint endPoint;  
  70.   
  71.     private Paint mBitmapPaint;  
  72.   
  73.     private RectF mRoundRect;  
  74.   
  75.     private boolean mTagEnable;  
  76.   
  77.     private int mRoundRadius;  
  78.   
  79.     public SimpleTagImageView(Context context) {  
  80.         this(context, null);  
  81.     }  
  82.   
  83.     public SimpleTagImageView(Context context, AttributeSet attrs) {  
  84.         this(context, attrs, 0);  
  85.     }  
  86.   
  87.     public SimpleTagImageView(Context context, AttributeSet attrs, int defStyleAttr) {  
  88.         super(context, attrs, defStyleAttr);  
  89.         mDensity = context.getResources().getDisplayMetrics().density;  
  90.         TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.SimpleTagImageView,defStyleAttr,0);  
  91.         mTagOrientation = a.getInteger(R.styleable.SimpleTagImageView_simple_tag_orientation,0);  
  92.         mTagWidth = a.getDimensionPixelSize(R.styleable.SimpleTagImageView_simple_tag_width, dip2px(DEFAULT_TAG_WIDTH));  
  93.         mCornerDistance = a.getDimensionPixelSize(R.styleable.SimpleTagImageView_simple_corner_distance,dip2px(DEFAULT_CORNER_DISTANCE));  
  94.         mTagBackgroundColor = a.getColor(R.styleable.SimpleTagImageView_simple_tag_background_color,DEFAULT_TAG_BACKGROUND_COLOR);  
  95.         mTagText = a.getString(R.styleable.SimpleTagImageView_simple_tag_text);  
  96.         mTagTextSize = a.getDimensionPixelSize(R.styleable.SimpleTagImageView_simple_tag_textSize, dip2px(DEFAULT_TAG_TEXT_SIZE));  
  97.         mTagTextColor = a.getColor(R.styleable.SimpleTagImageView_simple_tag_textColor, DEFAULT_TAG_TEXT_COLOR);  
  98.         mTagEnable = a.getBoolean(R.styleable.SimpleTagImageView_simple_tag_enable,true);  
  99.         mRoundRadius = a.getDimensionPixelSize(R.styleable.SimpleTagImageView_simple_tag_round_radius,0);  
  100.         a.recycle();  
  101.         if(TextUtils.isEmpty(mTagText))mTagText = "";  
  102.         mPaint = new Paint();  
  103.         mPath = new Path();  
  104.         mTextPaint = new Paint();  
  105.         mTagTextBound = new Rect();  
  106.         startPoint = new MyPoint();  
  107.         endPoint = new MyPoint();  
  108.         mRoundRect = new RectF();  
  109.     }  
  110.   
  111.     /** 
  112.      * 
  113.      * @param textSize unit:dip 
  114.      */  
  115.     public void setTagTextSize(int textSize) {  
  116.         this.mTagTextSize = dip2px(textSize);  
  117.         invalidate();  
  118.     }  
  119.   
  120.     public int getTagTextSize(){  
  121.         return mTagTextSize;  
  122.     }  
  123.   
  124.     /** 
  125.      * 
  126.      * @param cornerDistance unit:dip 
  127.      */  
  128.     public void setCornerDistance(int cornerDistance) {  
  129.         if(this.mCornerDistance == cornerDistance)return;  
  130.         this.mCornerDistance = dip2px(cornerDistance);  
  131.         invalidate();  
  132.     }  
  133.   
  134.     /** 
  135.      * 
  136.      * @return unit:dip 
  137.      */  
  138.     public int getCornerDistance() {  
  139.         return px2dip(this.mCornerDistance);  
  140.     }  
  141.   
  142.     public int getTagTextColor() {  
  143.         return this.mTagTextColor;  
  144.     }  
  145.   
  146.     public void setTagTextColor(int tagTextColor) {  
  147.         if(this.mTagTextColor == tagTextColor)return;  
  148.         this.mTagTextColor = tagTextColor;  
  149.         invalidate();  
  150.     }  
  151.   
  152.     public String getTagText() {  
  153.         return this.mTagText;  
  154.     }  
  155.   
  156.     public void setTagText(String tagText){  
  157.         if(tagText.equals(this.mTagText))return;  
  158.         this.mTagText = tagText;  
  159.         invalidate();  
  160.     }  
  161.   
  162.     public void setTagBackgroundColor(int tagBackgroundColor) {  
  163.         if(this.mTagBackgroundColor == tagBackgroundColor)return;  
  164.         this.mTagBackgroundColor = tagBackgroundColor;  
  165.         invalidate();  
  166.     }  
  167.   
  168.     public int getTagBackgroundColor() {  
  169.         return this.mTagBackgroundColor;  
  170.     }  
  171.   
  172.     /** 
  173.      * @return unit:dip 
  174.      */  
  175.     public int getTagWidth() {  
  176.         return px2dip(this.mTagWidth);  
  177.     }  
  178.   
  179.     /** 
  180.      * 
  181.      * @param tagWidth unit:dip 
  182.      */  
  183.     public void setTagWidth(int tagWidth) {  
  184.         this.mTagWidth = dip2px(tagWidth);  
  185.         invalidate();  
  186.     }  
  187.   
  188.     /** 
  189.      * @return  0 : left_top 
  190.      *          1 : right_top 
  191.      *          2 : left_bottom 
  192.      *          3 : right_bottom 
  193.      */  
  194.     public int getTagOrientation() {  
  195.         return mTagOrientation;  
  196.     }  
  197.   
  198.     /** 
  199.      * 
  200.      * @param tagOrientation {@link #LEFT_TOP} or 
  201.      *                       {@link #LEFT_BOTTOM} or 
  202.      *                       {@link #RIGHT_TOP} or 
  203.      *                       {@link #RIGHT_BOTTOM} 
  204.      */  
  205.     public void setTagOrientation(int tagOrientation) {  
  206.         if(tagOrientation == this.mTagOrientation)return;  
  207.         this.mTagOrientation = tagOrientation;  
  208.         invalidate();  
  209.     }  
  210.   
  211.     public void setTagEnable(boolean tagEnable) {  
  212.         if(this.mTagEnable == tagEnable) return ;  
  213.         this.mTagEnable = tagEnable;  
  214.         invalidate();  
  215.     }  
  216.   
  217.     public boolean getTagEnable() {  
  218.         return this.mTagEnable;  
  219.     }  
  220.   
  221.     public  int getTagRoundRadius() {  
  222.         return this.mRoundRadius;  
  223.     }  
  224.   
  225.     public void setTagRoundRadius(int roundRadius) {  
  226.         if(this.mRoundRadius == roundRadius) return;  
  227.         this.mRoundRadius = roundRadius;  
  228.         invalidate();  
  229.     }  
  230.   
  231.     @Override  
  232.     protected void onDraw(@SuppressWarnings("NullableProblems") Canvas mCanvas) {  
  233.         if(mRoundRadius == 0) {  
  234.             super.onDraw(mCanvas);  
  235.         }else {  
  236.             Drawable d = getDrawable();  
  237.             if(d == nullreturn;  
  238.             if(d.getIntrinsicWidth() == 0 || d.getIntrinsicHeight() == 0return;  
  239.             setupBitmapPaint();  
  240.             mRoundRect.set(getPaddingLeft(),getPaddingTop(),getMeasuredWidth() - getPaddingRight(),getMeasuredHeight() - getPaddingBottom());  
  241.             mCanvas.drawRoundRect(mRoundRect, mRoundRadius, mRoundRadius, mBitmapPaint);  
  242.         }  
  243.   
  244.         if(mTagWidth > 0 && mTagEnable) {  
  245.             float rDistance = mCornerDistance + mTagWidth/2;  
  246.             chooseTagOrientation(rDistance);  
  247.             mTextPaint.setTextSize(mTagTextSize);  
  248.             mTextPaint.getTextBounds(mTagText,0,mTagText.length(),mTagTextBound);  
  249.             mPaint.setDither(true);  
  250.             mPaint.setAntiAlias(true);  
  251.             mPaint.setColor(mTagBackgroundColor);  
  252.             mPaint.setStyle(Paint.Style.STROKE);  
  253.             mPaint.setStrokeJoin(Paint.Join.ROUND);  
  254.             mPaint.setStrokeCap(Paint.Cap.SQUARE);  
  255.             mPaint.setStrokeWidth(mTagWidth);  
  256.             mPath.reset();  
  257.             mPath.moveTo(startPoint.x, startPoint.y);  
  258.             mPath.lineTo(endPoint.x, endPoint.y);  
  259.             mCanvas.drawPath(mPath, mPaint);  
  260.             mTextPaint.setColor(mTagTextColor);  
  261.             mTextPaint.setTextSize(mTagTextSize);  
  262.             mTextPaint.setAntiAlias(true);  
  263. //          斜边长度  
  264.             float hypotenuse = THE_SQUARE_ROOT_OF_2 * rDistance;  
  265.             mCanvas.drawTextOnPath(mTagText, mPath, hypotenuse / 2 - mTagTextBound.width() / 2,  
  266.                     mTagTextBound.height() / 2, mTextPaint);  
  267.         }  
  268.     }  
  269.   
  270.     private void chooseTagOrientation(float rDistance) {  
  271.         int mWidth = getMeasuredWidth();  
  272.         int mHeight = getMeasuredHeight();  
  273.         switch (mTagOrientation) {  
  274.             case 0:  
  275.                 startPoint.x = 0;  
  276.                 startPoint.y = rDistance;  
  277.                 endPoint.x = rDistance;  
  278.                 endPoint.y = 0;  
  279.                 break;  
  280.             case 1:  
  281.                 startPoint.x = mWidth - rDistance;  
  282.                 startPoint.y = 0;  
  283.                 endPoint.x = mWidth;  
  284.                 endPoint.y = rDistance;  
  285.                 break;  
  286.             case 2:  
  287.                 startPoint.x = 0;  
  288.                 startPoint.y = mHeight - rDistance;  
  289.                 endPoint.x = rDistance;  
  290.                 endPoint.y = mHeight;  
  291.                 break;  
  292.             case 3:  
  293.                 startPoint.x = mWidth - rDistance;  
  294.                 startPoint.y = mHeight;  
  295.                 endPoint.x = mWidth;  
  296.                 endPoint.y = mHeight - rDistance;  
  297.                 break;  
  298.         }  
  299.     }  
  300.   
  301.     private void setupBitmapPaint() {  
  302.         Drawable drawable = getDrawable();  
  303.         if (drawable == null) {  
  304.             return;  
  305.         }  
  306.         Bitmap mBitmap = drawableToBitmap(drawable);  
  307.         BitmapShader mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);  
  308.         if(getScaleType() != ScaleType.FIT_XY){  
  309.             Log.w(TAG,String.format("Now scale type just support fitXY,other type invalid"));  
  310.         }  
  311.         //now scale type just support fitXY  
  312.         //todo support all scale type  
  313.         Matrix mMatrix = new Matrix();  
  314.         mMatrix.setScale(getWidth() * 1.0f / mBitmap.getWidth(), getHeight() * 1.0f / mBitmap.getHeight());  
  315.         mBitmapShader.setLocalMatrix(mMatrix);  
  316.         if(mBitmapPaint == null) {  
  317.             mBitmapPaint = new Paint();  
  318.             mBitmapPaint.setDither(false);  
  319.             mBitmapPaint.setAntiAlias(true);  
  320.             mBitmapPaint.setShader(mBitmapShader);  
  321.         }  
  322.     }  
  323.   
  324.     private int dip2px(int dip) {  
  325.         return (int)(mDensity * dip + 0.5f);  
  326.     }  
  327.   
  328.     private int px2dip(float px) {  
  329.         return (int)(px/mDensity + 0.5f);  
  330.     }  
  331.   
  332.     private Bitmap drawableToBitmap(Drawable drawable) {  
  333.         if (drawable instanceof BitmapDrawable) {  
  334.             BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;  
  335.             return bitmapDrawable.getBitmap();  
  336.         }  
  337.         int w = drawable.getIntrinsicWidth();  
  338.         int h = drawable.getIntrinsicHeight();  
  339.         Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);  
  340.         Canvas canvas = new Canvas(bitmap);  
  341.         drawable.setBounds(00, w, h);  
  342.         drawable.draw(canvas);  
  343.         return bitmap;  
  344.     }  
  345.   
  346.     static class MyPoint {  
  347.         float x;  
  348.         float y;  
  349.     }  
  350. }  


属性: 
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="SimpleTagImageView">  
  4.         <attr name="simple_corner_distance" format="dimension" />  
  5.         <attr name="simple_tag_width" format="dimension"/>  
  6.         <attr name="simple_tag_background_color" format="color"/>  
  7.         <attr name="simple_tag_text" format="string"/>  
  8.         <attr name="simple_tag_textSize" format="dimension" />  
  9.         <attr name="simple_tag_textColor" format="color"/>  
  10.         <attr name="simple_tag_orientation" format="enum">  
  11.             <enum name="left_top" value="0"/>  
  12.             <enum name="right_top" value="1"/>  
  13.             <enum name="left_bottom" value="2"/>  
  14.             <enum name="right_bottom" value="3"/>  
  15.         </attr>  
  16.         <attr name="simple_tag_enable" format="boolean"/>  
  17.         <attr name="simple_tag_round_radius" format="dimension"/>  
  18.     </declare-styleable>  
  19. </resources>  

一个为ImageView添加徽章,标记(badge)的库 
http://www.jcodecraeer.com/a/opensource/2015/1125/3720.html  
Android标签控件 
https://github.com/H07000223/FlycoLabelView

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值