中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android自定義View倒計時圓

發布時間:2020-08-25 21:27:02 來源:腳本之家 閱讀:163 作者:贏le 欄目:移動開發

本文實例為大家分享了Android自定義View倒計時圓的具體代碼,供大家參考,具體內容如下

Android自定義View倒計時圓 

創建attr

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="CountDownView">
    <!--顏色-->
    <attr name="ringColor" format="color" />
    <!-- 進度文本的字體大小 -->
    <attr name="progressTextSize" format="dimension" />
    <!-- 圓環寬度 -->
    <attr name="ringWidth" format="float" />
    <!--進度文本顏色-->
    <attr name="progressTextColor" format="color"/>
    <!--倒計時-->
    <attr name="countdownTime" format="integer"/>
  </declare-styleable>
</resources>

創建DisplayUtil 類

import android.content.Context;

/**
 * Created by 王 on 2017/10/21.
 */

public class DisplayUtil {
  /**
   * 將px裝換成dp,保證尺寸不變
   * @param context
   * @param pxValue
   * @return
   */
  public static int px2dp(Context context, float pxValue){
    float density = context.getResources().getDisplayMetrics().density;//得到設備的密度
    return (int) (pxValue/density+0.5f);
  }
  public static int dp2px(Context context,float dpValue){
    float density = context.getResources().getDisplayMetrics().density;
    return (int) (dpValue*density+0.5f);
  }
  public static int px2sp(Context context,float pxValue){
    float scaleDensity = context.getResources().getDisplayMetrics().scaledDensity;//縮放密度
    return (int) (pxValue/scaleDensity+0.5f);
  }
  public static int sp2px(Context context,float spValue) {
    float scaleDensity = context.getResources().getDisplayMetrics().scaledDensity;
    return (int) (spValue*scaleDensity+0.5f);
  }
}

繼承View

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Created by 王 on 2017/10/21.
 */

public class CountDownView extends View{
  //圓輪顏色
  private int mRingColor;
  //圓輪寬度
  private float mRingWidth;
  //圓輪進度值文本大小
  private int mRingProgessTextSize;
  //寬度
  private int mWidth;
  //高度
  private int mHeight;
  private Paint mPaint;
  //圓環的矩形區域
  private RectF mRectF;
  //
  private int mProgessTextColor;
  private int mCountdownTime;
  private float mCurrentProgress;
  private OnCountDownFinishListener mListener;

  public CountDownView(Context context) {
    this(context, null);
  }

  public CountDownView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public CountDownView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CountDownView);
    mRingColor = a.getColor(R.styleable.CountDownView_ringColor, context.getResources().getColor(R.color.colorAccent));
    mRingWidth = a.getFloat(R.styleable.CountDownView_ringWidth, 40);
    mRingProgessTextSize = a.getDimensionPixelSize(R.styleable.CountDownView_progressTextSize, DisplayUtil.sp2px(context, 20));
    mProgessTextColor = a.getColor(R.styleable.CountDownView_progressTextColor, context.getResources().getColor(R.color.colorAccent));
    mCountdownTime = a.getInteger(R.styleable.CountDownView_countdownTime, 10);
    a.recycle();
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setAntiAlias(true);
    this.setWillNotDraw(false);
  }

  public void setCountdownTime(int mCountdownTime) {
    this.mCountdownTime = mCountdownTime;
  }

  @Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    mWidth = getMeasuredWidth();
    mHeight = getMeasuredHeight();
    mRectF = new RectF(0 + mRingWidth / 2, 0 + mRingWidth / 2,
        mWidth - mRingWidth / 2, mHeight - mRingWidth / 2);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    /**
     *圓環
     */
    //顏色
    mPaint.setColor(mRingColor);
    //空心
    mPaint.setStyle(Paint.Style.STROKE);
    //寬度
    mPaint.setStrokeWidth(mRingWidth);
    canvas.drawArc(mRectF, -90, mCurrentProgress - 360, false, mPaint);
    //繪制文本
    Paint textPaint = new Paint();
    textPaint.setAntiAlias(true);
    textPaint.setTextAlign(Paint.Align.CENTER);
    String text = mCountdownTime - (int) (mCurrentProgress / 360f * mCountdownTime) + "";
    textPaint.setTextSize(mRingProgessTextSize);
    textPaint.setColor(mProgessTextColor);

    //文字居中顯示
    Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
    int baseline = (int) ((mRectF.bottom + mRectF.top - fontMetrics.bottom - fontMetrics.top) / 2);
    canvas.drawText(text, mRectF.centerX(), baseline, textPaint);
  }

  private ValueAnimator getValA(long countdownTime) {
    ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 100);
    valueAnimator.setDuration(countdownTime);
    valueAnimator.setInterpolator(new LinearInterpolator());
    valueAnimator.setRepeatCount(0);
    return valueAnimator;
  }
  /**
   * 開始倒計時
   */
  public void startCountDown() {
    setClickable(false);
    ValueAnimator valueAnimator = getValA(mCountdownTime * 1000);
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        float i = Float.valueOf(String.valueOf(animation.getAnimatedValue()));
        mCurrentProgress = (int) (360 * (i / 100f));
        invalidate();
      }
    });
    valueAnimator.start();
    valueAnimator.addListener(new AnimatorListenerAdapter() {
      @Override
      public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        //倒計時結束回調
        if (mListener != null) {
          mListener.countDownFinished();
        }
        setClickable(true);
      }

    });
  }
  public void setAddCountDownListener(OnCountDownFinishListener mListener) {
    this.mListener = mListener;
  }
  public interface OnCountDownFinishListener {
    void countDownFinished();
  }

}

布局

<?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"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.circulardemo.MainActivity">

  <com.example.circulardemo.CountDownView
    android:id="@+id/cdv"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

Mainactivity

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CountDownView countDownView = (CountDownView) findViewById(R.id.cdv);
    //啟動
    countDownView.startCountDown();

    countDownView.setAddCountDownListener(new CountDownView.OnCountDownFinishListener() {
      @Override
      public void countDownFinished() {
        Toast.makeText(MainActivity.this, "倒計時結束", Toast.LENGTH_SHORT).show();
      }
    });
  }


}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

苗栗市| 宕昌县| 扶绥县| 宁晋县| 荥阳市| 铜陵市| 都昌县| 汉沽区| 都匀市| 北海市| 宁波市| 吕梁市| 永顺县| 新河县| 乐至县| 衡水市| 田林县| 灵武市| 平潭县| 凤山县| 天水市| 建昌县| 东山县| 郁南县| 宜宾县| 山阳县| 日喀则市| 馆陶县| 阳城县| 汶上县| 青冈县| 商城县| 卢氏县| 金华市| 英超| 斗六市| 沐川县| 宁乡县| 崇礼县| 桃园市| 南安市|