Android自定義電池控制元件
一、背景
最近公司有個業務,需要自定義一個電池控制元件,因此自己按照UI圖編寫了一個自定義View。
二、效果
三、實現過程
首先看下視覺給出的UI效果圖
從這裡我們可以看得出來,要自定義這個電池View的話,分為3部分來繪製。
- 第一部分是電池頭
- 第二部分是電池邊框
- 第三部分是電池電量
3.1 自定義屬性
因此按照上面的UI效果圖來做的話,我們自定義幾個屬性。
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--自定義耗電排行電池View的自定義屬性-->
<declare-styleable name="PowerConsumptionRankingsBatteryView">
<!-- 手錶低電時候的電池顏色 -->
<attr name="batteryLowerPowerColor" format="color"/>
<!-- 手錶線上時候的電池顏色 -->
<attr name="batteryOnlineColor" format="color"/>
<!-- 手錶離線時候的電池顏色 -->
<attr name ="batteryOfflineColor" format="color"/>
<!-- 電池最大高度 -->
<attr name="batteryLevelMaxHeight" format="integer"/>
<!-- 電池寬度 -->
<attr name="batteryLevelWidth" format="integer"/>
<!-- 外殼的相關資訊 -->
<attr name="batteryShellCornerRadius" format="dimension"/>
<attr name="batteryShellWidth" format="dimension"/>
<attr name="batteryShellHeight" format="dimension"/>
<attr name="batteryShellStrokeWidth" format="dimension"/>
<!-- 電池頭的相關資訊 -->
<attr name="batteryShellHeadCornerRadius" format="dimension"/>
<attr name="batteryShellHeadWidth" format="dimension"/>
<attr name="batteryShellHeadHeight" format="dimension"/>
<!-- 電池外殼和電池等級直接的間距 -->
<attr name="batteryGap" format="dimension"/>
</declare-styleable>
</resources>
執行自定義屬性,包括電池顏色、電池最大高度、電池寬度、電池頭部的相關資訊和電池邊框的相關資訊。
colors.xml 定義了一些UI標註的顏色值
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="lowerPowerColor">#ff4d4d</color>
<color name="onlineColor">#00d68d</color>
<color name="offlineColor">#bfbfbf</color>
</resources>
dimens.xml 定義了一些UI標註的尺寸值
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<!--電池控制元件 高度-->
<dimen name="power_consumption_rankings_dimen_main_battery_view_height">48dp</dimen>
<!--電池控制元件 寬度-->
<dimen name="power_consumption_rankings_dimen_main_battery_view_width">31dp</dimen>
<!--電池外殼 厚度-->
<dimen name="power_consumption_rankings_dimen_main_battery_view_shell_stroke_width">2.5dp
</dimen>
<!--電池外殼 寬度-->
<dimen name="power_consumption_rankings_dimen_main_battery_view_shell_width">28dp</dimen>
<!--電池外殼 高度-->
<dimen name="power_consumption_rankings_dimen_main_battery_view_shell_height">42dp</dimen>
<!--電池外殼 圓角-->
<dimen name="power_consumption_rankings_dimen_main_battery_view_shell_corner">3dp</dimen>
<!--電池頭 寬度-->
<dimen name="power_consumption_rankings_dimen_main_battery_view_head_width">14dp</dimen>
<!--電池頭 高度-->
<dimen name="power_consumption_rankings_dimen_main_battery_view_head_height">3dp</dimen>
<!--電池頭 圓角-->
<dimen name="power_consumption_rankings_dimen_main_battery_view_head_corner">2dp</dimen>
<!--電池外殼和電池等級直接的間距-->
<dimen name="power_consumption_rankings_dimen_main_battery_view_gap">2dp</dimen>
<!--電池 寬度-->
<dimen name="power_consumption_rankings_dimen_main_battery_level_width">22dp</dimen>
<!--電池 最大高度-->
<dimen name="power_consumption_rankings_dimen_main_battery_level_max_height">35dp</dimen>
</resources>
3.2 實現自定義View
建立PowerConsumptionRankingsBatteryView繼承View,
在View的構造方法中,獲取我們需要的自定義樣式,重寫onMesure,onDraw方法。
package com.oyp.battery;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.DrawFilter;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
/**
* 自定義 耗電排行內的 電池圖示
* </p>
* created by OuyangPeng at 2018/6/10 下午 05:57
*
* @author OuyangPeng
*/
public class PowerConsumptionRankingsBatteryView extends View {
/**
* 電池最大電量
*/
public static final int MAX_LEVEL = 100;
/**
* 電池預設電量
*/
public static final int DEFAULT_LEVEL = 40;
/**
* 自定義View的寬
*/
private int width;
/**
* 自定義View的高
*/
private int height;
/**
* 抗鋸齒標誌
*/
private DrawFilter drawFilter;
/**
* 電池外殼 厚度
*/
private int shellStrokeWidth;
/**
* 電池外殼 圓角
*/
private int shellCornerRadius;
/**
* 電池外殼 寬度
*/
private int shellWidth;
/**
* 電池外殼 寬度
*/
private int shellHeight;
/**
* 電池頭 圓角
*/
private int shellHeadCornerRadius;
/**
* 電池頭 寬度
*/
private int shellHeadWidth;
/**
* 電池頭 高度
*/
private int shellHeadHeight;
/**
* 電池寬度
*/
private int levelWidth;
/**
* 電池最大高度
*/
private int levelMaxHeight;
/**
* 電池高度
*/
private int levelHeight = 100;
/**
* 電池外殼和電池等級直接的間距
*/
private int gap;
/**
* 電池外殼 畫筆
*/
private Paint shellPaint;
/**
* 電池外殼
*/
private RectF shellRectF;
/**
* 電池頭
*/
private RectF shellHeadRect;
/**
* 電池電量 畫筆
*/
private Paint levelPaint;
/**
* 電池電量
*/
private RectF levelRect;
/**
* 低電顏色
*/
private int lowerPowerColor;
/**
* 線上顏色
*/
private int onlineColor;
/**
* 離線顏色
*/
private int offlineColor;
public PowerConsumptionRankingsBatteryView(Context context) {
super(context);
}
public PowerConsumptionRankingsBatteryView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
drawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
initTypeArray(context, attrs);
//設定 電池殼畫筆的相關屬性
shellPaint = new Paint();
shellPaint.setAntiAlias(true);
shellPaint.setColor(onlineColor);
shellPaint.setStrokeWidth(shellStrokeWidth);
shellPaint.setAntiAlias(true);
//設定 電池畫筆的相關屬性
levelPaint = new Paint();
levelPaint.setColor(onlineColor);
levelPaint.setStyle(Paint.Style.FILL);
levelPaint.setStrokeWidth(levelWidth);
shellRectF = new RectF();
shellHeadRect = new RectF();
levelRect = new RectF();
}
/**
* 初始化自定義屬性
*/
private void initTypeArray(Context context, @Nullable AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PowerConsumptionRankingsBatteryView);
lowerPowerColor = typedArray.getColor(R.styleable.PowerConsumptionRankingsBatteryView_batteryLowerPowerColor,
getResources().getColor(R.color.lowerPowerColor));
onlineColor = typedArray.getColor(R.styleable.PowerConsumptionRankingsBatteryView_batteryOnlineColor,
getResources().getColor(R.color.onlineColor));
offlineColor = typedArray.getColor(R.styleable.PowerConsumptionRankingsBatteryView_batteryOfflineColor,
getResources().getColor(R.color.offlineColor));
//外殼的相關資訊
shellCornerRadius = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellCornerRadius,
getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_shell_corner));
shellWidth = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellWidth,
getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_shell_width));
shellHeight = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellHeight,
getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_shell_height));
shellStrokeWidth = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellStrokeWidth,
getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_shell_stroke_width));
//電池頭的相關資訊
shellHeadCornerRadius = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellHeadCornerRadius,
getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_head_corner));
shellHeadWidth = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellHeadWidth,
getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_head_width));
shellHeadHeight = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellHeadHeight,
getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_head_height));
//電池最大高度
levelMaxHeight = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryLevelMaxHeight,
getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_level_max_height));
//電池寬度
levelWidth = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryLevelWidth,
getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_level_width));
//電池外殼和電池等級直接的間距
gap = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryGap,
getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_gap));
//回收typedArray
typedArray.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//對View上的內容進行測量後得到的View內容佔據的寬度
width = getMeasuredWidth();
//對View上的內容進行測量後得到的View內容佔據的高度
height = getMeasuredHeight();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.setDrawFilter(drawFilter);
// 電池頭 矩形的座標
//座標 left:控制元件整體寬度的一半 減去 電池頭寬度的一半
shellHeadRect.left = width / 2 - shellHeadWidth / 2;
//座標 top: 0
shellHeadRect.top = 0;
//座標 right:控制元件整體寬度的一半 加上 電池頭寬度的一半
shellHeadRect.right = width / 2 + shellHeadWidth / 2;
//座標 bottom:電池頭的高度
shellHeadRect.bottom = shellHeadHeight;
// 電池殼 矩形的座標
//座標 left:電池殼厚度的一半
shellRectF.left = shellStrokeWidth / 2;
//座標 left:電池殼厚度的一半 加上 電池頭的高度
shellRectF.top = shellStrokeWidth / 2 + shellHeadHeight;
//座標 right:控制元件整體寬度 減去 電池殼厚度的一半
shellRectF.right = width - shellStrokeWidth / 2;
//座標 bottom:控制元件整體高度 減去 電池殼厚度的一半
shellRectF.bottom = height - shellStrokeWidth / 2;
// 電池電量 矩形的座標
//座標 left:電池殼厚度的一半 加上 電池外殼和電池等級直接的間距
levelRect.left = shellStrokeWidth + gap;
//電池滿格時候的最大高度 :(控制元件整體高度 減去電池殼厚度 減去電池頭高度 減去 電池外殼和電池等級直接的間距的兩倍)
//topOffset: 電池滿格時候的最大高度 * (電池滿格100 - 當前的電量)/ 電池滿格100
float topOffset = (height - shellHeadHeight - gap * 2 - shellStrokeWidth) * (MAX_LEVEL - levelHeight) / MAX_LEVEL;
//座標 top:電池頭的高度 + 電池殼的厚度 + 電池外殼和電池等級直接的間距 + topOffset
levelRect.top = shellHeadHeight + shellStrokeWidth + gap + topOffset;
//座標 right:控制元件整體寬度 減去 電池殼厚度的一半 減去 電池外殼和電池等級直接的間距
levelRect.right = width - shellStrokeWidth - gap;
//座標 bottom:控制元件整體寬度 減去 電池殼厚度的一半 減去 電池外殼和電池等級直接的間距
levelRect.bottom = height - shellStrokeWidth - gap;
//繪製電池頭
shellPaint.setStyle(Paint.Style.FILL);
canvas.drawRoundRect(shellHeadRect, shellHeadCornerRadius, shellHeadCornerRadius, shellPaint);
//由於電池頭的左下角和右下角不是圓角的,因此我們需要畫一個矩形 覆蓋圓角
//繪製左下角矩形
canvas.drawRect(shellHeadRect.left, shellHeadRect.bottom - shellHeadCornerRadius,
shellHeadRect.left + shellHeadCornerRadius, shellHeadRect.bottom, shellPaint);
//繪製右下角矩形
canvas.drawRect(shellHeadRect.right - shellHeadCornerRadius, shellHeadRect.bottom - shellHeadCornerRadius,
shellHeadRect.right, shellHeadRect.bottom, shellPaint);
//繪製電池殼
shellPaint.setStyle(Paint.Style.STROKE);
canvas.drawRoundRect(shellRectF, shellCornerRadius, shellCornerRadius, shellPaint);
//繪製電池等級
canvas.drawRect(levelRect, levelPaint);
}
/**
* 設定電池電量
*
* @param level
*/
public void setLevelHeight(int level) {
this.levelHeight = level;
if (this.levelHeight < 0) {
levelHeight = MAX_LEVEL;
} else if (this.levelHeight > MAX_LEVEL) {
levelHeight = MAX_LEVEL;
}
postInvalidate();
}
/**
* 設定線上 重繪
*/
public void setOnline() {
shellPaint.setColor(onlineColor);
levelPaint.setColor(onlineColor);
postInvalidate();
}
/**
* 設定離線 重繪
*/
public void setOffline() {
shellPaint.setColor(offlineColor);
levelPaint.setColor(offlineColor);
postInvalidate();
}
/**
* 設定低電 重繪
*/
public void setLowerPower() {
shellPaint.setColor(lowerPowerColor);
levelPaint.setColor(lowerPowerColor);
postInvalidate();
}
}
3.3 使用我們的自定義View
activity_main.xml 中使用我們的自定義View
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/constraintLayout"
tools:context=".MainActivity">
<com.oyp.battery.PowerConsumptionRankingsBatteryView
android:id="@+id/mPowerConsumptionRankingsBatteryView"
android:layout_width="@dimen/power_consumption_rankings_dimen_main_battery_view_width"
android:layout_height="@dimen/power_consumption_rankings_dimen_main_battery_view_height"
android:layout_marginTop="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="86dp"
android:text="@string/ouyangpeng"
android:gravity="center_horizontal"
app:layout_constraintEnd_toEndOf="@id/constraintLayout"
app:layout_constraintStart_toStartOf="@id/constraintLayout"
app:layout_constraintTop_toTopOf="@+id/constraintLayout"
app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
/>
</android.support.constraint.ConstraintLayout>
3.4 動態展示不同狀態下的電池View
MainActivity.java 檔案中 動態更新電池的狀態
package com.oyp.battery;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;
/**
* 自定義View 展示介面
* </p>
* created by OuyangPeng at 2018/6/15 下午 03:33
* @author OuyangPeng
*/
public class MainActivity extends AppCompatActivity implements BaseHandlerCallBack {
private PowerConsumptionRankingsBatteryView mPowerConsumptionRankingsBatteryView;
private int power;
private NoLeakHandler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHandler = new NoLeakHandler(this);
mPowerConsumptionRankingsBatteryView = (PowerConsumptionRankingsBatteryView) findViewById(R.id.mPowerConsumptionRankingsBatteryView);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
mHandler.sendEmptyMessage(0);
}
}, 0, 100);
}
@Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacksAndMessages(null);
}
@Override
public void callBack(Message msg) {
switch (msg.what) {
case 0:
mPowerConsumptionRankingsBatteryView.setLevelHeight(power += 5);
if (power == 100) {
power = 0;
}
if (power < 30) {
mPowerConsumptionRankingsBatteryView.setLowerPower();
} else if (power < 60) {
mPowerConsumptionRankingsBatteryView.setOffline();
} else {
mPowerConsumptionRankingsBatteryView.setOnline();
}
break;
default:
break;
}
}
private static class NoLeakHandler<T extends BaseHandlerCallBack> extends Handler {
private WeakReference<T> wr;
public NoLeakHandler(T t) {
wr = new WeakReference<>(t);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
T t = wr.get();
if (t != null) {
t.callBack(msg);
}
}
}
}
BaseHandlerCallBack 定義的介面
package com.oyp.battery;
import android.os.Message;
public interface BaseHandlerCallBack {
public void callBack(Message msg);
}
3.5 效果
四、github原始碼
相關推薦
Android自定義電池控制元件
一、背景最近公司有個業務,需要自定義一個電池控制元件,因此自己按照UI圖編寫了一個自定義View。二、效果三、實現過程首先看下視覺給出的UI效果圖 從這裡我們可以看得出來,要自定義這個電池View的話,分為3部分來繪製。第一部分是電池頭第二部分是電池邊框第三部分是電池電量3.
【我的Android進階之旅】Android自定義電池控制元件
一、背景 最近公司有個業務,需要自定義一個電池控制元件,因此自己按照UI圖編寫了一個自定義View。 二、效果 三、實現過程 首先看下視覺給出的UI效果圖 從這裡我們可以看得出來,要自定義這個電池View的話,分為3部分來
android自定義開關控制元件-SlideSwitch
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
android-自定義組合控制元件(EditText+選項)
一.前言 在開發中,或許一個業務需求中會出現很多系統控制元件組合成的佈局,並且經常需要複用。比如在一個表單中,裡面有個編輯框EditText右側還需要有個選項卡按鈕,需要有編輯框的輸入功能也需要有右側選項卡的點選事件,同時這兩個控制元件也存在一定關聯,且在一個介
Android自定義複合控制元件
在Android中,複合控制元件是非常常見的,下面以建立一個標題欄為例,講解建立自定義複合控制元件的過程。 以下圖為例:我們要建立一個標題欄,這個標題欄是由左邊的Button、右邊的Button以及中間的TextView複合而成的,而我們希望能夠
android自定義view控制元件之一圓角背景TextView
繼昨天寫了一個TextView可以包括兩種不同的風格字型,而保證可以換行的情況下的自定義View。今天的正文還是寫一個自定義的TextView。 一慣風格首先亮出實現效果,這最是直接不過的了。看下圖: 其實不通過寫一個自定義view的方式也可以實現這個效果,但是就需你在你
android 自定義組合控制元件 頂部導航欄
在軟體開發過程中,經常見到,就是APP 的標題欄樣式幾乎都是一樣的,只是文字不同而已,兩邊圖示不同。為了減少重複程式碼,提高效率, 方便大家使用,我們把標題欄通過組合的方式定義成一個控制元件。 例下圖:
Android 自定義RatingBar控制元件,顯示不全問題
最近專案要用到 自定義RatingBar控制元件 但是自定義好了樣式發現,星星只顯示一半,於是在網上找各種解決方法。 最後竟然是直接把資源圖片,移動到較高解析度的資料夾裡面。 我的解決做法如下,如果有更好的方法,請留言告知。 一,先寫一個drawable,設定好backg
Android自定義倒計時控制元件
序: 最近越來越多的APP都是用手機號註冊,一是為了方便使用者記憶,二是為了增加使用者賬戶的安全性。在我們進行交易操作或者修改密碼等操作的時候,這時候需要輸入簡訊驗證碼。這個控制元件會需要有倒計時的功能,這裡主要總結常見的幾種實現方式。 1.Android中實現倒計時
Android 自定義日曆控制元件
有圖有真像: 日曆控制元件View: /** * 日曆控制元件 功能:獲得點選的日期區間 * */ public class CalendarView extends View implements View.OnTouchListener { priv
Android自定義組合控制元件之實現CheckBox變化
前言:自定義組合控制元件最大的好處就是複用性,可以為我們節省不少程式碼量,但是一般我們自定義組合控制元件的時候,封裝出來的控制元件不管是自己用還是別人用也好,封裝的程式碼最好是易讀性強,便於修改,沒有必要封裝太多的屬性,一般控制在兩三個屬性為最佳,畢竟我們不是Google.
android 自定義倒計時控制元件(圓形倒計時顯示)
先上效果圖 - 倒計時結束 程式碼塊 attr.xml 控制元件需要用到的屬性: <?xml version="1.0" encoding="utf-8"?> <resources> <de
android 自定義組合控制元件
1.要實現這個效果,用多個控制元件組合起來,要用到自定義控制元件 程式碼實現 1.自己寫一個佈局檔案 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android
Android自定義時鐘控制元件
專案要求訪問網路是等待狀態要做時鐘的樣子,經過不懈努力,終於寫出來了,現分享出來,功能比較全,直接拷貝程式碼就可以用,僅供有這種需求的碼農們參考,如果採納,請點個贊,謝謝支援。 效果圖 主Activity,主要是在訪問介面的時候開啟時鐘,介面訪問結束關閉時鐘。 pa
android自定義組合控制元件圖片輪播+小圓點+點選跳轉廣告頁面
1.寫一個佈局,用於自定義組合控制元件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and
Android UI-自定義日曆控制元件
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
android開發:自定義組合控制元件
內容介紹 本文記錄,自定義組合控制元件,為了可以程式碼複用,減少程式碼量 配置控制元件屬性檔案 開啟res/values/目錄下的arss.xml檔案,新增下面屬性程式碼,如果沒有建立arrs.xml檔案。 <?xml version="1.0" enc
android 多功能自定義畫板控制元件(用於解決特定需求)
在專案中需要做一個可以自定義軌跡,但始終只有一條線,並且支援撤銷(撤銷單位為MotionEvent的down事件到up事件),還要支援動畫預覽等功能,最重要的是能夠按照間隔畫素來獲取所有點的座標,用於專案的其他功能。 整體的思路 1.專案中的應用場景需要畫板是一個圓形的,這個好實現用canv
android之自定義組合控制元件
---------------setting_item---------------<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.an
Android 三角標籤(自定義Textview控制元件)
先來看一下效果 上程式碼 package com.zph.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.C