207 lines
8.3 KiB
Java
207 lines
8.3 KiB
Java
package com.gh.common.view;
|
|
|
|
import android.content.Context;
|
|
import android.content.res.TypedArray;
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.Canvas;
|
|
import android.graphics.Color;
|
|
import android.graphics.Paint;
|
|
import android.graphics.PorterDuff;
|
|
import android.graphics.PorterDuffXfermode;
|
|
import android.graphics.Rect;
|
|
import android.graphics.RectF;
|
|
import android.support.annotation.StringRes;
|
|
import android.support.v4.content.ContextCompat;
|
|
import android.text.TextPaint;
|
|
import android.text.TextUtils;
|
|
import android.util.AttributeSet;
|
|
import android.widget.ProgressBar;
|
|
|
|
import com.gh.common.util.DisplayUtils;
|
|
import com.gh.gamecenter.R;
|
|
|
|
public class DownloadProgressBar extends ProgressBar {
|
|
private static final int MAX_LENGTH = 1000;
|
|
private static final int DOWNLOAD_NORMAL_STYLE = 0;
|
|
private static final int DOWNLOAD_RECT_STYLE = 1;
|
|
private static final int DOWNLOAD_IMAGE_STYLE = 2;
|
|
|
|
public enum DownloadType {
|
|
NORMAL,
|
|
NONE,
|
|
PLUGIN,
|
|
LAUNCH_OR_OPEN,
|
|
INSTALL_NORMAL,
|
|
INSTALL_PLUGIN,
|
|
DOWNLOADING_NORMAL,
|
|
DOWNLOADING_PLUGIN,
|
|
}
|
|
|
|
private PorterDuffXfermode mDuffXFerMode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
|
|
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
// 仅用于测量文字是否超出范围,不用于画文字
|
|
private TextPaint mFakeTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
|
|
|
|
private DownloadType mDownloadType;
|
|
|
|
private String mText;
|
|
|
|
private int mDownloadStyle;
|
|
private int mDefaultColor;
|
|
private int mTextSize;
|
|
|
|
private Rect mTextBound = new Rect();
|
|
|
|
public DownloadProgressBar(Context context) {
|
|
super(context);
|
|
}
|
|
|
|
public DownloadProgressBar(Context context, AttributeSet attrs) {
|
|
super(context, attrs);
|
|
if (attrs != null) {
|
|
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DownloadProgressBar);
|
|
mDownloadStyle = ta.getInteger(R.styleable.DownloadProgressBar_downloadStyle, DOWNLOAD_NORMAL_STYLE);
|
|
mTextSize = ta.getDimensionPixelSize(R.styleable.DownloadProgressBar_textSize, DisplayUtils.sp2px(getContext(), 14));
|
|
ta.recycle();
|
|
}
|
|
setMax(MAX_LENGTH);
|
|
setProgressDrawable(getResources().getDrawable(R.drawable.game_item_btn_download_style));
|
|
}
|
|
|
|
Bitmap srcBitmap;
|
|
Canvas srcCanvas;
|
|
RectF rectF;
|
|
|
|
private void create() {
|
|
srcBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
|
|
srcCanvas = new Canvas(srcBitmap);
|
|
rectF = new RectF(0, 0, getProgress() * getWidth() / MAX_LENGTH, getMeasuredHeight());
|
|
}
|
|
|
|
@Override
|
|
protected void onDraw(Canvas canvas) {
|
|
super.onDraw(canvas);
|
|
if (TextUtils.isEmpty(mText)) return;
|
|
|
|
mPaint.setColor(mDefaultColor == 0 ? ContextCompat.getColor(getContext(), R.color.theme) : mDefaultColor); // 初始化颜色
|
|
mPaint.setTextSize(mTextSize);
|
|
mFakeTextPaint.setTextSize(mTextSize);
|
|
mFakeTextPaint.setStyle(Paint.Style.FILL_AND_STROKE);
|
|
mFakeTextPaint.setTextAlign(Paint.Align.CENTER);
|
|
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
|
|
mPaint.setXfermode(null);
|
|
create();
|
|
|
|
Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();
|
|
int baseline = (getHeight() - fontMetrics.bottom - fontMetrics.top) / 2;
|
|
|
|
mPaint.setTextAlign(Paint.Align.CENTER);
|
|
|
|
canvas.getClipBounds(mTextBound); //The dimensions of your canvas
|
|
int width = mTextBound.width() - 20; //10 to keep some space on the right for the "..."
|
|
String txt = TextUtils.ellipsize(mText, mFakeTextPaint, width, TextUtils.TruncateAt.END).toString();
|
|
srcCanvas.drawText(txt, getWidth() / 2, baseline, mPaint);
|
|
mPaint.setXfermode(mDuffXFerMode);
|
|
if (getProgress() != 0) {
|
|
mPaint.setColor(DOWNLOAD_IMAGE_STYLE == mDownloadStyle ? Color.BLACK : Color.WHITE); // 反向颜色
|
|
}
|
|
|
|
srcCanvas.drawRect(rectF, mPaint);
|
|
canvas.drawBitmap(srcBitmap, 0, 0, null);
|
|
}
|
|
|
|
public void setText(String text) {
|
|
mText = text;
|
|
invalidate();
|
|
}
|
|
|
|
public String getText() {
|
|
return mText;
|
|
}
|
|
|
|
public void setText(@StringRes int res) {
|
|
setText(getResources().getString(res));
|
|
}
|
|
|
|
public void setDownloadType(DownloadType downloadType) {
|
|
switch (downloadType) {
|
|
case NORMAL:
|
|
case INSTALL_NORMAL:
|
|
switch (mDownloadStyle) {
|
|
case DOWNLOAD_RECT_STYLE:
|
|
setProgressDrawable(getResources().getDrawable(R.drawable.detail_download_normal_rect_style));
|
|
mDefaultColor = Color.WHITE;
|
|
break;
|
|
case DOWNLOAD_IMAGE_STYLE:
|
|
setProgressDrawable(getResources().getDrawable(R.drawable.detail_download_normal_image_style));
|
|
mDefaultColor = Color.BLACK;
|
|
break;
|
|
default:
|
|
setProgressDrawable(getResources().getDrawable(R.drawable.game_item_btn_download_style));
|
|
mDefaultColor = Color.WHITE;
|
|
break;
|
|
}
|
|
setProgress(0);
|
|
break;
|
|
case PLUGIN:
|
|
case INSTALL_PLUGIN:
|
|
setProgressDrawable(getResources().getDrawable(mDownloadStyle == DOWNLOAD_RECT_STYLE
|
|
? R.drawable.detail_download_plugin_install_rect_style : R.drawable.game_item_btn_plugin_style));
|
|
mDefaultColor = Color.WHITE;
|
|
setProgress(0);
|
|
break;
|
|
case NONE:
|
|
setProgressDrawable(getResources().getDrawable(mDownloadStyle == DOWNLOAD_RECT_STYLE
|
|
? R.drawable.detail_download_none_rect_style : R.drawable.news_detail_comment));
|
|
mDefaultColor = ContextCompat.getColor(getContext(), R.color.hint);
|
|
setProgress(0);
|
|
break;
|
|
case LAUNCH_OR_OPEN:
|
|
switch (mDownloadStyle) {
|
|
case DOWNLOAD_RECT_STYLE:
|
|
setProgressDrawable(getResources().getDrawable(R.drawable.detail_download_open_rect_style));
|
|
mDefaultColor = ContextCompat.getColor(getContext(), R.color.theme);
|
|
break;
|
|
case DOWNLOAD_IMAGE_STYLE:
|
|
setProgressDrawable(getResources().getDrawable(R.drawable.detail_download_open_image_style));
|
|
mDefaultColor = Color.WHITE;
|
|
break;
|
|
default:
|
|
setProgressDrawable(getResources().getDrawable(R.drawable.detail_download_open_style));
|
|
mDefaultColor = ContextCompat.getColor(getContext(), R.color.theme);
|
|
break;
|
|
}
|
|
setProgress(0);
|
|
break;
|
|
case DOWNLOADING_NORMAL:
|
|
switch (mDownloadStyle) {
|
|
case DOWNLOAD_RECT_STYLE:
|
|
setProgressDrawable(getResources().getDrawable(R.drawable.detail_downloading_normal_rect_style));
|
|
mDefaultColor = ContextCompat.getColor(getContext(), R.color.theme);
|
|
break;
|
|
case DOWNLOAD_IMAGE_STYLE:
|
|
setProgressDrawable(getResources().getDrawable(R.drawable.detail_downloading_normal_image_style));
|
|
mDefaultColor = Color.WHITE;
|
|
break;
|
|
default:
|
|
setProgressDrawable(getResources().getDrawable(R.drawable.detail_downloading_normal_style));
|
|
mDefaultColor = ContextCompat.getColor(getContext(), R.color.theme);
|
|
break;
|
|
}
|
|
break;
|
|
case DOWNLOADING_PLUGIN:
|
|
setProgressDrawable(getResources().getDrawable(mDownloadStyle == DOWNLOAD_RECT_STYLE
|
|
? R.drawable.detail_downloading_plugin_rect_style : R.drawable.detail_downloading_plugin_style));
|
|
mDefaultColor = ContextCompat.getColor(getContext(), R.color.btn_plugin);
|
|
break;
|
|
}
|
|
|
|
mDownloadType = downloadType;
|
|
}
|
|
|
|
public DownloadType getDownloadType() {
|
|
return mDownloadType;
|
|
}
|
|
|
|
}
|