Android 貼紙樣式標籤
樣式效果
實現方法:
1、自定義標籤類
public class LabelImageView extends ImageView { LabelViewHelper utils; public LabelImageView(Context context) { this(context, null); } public LabelImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public LabelImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); utils = new LabelViewHelper(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); utils.onDraw(canvas, getMeasuredWidth(), getMeasuredHeight()); } public void setLabelText(String text) { utils.setLabelText(this, text); } }
2、部分工具類
public class LabelViewHelper { private static final int LEFT_TOP = 1; private static final int RIGHT_TOP = 2; private static final int LEFT_BOTTOM = 3; private static final int RIGHT_BOTTOM = 4; private static final int DEFAULT_DISTANCE = 40; private static final int DEFAULT_HEIGHT = 20; private static final int DEFAULT_STROKE_WIDTH = 1; private static final int DEFAULT_TEXT_SIZE = 14; private static final int DEFAULT_BACKGROUND_COLOR = 0x9F27CDC0; private static final int DEFAULT_STROKE_COLOR = 0xFFFFFFFF; private static final int DEFAULT_TEXT_COLOR = 0xFFFFFFFF; private static final int DEFAULT_ORIENTATION = LEFT_TOP; private static final int DEFAULT_TEXT_STYLE = 0; private int distance; private int height; private int strokeWidth; private String text; private int backgroundColor; private int strokeColor; private int textSize; private int textStyle; private int textColor; private boolean visual; private int orientation; // private float startPosX; // private float startPosY; // private float endPosX; // private float endPosY; private Paint rectPaint; private Paint rectStrokePaint; // simulator private Path rectPath; private Path textPath; private Paint textPaint; private Rect textBound; private Context context; private int alpha; public LabelViewHelper(Context context, AttributeSet attrs, int defStyleAttr) { this.context = context; TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.LabelView, defStyleAttr, 0); distance = attributes.getDimensionPixelSize(R.styleable.LabelView_label_distance, dip2Px(DEFAULT_DISTANCE)); height = attributes.getDimensionPixelSize(R.styleable.LabelView_label_height, dip2Px(DEFAULT_HEIGHT)); strokeWidth = attributes.getDimensionPixelSize(R.styleable.LabelView_label_strokeWidth, dip2Px(DEFAULT_STROKE_WIDTH)); text = attributes.getString(R.styleable.LabelView_label_text); backgroundColor = attributes.getColor(R.styleable.LabelView_label_backgroundColor, DEFAULT_BACKGROUND_COLOR); strokeColor = attributes.getColor(R.styleable.LabelView_label_strokeColor, DEFAULT_STROKE_COLOR); textSize = attributes.getDimensionPixelSize(R.styleable.LabelView_label_textSize, dip2Px(DEFAULT_TEXT_SIZE)); textStyle = attributes.getInt(R.styleable.LabelView_label_textStyle, DEFAULT_TEXT_STYLE); textColor = attributes.getColor(R.styleable.LabelView_label_textColor, DEFAULT_TEXT_COLOR); visual = attributes.getBoolean(R.styleable.LabelView_label_visual, true); orientation = attributes.getInteger(R.styleable.LabelView_label_orientation, DEFAULT_ORIENTATION); attributes.recycle(); rectPaint = new Paint(); rectPaint.setDither(true); rectPaint.setAntiAlias(true); rectPaint.setStyle(Paint.Style.FILL); rectStrokePaint = new Paint(); rectStrokePaint.setDither(true); rectStrokePaint.setAntiAlias(true); rectStrokePaint.setStyle(Paint.Style.STROKE); rectPath = new Path(); rectPath.reset(); textPath = new Path(); textPath.reset(); textPaint = new Paint(); textPaint.setDither(true); textPaint.setAntiAlias(true); textPaint.setStrokeJoin(Paint.Join.ROUND); textPaint.setStrokeCap(Paint.Cap.SQUARE); textBound = new Rect(); } public void onDraw(Canvas canvas, int measuredWidth, int measuredHeight) { if (!visual || text == null) { return; } float actualDistance = distance + height / 2; calcOffset(measuredWidth, measuredHeight); rectPaint.setColor(backgroundColor); if (alpha != 0) { rectPaint.setAlpha(alpha); } rectStrokePaint.setColor(strokeColor); rectStrokePaint.setStrokeWidth(strokeWidth); canvas.drawPath(rectPath, rectPaint); canvas.drawPath(rectPath, rectStrokePaint); textPaint.setTextSize(textSize); textPaint.setColor(context.getResources().getColor(R.color.white)); textPaint.getTextBounds(text, 0, text.length(), textBound); textPaint.setTypeface(Typeface.defaultFromStyle(textStyle)); float begin_w_offset = (1.4142135f * actualDistance) / 2 - textBound.width() / 2; if (begin_w_offset < 0) begin_w_offset = 0; canvas.drawTextOnPath(text, textPath, begin_w_offset, textBound.height() / 2, textPaint); } private void calcOffset(int measuredWidth, int measuredHeight) { float startPosX = measuredWidth - distance - height; float endPosX = measuredWidth; float startPosY = measuredHeight - distance - height; float endPosY = measuredHeight; float middle = height/2; switch (orientation) { case 1: // LEFT_TOP rectPath.reset(); rectPath.moveTo(0, distance); rectPath.lineTo(distance, 0); rectPath.lineTo(distance + height, 0); rectPath.lineTo(0, distance + height); rectPath.close(); textPath.reset(); textPath.moveTo(0, distance + middle); textPath.lineTo(distance + middle, 0); textPath.close(); break; case 2: // RIGHT_TOP rectPath.reset(); rectPath.moveTo(startPosX, 0); rectPath.lineTo(startPosX + height, 0); rectPath.lineTo(endPosX, distance); rectPath.lineTo(endPosX, distance + height); rectPath.close(); textPath.reset(); textPath.moveTo(startPosX + middle, 0); textPath.lineTo(endPosX, distance + middle); textPath.close(); break; case 3: // LEFT_BOTTOM rectPath.reset(); rectPath.moveTo(0, startPosY); rectPath.lineTo(distance + height, endPosY); rectPath.lineTo(distance, endPosY); rectPath.lineTo(0, startPosY + height); rectPath.close(); textPath.reset(); textPath.moveTo(0, startPosY + middle); textPath.lineTo(distance + middle, endPosY); textPath.close(); break; case 4: // RIGHT_BOTTOM rectPath.reset(); rectPath.moveTo(startPosX, endPosY); rectPath.lineTo(measuredWidth, startPosY); rectPath.lineTo(measuredWidth, startPosY + height); rectPath.lineTo(startPosX + height, endPosY); rectPath.close(); textPath.reset(); textPath.moveTo(startPosX + middle, endPosY); textPath.lineTo(endPosX, startPosY + middle); textPath.close(); break; } } private int dip2Px(float dip) { return (int) (dip * context.getResources().getDisplayMetrics().density + 0.5f); } private int px2Dip(float px) { return (int) (px / context.getResources().getDisplayMetrics().density + 0.5f); } public void setLabelHeight(View view, int height) { if (this.height != dip2Px(height)) { this.height = dip2Px(height); view.invalidate(); } } public int getLabelHeight() { return px2Dip(this.height); } public void setLabelDistance(View view, int distance) { if (this.distance != dip2Px(distance)) { this.distance = dip2Px(distance); view.invalidate(); } } public int getLabelStrokeWidth() { return px2Dip(this.strokeWidth); } public void setLabelStrokeWidth(View view, int strokeWidth) { if (this.strokeWidth != dip2Px(strokeWidth)) { this.strokeWidth = dip2Px(strokeWidth); view.invalidate(); } } public int getLabelDistance() { return px2Dip(this.distance); } public boolean isLabelVisual() { return visual; } public void setLabelVisual(View view, boolean visual) { if (this.visual != visual) { this.visual = visual; view.invalidate(); } } public int getLabelOrientation() { return orientation; } public void setLabelOrientation(View view, int orientation) { if (this.orientation != orientation && orientation <= 4 && orientation >= 1) { this.orientation = orientation; view.invalidate(); } } public int getLabelTextColor() { return textColor; } public void setLabelTextColor(View view, int textColor) { if (this.textColor != textColor) { this.textColor = textColor; view.invalidate(); } } public int getLabelBackgroundColor() { return backgroundColor; } public void setLabelBackgroundColor(View view, int backgroundColor) { if (this.backgroundColor != backgroundColor) { this.backgroundColor = backgroundColor; view.invalidate(); } } public int getLabelStrokeColor() { return strokeColor; } public void setLabelStrokeColor(View view, int strokeColor) { if (this.strokeColor != strokeColor) { this.strokeColor = strokeColor; view.invalidate(); } } public void setLabelBackgroundAlpha(View view, int alpha) { if (this.alpha != alpha) { this.alpha = alpha; view.invalidate(); } } public String getLabelText() { return text; } public void setLabelText(View view, String text) { if (this.text == null || !this.text.equals(text)) { this.text = text; view.invalidate(); } } public int getLabelTextSize() { return px2Dip(this.textSize); } public void setLabelTextSize(View view, int textSize) { if (this.textSize != textSize) { this.textSize = textSize; view.invalidate(); } } public int getLabelTextStyle(){ return textStyle; } public void setLabelTextStyle(View view, int textStyle){ if(this.textStyle == textStyle) return; this.textStyle = textStyle; view.invalidate(); } }
3、activity。layout呼叫
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:minHeight="180dp"> <ImageView android:id="@+id/text" android:layout_width="138dp" android:layout_height="100dp" android:layout_centerVertical="true" android:layout_marginLeft="8dp" android:scaleType="fitXY" android:src="@mipmap/defaul" /> <com.lid.lib.LabelImageView android:id="@+id/labelte" android:layout_width="138dp" android:layout_height="100dp" android:layout_centerVertical="true" android:layout_marginLeft="8dp" app:label_backgroundColor="#C2185B" app:label_height="30dp" app:label_orientation="LEFT_TOP" app:label_strokeWidth="0dp" app:label_text="CHINA" /> <TextView android:layout_width="200dp" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_centerVertical="true" android:text="sdfsdfsgbfwe範圍分為范文芳 分物管費為 各位為危房違法各位各位各位為個人股為為為" /> </RelativeLayout>
相關推薦
Android 貼紙樣式標籤
樣式效果 實現方法: 1、自定義標籤類 public class LabelImageView extends ImageView { LabelViewHelper utils; public LabelImageView(Contex
Android OpenGLES濾鏡開發之貼紙效果
前言 上一篇中寫到了如何實現放大眼睛的效果,這一篇實現貼紙效果,就像那個faceu相機,b612相機,還有抖音都會有的這種貼紙效果。 思路 1、貼紙肯定也是需要定位到人臉的 2、找到貼紙需要放置的位置 3、將貼紙紋理和人本身紋理進行融合 實現 人臉定位啥的,我就不說了,不
Android美顏貼紙部分原理
SurfaceTexture是從Android3.0(API 11)加入的一個新類。這個類跟SurfaceView很像,可以從camera preview或者video decode裡面獲取影象流(image stream)。但是,和SurfaceView不同的是,Surf
Android之給自定義相機增加貼紙
##前言 給自己的APP增加相機是一個不錯的功能,在我們開啟相機時,如果能動態給我們的臉上貼上標籤,或者是貼上一個卡通的眼睛,最後點選拍照,一張合成的圖片就產生了,這是不是一個好主意,下面的效果圖是我在一邊拍照一邊擺弄我的卡通貼紙,最後進行拍照,並把圖片儲存到
Android 仿摩拜貼紙的動畫
public class MainActivity extends AppCompatActivity { private MobikeView mobike_view; private SensorManager sensorManager; private Sensor sens
Android 更改按鈕樣式 Button Styles
img order href new ets com 更改 orange sin extends:http://stackoverflow.com/questions/26346727/android-material-design-button-styles
div可編輯框,去除粘貼文字樣式😄
文本 light tar target 復制 插入圖片 asc 部分 brush 上個月做了個聊天的需求(網頁版的)。說到聊天都想到輸入框,說到輸入框都會想到input,但是input標簽是不支持插入圖片的(包括areatext標簽)。查閱了一些資料就看到div標簽有一
js 將複製面板的貼上內容,樣式標籤去除
可編輯div,將js複製面板的貼上內容,樣式標籤去除 <div class='page3-emjoy5' contenteditable=='true'></div> $(".page3-emjoy5").on("paste", function (e) { t
Android 字型及樣式設定
測試應用中有關字型設定的介面: package com.gzgd.typefacetest; import android.app.Activity; import android.graphics.Typeface; import android.support.v7.app.AppComp
Android佈局屬性講解標籤屬性
RelativeLayout 第一類:屬性值為true可false android:layout_centerHrizontal 水平居中 android:layout_centerVertical &nb
狼人殺涼了,貼著AI標籤的劇本殺如何構建自己的商業版圖
文 | 魏啟揚 來源 | 智慧相對論(ID:aixdlun) “天黑請閉眼”。 這是“狼人殺”的開場臺詞,也可用來形容“狼人殺”的現狀——前景黑暗,慘不忍睹。 2017年,“狼人殺”的熱度達到頂點,無論是線下的桌遊吧,還是線
HTML文字樣式標籤
文字加粗:<b></b> 傾斜:<i></i> 加下劃線:<u></u> 加刪除線:<s></s> 以上標籤全部不推薦使用,已經被廢棄; 強調文字,表現為加粗:<strong></str
CSDN部落格文章Markdown快捷鍵樣式標籤格式
用處 讓快捷鍵有個浮現的外邊框格式, 閱讀體驗更佳 格式 <kbd>按鍵名字</kbd> 效果 Ctrl+Shift+A 編寫的時候填寫 <kbd>Ctrl</kbd>+<kbd>Shift</k
android修改checkbox樣式邊框顏色
之前寫了一個自動登入和記住密碼的功能,用的是checkbox控制元件,但是原生控制元件邊框樣式不太符合要求 如圖,我想修改成白色 在網上查了一些資料,說在style.xml檔案中定義一個樣式 <style name="My_CheckBox" parent=
Android 摺紙效果
https://github.com/Ramotion/folding-cell-android https://github.com/alexvasilkov/FoldableLayout 第二個有1600star+,所以我決定用第一個,因為第一個有xxx個 .....主要是 第一個
Android FlowLayout實現熱門標籤功能
FlowLayout實現熱門標籤的功能想必大家都見過,有的為搜尋的歷史記錄,有的則是一些推薦等等。總之熱門標籤在很多應用裡面都有使用,先看一下實現的效果圖 下面的一張是擷取的淘寶搜尋的效果 那麼我們如何實現上面的效果呢?我實現的效果是充滿屏寬狀態的,
黑白貼紙問題
有A,B,C,D,E 5個人,每個人額頭上都貼了一張黑或白的紙。5個人對坐,每個人都可以看到其他人額頭上的紙的顏色。5人相互觀察後, A說:“我看見有3人額頭上貼的是白紙,1人額頭上貼的是黑紙。” B說:“我看見其他3人額頭上貼的都是黑紙。” C說:“我看見1人額頭上貼的是白紙,
Android TextView字型樣式設定
在Android的開發中,對字型樣式的設定是必不可少的,普通的設定主要包括字型的顏色,大小。這些都太小兒科了,略過。但是當一個TextView要顯示多種樣式的時候就不一樣了。如要求其中的某幾個文字加上特殊的顏色,或者字型加大等。別的不多說,先上一個效果圖說明下:
Android定製RadioButton樣式三種實現方法
@Override public boolean onTouchEvent(MotionEvent event) { if(event.getActionMasked() == MotionEvent.ACTION_DOWN){ this.setBackgroundResource(com.wxg.
android實現不同樣式的日曆控制元件(MaterialCalendarView的改造工程)
本來以為吃透了MaterialCalendarView這個開源控制元件,為了做個考勤,心力憔悴啊!!! MaterialCalendarView 這個控制元件雖然可以自定樣式,但是樣式卻是所有的日期都是一種樣式。這個就搞的有點尷尬了!!! 我現在的需求是