欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Android中怎么通過(guò)自定義view實(shí)現(xiàn)動(dòng)態(tài)柱狀圖

這篇文章將為大家詳細(xì)講解有關(guān)Android中怎么通過(guò)自定義view實(shí)現(xiàn)動(dòng)態(tài)柱狀圖,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、重慶小程序開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了溫宿免費(fèi)建站歡迎大家使用!

自定義view

public class Histogram extends View {
 int MAX = 100;//矩形顯示的最大值
 int corner = 0; //矩形的角度。 設(shè)置為0 則沒(méi)有角度。
 double data = 0.0;//顯示的數(shù)
 double tempData = 0; //初始數(shù)據(jù)
 int textPadding = 50; //字體與矩形圖的距離
  Paint mPaint;
 int mColor;
  Context mContext;


 //構(gòu)造函數(shù)
 public Histogram(Context context) {
  super(context);
  mContext = context;
 }

 public Histogram(Context context, @Nullable AttributeSet attrs) {
  super(context, attrs);
  mContext = context;
  initPaint();
 }

 public Histogram(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  mContext = context;
  initPaint();
 }

 //畫(huà)筆方法
 private void initPaint() {
  mPaint = new Paint();
  mPaint.setAntiAlias(true);
  mColor = mContext.getResources().getColor(R.color.gary);
  mPaint.setColor(mColor);
 }

 @Override
 public void draw(Canvas canvas) {
  super.draw(canvas);

  if (data == 0.0) {
   mPaint.setTextSize(getWidth() / 2);
   RectF oval3 = new RectF(0, getHeight() - DensityUtils.pxTodip(mContext, 20), getWidth(), getHeight());// 設(shè)置個(gè)新的長(zhǎng)方形
   canvas.drawRoundRect(oval3, DensityUtils.pxTodip(mContext, corner), DensityUtils.pxTodip(mContext, corner), mPaint);

   canvas.drawText("0",
     getWidth() * 0.5f - mPaint.measureText("0") * 0.5f,
     getHeight() - DensityUtils.pxTodip(mContext, 20) - 2 * DensityUtils.pxTodip(mContext, textPadding),
     mPaint);
   return;
  }

  //防止數(shù)值很大的的時(shí)候,動(dòng)畫(huà)時(shí)間過(guò)長(zhǎng)
  int step = (int) (data / 100 + 1.0);

  if (tempData < data - step) {
   tempData = tempData + step;
  } else {
   tempData = data;
  }
  //畫(huà)圓角矩形
  String S = tempData + ""; //如果數(shù)字后面需要加% 則在""中添加%
  //設(shè)置顯示的字體
  Typeface typeface = Typeface.createFromAsset(getContext().getAssets(),"digital-7.ttf");
  mPaint.setTypeface(typeface);
//  //一個(gè)字和兩,三個(gè)字的字號(hào)相同
  if (S.length() < 4) {
   mPaint.setTextSize(getWidth()/2 );
  } else {
   mPaint.setTextSize(50); //可以通過(guò)getWidth()/2 改變字體大小 也可以通過(guò)設(shè)置數(shù)字來(lái)改變自己想要的字體大小 當(dāng)超出矩形圖寬度時(shí)不能顯示全部
  }
//
  float textH = mPaint.ascent() + mPaint.descent();
  float MaxH = getHeight() - textH - 2 * DensityUtils.pxTodip(mContext, textPadding);
//  //圓角矩形的實(shí)際高度
  float realH = (float) (MaxH / MAX * tempData);
  RectF oval3 = new RectF(0, getHeight() - realH, getWidth(), getHeight());// 設(shè)置個(gè)新的長(zhǎng)方形
  canvas.drawRoundRect(oval3, DensityUtils.pxTodip(mContext, corner), DensityUtils.pxTodip(mContext, corner), mPaint);
  //寫(xiě)數(shù)字
  canvas.drawText(S,
    getWidth() * 0.5f - mPaint.measureText(S) * 0.5f,
    getHeight() - realH - 2 * DensityUtils.pxTodip(mContext, textPadding),
    mPaint);
  if (tempData != data) {
   postInvalidate();
  }
 }

 public void setData(double data, int MAX) {
  this.data = data;
  this.MAX = MAX;
  postInvalidate();
 }

 public int getmColor() {
  return mColor;
 }

 public void setmColor(int mColor) {
  this.mColor = mColor;
 }

}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1"
 >
 <View
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:layout_weight="0.2"/>
 <com.mieasy.myhistogramview.Histogram
  android:id="@+id/column_one"
  android:layout_width="0dp"
  android:layout_height="300dp"
  android:layout_weight="0.8"/>

 <View
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:layout_weight="2.4"/>

 <com.mieasy.myhistogramview.Histogram
  android:id="@+id/column_two"
  android:layout_width="0dp"
  android:layout_height="300dp"
  android:layout_weight="1"/>

 <View
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:layout_weight="2.4"/>

 <com.mieasy.myhistogramview.Histogram
  android:id="@+id/column_three"
  android:layout_width="0dp"
  android:layout_height="300dp"
  android:layout_weight="1"/>
 <View
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:layout_weight="0.2"/>


</LinearLayout>

MainActivity調(diào)用initAllViews()方法

 private void initAllViews() {
  column_one = (Histogram) findViewById(R.id.column_one);
  column_two = (Histogram) findViewById(R.id.column_two);
  column_three = (Histogram) findViewById(R.id.column_three);

  column_one.setData( 20.22, 100);
  column_two.setData(30.2, 100);
  column_three.setData(40, 100);
  column_one.mPaint.setColor(getResources().getColor(R.color.colorAccent)); //改變柱狀圖的顏色
 }

關(guān)于Android中怎么通過(guò)自定義view實(shí)現(xiàn)動(dòng)態(tài)柱狀圖就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

當(dāng)前名稱:Android中怎么通過(guò)自定義view實(shí)現(xiàn)動(dòng)態(tài)柱狀圖
URL地址:http://www.chinadenli.net/article32/pigdsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作網(wǎng)站收錄建站公司營(yíng)銷(xiāo)型網(wǎng)站建設(shè)網(wǎng)站內(nèi)鏈網(wǎng)站營(yíng)銷(xiāo)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)