实现了双屏摄像头显示,添加了部分代码
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
package com.sw.dualscreen.view;
|
||||
|
||||
import android.animation.ValueAnimator;
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.animation.AlphaAnimation;
|
||||
|
||||
import com.sw.plate.utils.AppUtil;
|
||||
|
||||
public class CustomImageView extends androidx.appcompat.widget.AppCompatImageView {
|
||||
private AlphaAnimation alphaAnimation;
|
||||
private String textToDraw;
|
||||
private Paint mPaint;
|
||||
private int mTextSize = dpToPixels(26);
|
||||
|
||||
private boolean isDrawText = false;
|
||||
|
||||
public CustomImageView(Context context) {
|
||||
super(context);
|
||||
init();
|
||||
}
|
||||
|
||||
public CustomImageView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init();
|
||||
}
|
||||
|
||||
public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
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);
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
// 用于设置自定义高度的方法
|
||||
public void setCustomHeight(int height, boolean isAnimation) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user