欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)顯示電量的控件代碼

 更新時間:2016年10月21日 09:59:50   作者:87年的90后  
本文介紹了Android實現(xiàn)顯示電量的控件代碼,主要功能就是可以顯示電量,有需要的朋友可以來了解一下。

下面介紹了Android實現(xiàn)顯示電量的控件代碼,具體代碼如下:

1、目錄結構,本人是使用安卓死丟丟。


2、運行界面,輸入框中輸入數(shù)值,點擊刷新,會再電池中顯示出相應的電量

3、繪制自定義電池控件,首先,新建一個類BatteryState繼承View

private Context mContext; 
private float width; 
private float height; 
private Paint mPaint; 
private float powerQuantity=0.5f;//電量 

要使用到的變量

public BatteryState(Context context) { 
  super(context); 
  mContext=context; 
  mPaint = new Paint(); 
 
} 
 
public BatteryState(Context context, AttributeSet attrs) { 
  super(context, attrs); 
  mContext=context; 
  mPaint = new Paint(); 
} 
 
public BatteryState(Context context, AttributeSet attrs, int defStyleAttr) { 
  super(context, attrs, defStyleAttr); 
  mContext=context; 
  mPaint = new Paint(); 
} 

三個構造方法,自定義控件的時候一般會把這三個構造方法寫出來,便于在layout中使用或者直接定義,其中AttributeSet是當使用xml文件定義該控件時引用的屬性集

@Override 
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
//    計算控件尺寸 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
  } 
 
  @Override 
  protected void onDraw(Canvas canvas) { 
//繪制界面 
    super.onDraw(canvas); 
    Bitmap batteryBitmap=ReadBitMap(mContext, R.drawable.battery_empty);//讀取圖片資源 
    width=batteryBitmap.getWidth(); 
    height=batteryBitmap.getHeight(); 
    if (powerQuantity>0.3f&&powerQuantity<=1) { 
//      電量少于30%顯示紅色 
      mPaint.setColor(Color.GREEN); 
    } 
    else if (powerQuantity>=0&&powerQuantity<=0.3) 
    { 
      mPaint.setColor(Color.RED); 
    } 
//    計算繪制電量的區(qū)域 
    float right=width*0.94f; 
    float left=width*0.21f+(right-width*0.21f)*(1-powerQuantity); 
    float tope=height*0.45f; 
    float bottom=height*0.67f; 
 
    canvas.drawRect(left,tope,right,bottom,mPaint); 
    canvas.drawBitmap(batteryBitmap, 0, 0, mPaint); 
  } 

由于我們定義的控件時一個單個控件,不是容器控件,所以我只重寫了onMeasure、onDraw分別用來計算大小和繪制界面,根據(jù)背景圖片來計算要繪制的區(qū)域

  public void refreshPower(float power) 
{ 
  powerQuantity=power; 
  if (powerQuantity>1.0f) 
    powerQuantity=1.0f; 
  if (powerQuantity<0) 
    powerQuantity=0; 
  invalidate(); 
} 

刷新控件

4、在xml文件中定義:

<LinearLayout 
  android:layout_width="wrap_content" 
  android:layout_marginLeft="30dp" 
  android:layout_height="30dp"> 
  <com.example.administrator.batterytest.BatteryState 
    android:id="@+id/bs_power" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
</LinearLayout> 

5、在Activity中使用

mBtnTry = (TextView) findViewById(R.id.btn_try); 
    mBtnTry.setText("刷新電量"); 
//    mBtnTry.setBackground(getResources().getDrawable(R.drawable.maxwell_sun_5_bar)); 
    mBsPower = (BatteryState) findViewById(R.id.bs_power); 
    mBtnTry.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        float power = Integer.parseInt(mEtPower.getText().toString()); 
        float p = power / 100; 
        mBsPower.refreshPower(p); 
      } 
    }); 

希望本文所述對你有所幫助,Android實現(xiàn)顯示電量的控件代碼就給大家介紹到這里了。希望大家繼續(xù)關注我們的網(wǎng)站!

相關文章

  • Android Activity的生命周期與啟動模式全面解讀

    Android Activity的生命周期與啟動模式全面解讀

    雖然說我們天天都在使用Activity,但是你真的對Activity的生命機制完全了解了嗎?Activity的生命周期方法只有七個,但是其實那只是默認的情況。也就是說在其他情況下,Activity的生命周期可能不會是按照我們以前所知道的流程,這就要說到Activity的啟動模式
    2021-10-10
  • Android手機聯(lián)系人快速索引(手機通訊錄)

    Android手機聯(lián)系人快速索引(手機通訊錄)

    最近需要實現(xiàn)一個手機通訊錄的快速索引功能。根據(jù)姓名首字母快速索引功能,下面通過本篇文章給大家介紹Android手機聯(lián)系人快速索引(手機通訊錄)的相關代碼,需要的朋友參考下
    2015-12-12
  • Android簡單實現(xiàn) 緩存數(shù)據(jù)

    Android簡單實現(xiàn) 緩存數(shù)據(jù)

    這篇文章主要介紹了Android簡單實現(xiàn) 緩存數(shù)據(jù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • 詳解android shape的使用總結

    詳解android shape的使用總結

    在Android程序開發(fā)中,我們經常會去用到Shape這個東西去定義各種各樣的形狀,本篇文章主要介紹了android shape的使用,有興趣的可以一起了解一下。
    2016-12-12
  • JetpackCompose Navigation導航實現(xiàn)流程

    JetpackCompose Navigation導航實現(xiàn)流程

    Navigation是Jetpack用于Android導航的組件,作用是處理頁面跳轉,以及頁面跳轉過程中的交互。使用Navigation,你就需要為每個頁面設定一條唯一路徑,它是一個String常量,形式是DeepLink的樣子,從一個頁面跳轉到另一個頁面,它通過輸入目的地的路徑進行轉跳
    2023-01-01
  • Android中asset文件夾與raw文件夾的區(qū)別深入解析

    Android中asset文件夾與raw文件夾的區(qū)別深入解析

    本篇文章是對Android中的asset文件夾與raw文件夾區(qū)別進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • Flutter 快速實現(xiàn)聊天會話列表效果示例詳解

    Flutter 快速實現(xiàn)聊天會話列表效果示例詳解

    這篇文章主要為大家介紹了Flutter 快速實現(xiàn)聊天會話列表效果示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • Kotlin協(xié)程的啟動方式介紹

    Kotlin協(xié)程的啟動方式介紹

    這篇文章我們來講協(xié)程的啟動,畢竟協(xié)程是一個很強大的設計模式,深入了解需要花很多的時間,我們先從簡單開始,其實學會了簡單的使用,基本已經可以滿足我們平時的開發(fā)需要了,話不多說,開始
    2022-09-09
  • kotlin淺析when與循環(huán)的使用

    kotlin淺析when與循環(huán)的使用

    這篇文章主要介紹了kotlin語言中when與循環(huán)的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • Android實現(xiàn)2048小游戲

    Android實現(xiàn)2048小游戲

    這篇文章主要為大家介紹了Android實現(xiàn)2048小游戲的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-01-01

最新評論