1. 程式人生 > >android 帶圓角的View(原創)

android 帶圓角的View(原創)

importandroid.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes
; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.util.Log; import android.view.View; import com.android_rrhy.Activity.R; /** * Created by fanqibo on 2018/2/2. */ public class AngleView extends View { private Paint paint = new Paint(); //顏色 private int
bgColor = Color.YELLOW; //百分比 private float proportion = 1f; //高度 private int bgHeight = 0; public AngleView(Context context) { super(context); } public AngleView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public AngleView(Context context, AttributeSet attrs, int
defStyle) { super(context, attrs, defStyle); @SuppressLint("Recycle") TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.AngleView, defStyle, 0); try { bgColor = a.getColor(R.styleable.AngleView_bg_color, bgColor); proportion = a.getFloat(R.styleable.AngleView_percentage, proportion); bgHeight = a.getDimensionPixelSize(R.styleable.AngleView_bg_height, (int) dp2px(8)); } finally { a.recycle(); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(widthMeasureSpec, measureHeight(heightMeasureSpec)); } private int measureHeight(int measureSpec) { int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); int result = (int) (bgHeight + getPaddingTop() + getPaddingBottom()); if (specMode == MeasureSpec.AT_MOST) { result = (int) (bgHeight + getPaddingTop() + getPaddingBottom()); } else if (specMode == MeasureSpec.EXACTLY) { result = specSize; } return result; } public void setBgHeight(float bgHeight) { if (bgHeight == this.bgHeight) { return; } invalidate(); } public void setPercentage(float percentage) { if (percentage == this.proportion) { return; } if (percentage >= 0) { this.proportion = Math.min(percentage, 1f); invalidate(); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint.setStrokeWidth(bgHeight); paint.setAntiAlias(true); paint.setColor(bgColor); paint.setStyle(Paint.Style.STROKE); paint.setStrokeCap(Paint.Cap.ROUND); canvas.drawLine(getPaddingLeft() + bgHeight / 2, getPaddingTop() + bgHeight / 2, proportion * (getPaddingRight() + getWidth() - bgHeight / 2), getPaddingTop() + bgHeight / 2, paint); } public float dp2px(float dp) { return (getContext().getResources().getDisplayMetrics().density * dp + 0.5f); } public void setBgColor(@ColorInt int bgColor) { if (bgColor == this.bgColor) { return; } bgColor = bgColor; paint.setColor(bgColor); invalidate(); } @Deprecated public void setBgColorResource(@ColorRes int borderColorRes) { setBgColor(getContext().getResources().getColor(borderColorRes)); } }
attr
<declare-styleable name="AngleView">
    <attr name="bg_color" format="color" />
    <attr name="bg_height" format="dimension" />
    <attr name="percentage" format="float"/>
</declare-styleable>

用法:
<com.android_rrhy.Activity.statistics.view.AngleView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:bg_height="4dp"
app:percentage="0.12"
app:bg_color="@color/red" />

相關推薦

android 圓角View原創

importandroid.annotation.SuppressLint; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Can

Android自定義View-Draw原理篇

Android自定義View通常需要經過measure、layout和draw過程。 如果你沒有了解過measure過程,可以先看看這篇文章。 如果你沒有了解過layout過程,可以先看看這篇文章。 一、draw的作用:繪製View檢視 二、draw過程:類似meas

Android 自定義View

前言:可是有時候我們總感覺官方定義的一些基本元件不夠用,自定義元件就不可避免了。那麼如何才能做到像官方提供的那些元件一樣用xml來定義他的屬性呢? 先總結下自定義View的步驟: 1、自定義View的屬性; 2、在View的構造方法中獲得自定義的屬性。 一、在re

android自定義View、正弦波水波紋

文章目錄 1、正弦曲線知識 2、靜態正弦曲線繪製 3、動態正弦曲線繪製 4、完整原始碼 1、正弦曲線知識 對這個初中知識遺忘了的可以先看看正弦曲線百度百科詞條方便加深理解。

Android自定義view,打造絢麗的驗證碼

前言:我相信信念的力量,信念可以支撐起一個人,一個名族,一個國家。正如“人沒有夢想和鹹魚有什麼區別”一樣,我有信念,有理想,所以我正在努力向著夢想前進~。 自定義view,如果是我,我首先要看到自定義view的效果圖,然後再想想怎麼實現這種效果或功能,所以先貼

android自定義View2:實現百分比自適應佈局

