android实践:canvas文字图片旋转显示

发布于:2025-06-29 ⋅ 阅读:(15) ⋅ 点赞:(0)

问题:如何在手机竖屏模式下将文字/图片旋转显示,即类似横屏方向上显示

显示效果:

实现:

1.文字旋转显示

            canvas.save();//保存状态入栈
            canvas.translate(getWidth() >> 1, getHeight() >> 1);//显示中心
            Paint titlePaint = new Paint();
            titlePaint.setColor(Color.WHITE);
            titlePaint.setTextSize(60);
            titlePaint.setStrokeWidth(6);
            titlePaint.setStyle(Style.FILL);
            //水平居中显示
            titlePaint.setTextAlign(Align.CENTER);
            //旋转显示
            canvas.rotate(90.0F);
            if(cardType == EngineManager.cardType.EXOCRCardTypeIDCARD){
                String tip = "";
                if(imageType == 1){
                    tip = "人像面";
                }else if(imageType == 2){
                    tip = "国徽面";
                }
                canvas.drawText("请将取景框对准身份证", -titlePaint.measureText(tip)/2, -this.frame.width()/2 - 20, titlePaint);
                titlePaint.setColor(Color.rgb(250,186,2));
                canvas.drawText(tip, titlePaint.measureText("请将取景框对准身份证")/2, -this.frame.width()/2 - 20, titlePaint);
            }else{
                canvas.drawText("请将银行卡放在此处", 0, -this.frame.width()/2 - 20, titlePaint);
            }
            canvas.restore();//恢复保存的状态出栈

2.图片旋转显示

class Back {
    private float mWidth;
    private float mHeight;
    private Bitmap back;

    public Back(float width, float height, Context context) {
        this.mWidth = width;
        this.mHeight = height;
        int backId = ViewUtil.getResourseIdByName(DomCardManager.getInstance().getPackageName(), "drawable", "icon_delete");
        this.back = BitmapFactory.decodeResource(context.getResources(), backId);
    }

    public void draw(Canvas canvas, Context mContext) {
        canvas.save();
        canvas.translate(-this.mWidth / 2.0F, -this.mHeight / 2.0F);
        this.drawImage(canvas, this.back, 0, 0, (int)this.mWidth, (int)this.mHeight, 0, 0);
        canvas.restore();
    }

    public void drawImage(Canvas canvas, Bitmap blt, int x, int y, int w, int h, int bx, int by) {
        Rect src = new Rect();
        Rect dst = new Rect();
        src.left = bx;
        src.top = by;
        src.right = bx + w;
        src.bottom = by + h;
        dst.left = x;
        dst.top = y;
        dst.right = x + w;
        dst.bottom = y + h;
        canvas.drawBitmap(blt, (Rect)null, dst, (Paint)null);
        src = null;
        dst = null;
    }
}
this.mScale = this.getResources().getDisplayMetrics().density / 1.5F;
this.mBack = new Back(60.0F * this.mScale, 60.0F * this.mScale, context);
if (this.backPoint == null) {
    this.backPoint = new Point(screenSize.x - 120, this.frame.top / 3);
}
this.mBackRect = ViewUtil.rectGivenCenter(this.backPoint, 60.0F * this.mScale, 60.0F * this.mScale);
if (this.mBack != null) {
    canvas.save();
    canvas.translate(this.mBackRect.exactCenterX(), this.mBackRect.exactCenterY());
    canvas.rotate(90.0F);
    this.mBack.draw(canvas, this.mContext);
    canvas.restore();
}
public static Rect rectGivenCenter(Point center, float width, float height) {
        return new Rect((int)((float)center.x - width / 2.0F), (int)((float)center.y - height / 2.0F), (int)((float)center.x + width / 2.0F), (int)((float)center.y + height / 2.0F));
    }