220 lines
7.6 KiB
Java
220 lines
7.6 KiB
Java
package com.sw.dualscreen.view;
|
|
|
|
import android.animation.ValueAnimator;
|
|
import android.annotation.SuppressLint;
|
|
import android.content.Context;
|
|
import android.graphics.Canvas;
|
|
import android.graphics.Color;
|
|
import android.graphics.Paint;
|
|
import android.graphics.Rect;
|
|
import android.graphics.drawable.Drawable;
|
|
import android.util.AttributeSet;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.view.animation.AlphaAnimation;
|
|
|
|
import androidx.appcompat.content.res.AppCompatResources;
|
|
|
|
import com.sw.dualscreen.R;
|
|
import com.sw.plate.utils.AppUtil;
|
|
|
|
import timber.log.Timber;
|
|
|
|
/**
|
|
* 自定义柱状图
|
|
*/
|
|
public class CustomImageView extends androidx.appcompat.widget.AppCompatImageView {
|
|
private AlphaAnimation alphaAnimation;
|
|
private String textToDraw;
|
|
private Paint mPaint;
|
|
private int mTextSize = dpToPixels(26);
|
|
/**
|
|
* 最大高度
|
|
*/
|
|
// private static final int MAX_HEIGHT = 280;
|
|
private static final int MAX_HEIGHT = 250;
|
|
|
|
private boolean isDrawText = false;
|
|
// private Drawable bigRedDrawable;
|
|
// private Drawable bigGreenDrawable;
|
|
// private Drawable redDrawable;
|
|
// private Drawable greenDrawable;
|
|
|
|
public CustomImageView(Context context) {
|
|
super(context);
|
|
init(context);
|
|
}
|
|
|
|
public CustomImageView(Context context, AttributeSet attrs) {
|
|
super(context, attrs);
|
|
init(context);
|
|
}
|
|
|
|
public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
super(context, attrs, defStyleAttr);
|
|
init(context);
|
|
}
|
|
|
|
@SuppressLint("UseCompatLoadingForDrawables")
|
|
private void init(Context context) {
|
|
textToDraw = "超标";
|
|
mPaint = new Paint();//Paint.ANTI_ALIAS_FLAG
|
|
mPaint.setColor(Color.WHITE); // Set your desired color
|
|
mPaint.setTextSize(mTextSize); // Set your desired text size
|
|
mPaint.setAntiAlias(true);
|
|
// bigRedDrawable = context.getDrawable(R.drawable.img_big_red_column);
|
|
// bigGreenDrawable = context.getDrawable(R.drawable.img_big_green_column);
|
|
// redDrawable = context.getDrawable(R.drawable.img_red_column);
|
|
// greenDrawable = context.getDrawable(R.drawable.img_green_column);
|
|
// Typeface typeface = getResources().getFont(R.font.dakai);
|
|
// Typeface typeface = Typeface.create(Typeface.createFromAsset(getContext().getAssets(), "fonts/dakai.TTF"),
|
|
// Typeface.BOLD); // 创建Typeface
|
|
// mPaint.setTypeface(typeface); // 设置Paint的Typeface
|
|
}
|
|
|
|
@Override
|
|
protected void onDraw(Canvas canvas) {
|
|
super.onDraw(canvas);
|
|
if (isDrawText) {
|
|
Rect mBounds = new Rect();
|
|
mPaint.getTextBounds(textToDraw, 0, textToDraw.length(), mBounds);
|
|
mPaint.setShadowLayer(5, 2, 2, Color.GRAY);
|
|
float textWidth = mBounds.width();
|
|
Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();
|
|
float fontHeight = fontMetrics.bottom - fontMetrics.top;
|
|
//设置文字居中
|
|
float textBaseY = getHeight() - (getHeight() - fontHeight) / 2 - fontMetrics.bottom;
|
|
// textBaseY = 26;
|
|
canvas.drawText(textToDraw, (getWidth() - textWidth) / 2, textBaseY, mPaint);
|
|
}
|
|
}
|
|
|
|
// /**
|
|
// * 设置图片
|
|
// *
|
|
// * @param isBig 大图
|
|
// * @param isMore 是否超过
|
|
// */
|
|
// public void setImageDrawable(boolean isBig, boolean isMore) {
|
|
// if (isBig) {
|
|
// setImageDrawable(isMore ? bigRedDrawable : bigGreenDrawable);
|
|
// } else {
|
|
// setImageDrawable(isMore ? redDrawable : greenDrawable);
|
|
// }
|
|
// }
|
|
//
|
|
// public void setImageDrawable2(boolean isBig, Boolean isRed) {
|
|
// if (isBig) {
|
|
// setImageDrawable(isRed ? bigRedDrawable : bigGreenDrawable);
|
|
// } else {
|
|
// setImageDrawable(isRed ? redDrawable : greenDrawable);
|
|
// }
|
|
// }
|
|
|
|
public void setCustomHeightPercent(float percent, boolean isAnimation) {
|
|
Timber.d("setCustomHeightPercent percent = %s", percent);
|
|
if (percent <= 0) {
|
|
percent = 0f;
|
|
}
|
|
setCustomHeight((int) (percent * MAX_HEIGHT), false);
|
|
}
|
|
|
|
// 用于设置自定义高度的方法
|
|
public void setCustomHeight(int height, boolean isAnimation) {
|
|
Timber.d("setCustomHeight height = %s", height);
|
|
|
|
setVisibility(height == 0 ? View.GONE : VISIBLE);
|
|
if (height > MAX_HEIGHT) {
|
|
height = MAX_HEIGHT;
|
|
}
|
|
ViewGroup.LayoutParams layoutParams = getLayoutParams();
|
|
int height1 = layoutParams.height;
|
|
if (height1 != height) {
|
|
if (isAnimation) {
|
|
ValueAnimator animator = ValueAnimator.ofInt(height1, height);
|
|
animator.addUpdateListener(animation -> {
|
|
// 更新视图的高度
|
|
int val = (Integer) animation.getAnimatedValue();
|
|
ViewGroup.LayoutParams params = getLayoutParams();
|
|
params.height = dpToPixels(val);
|
|
setLayoutParams(params);
|
|
});
|
|
animator.setDuration(100); // 动画持续时间500毫秒
|
|
animator.start(); // 开始动画
|
|
} else {
|
|
if (layoutParams == null) {
|
|
layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
|
|
} else {
|
|
layoutParams.height = dpToPixels(height);
|
|
}
|
|
setLayoutParams(layoutParams);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
private boolean isStartAlphaAnimation = false;
|
|
|
|
public void startAlphaAnimation() {
|
|
if (!isStartAlphaAnimation) {
|
|
alphaAnimation = new AlphaAnimation(1.0f, 0.2f); // 从不透明渐变到完全透明
|
|
alphaAnimation.setDuration(420); // 设置动画持续时间,单位为毫秒
|
|
alphaAnimation.setRepeatCount(AlphaAnimation.INFINITE); // 设置动画重复次数为无限(可选)
|
|
startAnimation(alphaAnimation);
|
|
isStartAlphaAnimation = true;
|
|
}
|
|
}
|
|
|
|
public void cancelAlphaAnimation() {
|
|
if (alphaAnimation != null)
|
|
alphaAnimation.cancel();
|
|
isStartAlphaAnimation = false;
|
|
}
|
|
|
|
public void showText(String textToDraw) {
|
|
if (!AppUtil.isEmpty(textToDraw)) {
|
|
this.textToDraw = textToDraw;
|
|
}
|
|
isDrawText = true;
|
|
}
|
|
|
|
public void hideText() {
|
|
isDrawText = false;
|
|
}
|
|
|
|
private int dpToPixels(int dp) {
|
|
float scale = getResources().getDisplayMetrics().density;
|
|
return (int) (dp * scale + 0.5f);
|
|
}
|
|
|
|
public void setImageDrawable(boolean isBigImage, int index) {
|
|
int myIndex = index;
|
|
if (index < 0) {
|
|
myIndex = 0;
|
|
}else if (index > 4) {
|
|
myIndex = 4;
|
|
}
|
|
if (isBigImage) {
|
|
//使用大图bigImgArr
|
|
setImageDrawable(AppCompatResources.getDrawable(getContext(), bigImgArr[myIndex]));
|
|
return;
|
|
}
|
|
//使用小图smallImgArr
|
|
setImageDrawable(AppCompatResources.getDrawable(getContext(), smallImgArr[myIndex]));
|
|
}
|
|
|
|
private static final Integer[] bigImgArr = new Integer[]{
|
|
R.drawable.ic_gray_big,
|
|
R.drawable.ic_green_big,
|
|
R.drawable.ic_yellow_big,
|
|
R.drawable.ic_orange_big,
|
|
R.drawable.ic_red_big
|
|
};
|
|
private static final Integer[] smallImgArr = new Integer[]{
|
|
R.drawable.ic_gray_small,
|
|
R.drawable.ic_green_small,
|
|
R.drawable.ic_yellow_small,
|
|
R.drawable.ic_orange_small,
|
|
R.drawable.ic_red_small
|
|
};
|
|
} |