android介面適配難是歷史原因,我們只能想辦法解決。github上面已有一些佈局自適應的解決方案,今天我分享的是自定義控制元件:RelativieLayout自適應百分比寬高。直接上菜。 一,實現的效果圖 眼見為實,截圖所示,寬高都是50%,實現了自

android 自定義View完美

原文連結:http://www.jianshu.com/p/d507e3514b65 一、自定義View,你真的掌握了嗎? 什麼?你說你掌握了自定義View?來來來,回答老衲如下問題: Google提出View這個概念的目的是什麼?View這個概念與Activtiy、F

android 基礎知識View 滑動衝突攔截和原理

自定義View滑動衝突現象:  第一種是同向,第二種為異向,第三種為前兩種的組合模式 滑動衝突解決方案: 首先決定x和y移動方向的長度來決定是x還是y的移動 第一外部攔截法: @Override public boolean onI

Android 自定義View基礎

一:引言 Android的開發中,在移動裝置上展示的所有內容,都是靠一個一個具體的檢視控制元件,按照一定的排列規則展示出來的,這些一個個控制元件,都是系統提供給我們的。但是我們看到,app商店上有些比較炫酷的頁面展示,我們會發現,系統根本沒有提供那些控制元件,

Android 自定義View,點,線的繪製

public class PointLine extends View { Paint mLinePaint; Paint mPointPaint; float width; float height; float pointAddress

Android學習—簡單自定義View

最近手上不忙所以回顧了一下自己今年來所接觸和學習的東西,突然覺得寫部落格真是一個很好的方式,希望自己 可以堅持下去。 自定義View的流程 建立自定義類繼承View,並重寫構造方法,構造方法總共有四種,我們暫時只需要繼承前兩種 public CircleVi

Android應用程式視窗Activity的檢視物件View的建立過程分析

       從前文可知道,每一個Activity元件都有一個關聯的Window物件,用來描述一個應用程式視窗。每一個應用程式視窗內部又包含有一個View物件,用來描述應用程式視窗的檢視。應用程式視窗檢視是真正用來實現UI內容和佈局的,也就是說,每一個Activity元件的U

Android 自定義Viewinflate()模式

1.建立LayoutInflater例項 有兩種方式 1. LayoutInflater layoutInflater= LayoutInflater.from(MainActivity.this); LayoutInflater layoutInfla

Android封裝之PopupWindow (demo)

一、前言 Android的對話方塊有兩種:PopupWindow和AlertDialog。它們的不同點在於: AlertDialog的位置固定,但是PopupWindow的位置可以隨意, AlertDialog是非阻塞執行緒的,而PopupWindow是阻

原創談一點新手使用Android studio時遇見的各種小問題

如果你用慣了Eclipse進行Android開發的話,一時間恐怕難以適應Android studio,不怕!這就來教你一些Android studio使用冷知識。 1:R檔案異常,這個恐怕是所有新手開發

Android程序保活最新你淺析這幾種可行性的保活方案

1.概述   據前人驗證,在沒有白名單的情況下,安卓系統要做一個任何情況下都不被殺死的應用是基本不可能的,但是我們可以做到應用基本不被殺死,如果殺死可以立即復活.經過上網查詢,程序常駐的方案眾說紛紜,但是很多的方案都是不靠譜的或不是最好的,結合很多資料,今天總結一下And

Android View---基礎

吐槽 這幾天晚上老睡不著,然後只能晚上聽極客時間上老師的課程,聽著聽著就越來越興奮了233,以後晚上11點半就要熄燈了,自己也要早點睡覺,然後早上起床早點哈,控制好生物鐘。 本文思維導圖 主要是是看《安卓藝術開發》第三章的學習筆記 好好把安卓的view的基礎知識

Android自定義View二、深入解析自定義屬性

目錄: 繼承View,覆蓋構造方法 自定義屬性 重寫onMeasure方法測量寬高 重寫onDraw方法繪製控制元件   接下來幾篇部落格分別深入學習每一個步驟的知識點,第一步就不用多講了,這篇部落格我們看一

Android進階之自定義View1實現可換行的TextView

         今天來一起學習一下最簡單的自定義view,自己動手寫一個MyTextView,當然不會像系統的TextView那麼複雜,只是實現一下TextView的簡單功能,包括分行顯示及自定義屬性的處理,主要目的是介紹自定義view的實現的基本思路和需要掌握的一些基礎知

原創android PowerManager分析十分詳細

http://www.myexception.cn/android/2076061.html (原創)android PowerManager分析(非常詳細) 概述 一直以來,電源管理是電子產品設計中非常重要的環節,也是任何電子裝置中最為重要的系統模