Android中跟随屏幕方向变化的文本

最近在做一个相机项目,其中有个提示语需跟随屏幕方向显示,简单写了下,效果如下
在这里插入图片描述
自定义属性部分:


    <!-- 跟随屏幕旋转文本 -->
    <declare-styleable name="orientationTextView">
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />
        <attr name="marginVer" format="dimension" />
        <attr name="marginHor" format="dimension" />
        <attr name="textGravity">
            <enum name="center" value="17" />
            <enum name="left" value="3" />
            <enum name="right" value="5" />
        </attr>
    </declare-styleable>

自定义view部分:

package com.example.demo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;

import com.example.demo.R;


/**
 * created by cx on 2020/3/5
 * describe 根据当前屏幕方向,改变文字方向,该控件需全屏使用
 **/
public class OrientationTextView extends View {

    private int mOrientation;
    private String mInfoStr = "";//文本内容
    private int mTextColor = Color.WHITE;
    private int mTextSize = 18;
    private Paint mPaint;
    private Rect mTextBoundRect;
    private int mMarginVer = 10;//竖向
    private int mMarginHor = 10;
    private int mGravity;

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

    public OrientationTextView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public OrientationTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.orientationTextView, 0, 0);
            mTextColor = a.getColor(R.styleable.orientationTextView_textColor, 0xFFFFFFFF);
            mTextSize = a.getDimensionPixelOffset(R.styleable.orientationTextView_textSize, 18);
            mMarginVer = a.getDimensionPixelOffset(R.styleable.orientationTextView_marginVer, 10);
            mMarginHor = a.getDimensionPixelOffset(R.styleable.orientationTextView_marginHor, 10);
            mGravity = a.getInt(R.styleable.orientationTextView_textGravity, Gravity.CENTER);
            a.recycle();//回收内存
        }

        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(mTextColor);
        mPaint.setTextSize(mTextSize);
        mPaint.setStrokeWidth(2);
        mTextBoundRect = new Rect();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.getTextBounds(mInfoStr, 0, mInfoStr.length(), mTextBoundRect);
        int canvasW = canvas.getWidth();
        int canvasH = canvas.getHeight();
        int anchorX = canvasW / 2;
        int anchorY = canvasH / 2;
        int delta = (canvasH - canvasW) / 2;
        int dx = 0;

        //默认靠左显示
        int startX = mMarginHor;
        int startY = mTextBoundRect.height() + mMarginVer;

        //文字非居中模式时,居横屏后,需往x轴方向平移对应的delta
        switch (mGravity) {
            case Gravity.LEFT:
                dx = -delta;
                break;
            case Gravity.CENTER://显示内容居中
                startX = (canvasW - mTextBoundRect.width()) / 2 + mMarginHor;
                break;
            case Gravity.RIGHT:
                startX = canvasW - mTextBoundRect.width() - mMarginHor;
                dx = delta;
                break;
            default:
                break;
        }

        canvas.save();
        switch (mOrientation) {
            case 0://正常
                //do nothing
                break;
            case 1://手机左旋
                canvas.rotate(90, anchorX, anchorY);
                canvas.translate(dx, delta);
                break;
            case 2://手机右旋
                canvas.rotate(-90, anchorX, anchorY);
                canvas.translate(dx, delta);
                break;
            case 3://倒着
                canvas.rotate(180, anchorX, anchorY);
                break;
            default:
                break;
        }

        //绘制黑色背景
        mPaint.setColor(Color.BLACK);
        mPaint.setTypeface(Typeface.DEFAULT_BOLD);
        canvas.drawText(mInfoStr, startX, startY, mPaint);

        mPaint.setColor(mTextColor);
        mPaint.setTypeface(Typeface.DEFAULT);
        canvas.drawText(mInfoStr, startX, startY, mPaint);
        canvas.restore();
    }

    /**
     * 根据屏幕方向绘制文本
     *
     * @param text        文本
     * @param orientation 屏幕方向 0:正常 1:手机听筒朝左, 2.朝右 3:倒着
     */
    public void showTextByOrientation(String text, int orientation) {
        Log.i("otv", "temp=" + text + " mOrientation=" + orientation);
        setVisibility(VISIBLE);
        mInfoStr = text;
        this.mOrientation = orientation;
        invalidate();
    }

    /**
     * 设置文本颜色
     *
     * @param color
     */
    public void setTextColor(int color) {
        this.mTextColor = color;
    }

}

xml布局:


    <com.example.demo.view.OrientationTextView
        android:id="@+id/orientationTv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:textColor="#FFFFFF"
        app:textGravity="center"
        app:textSize="20sp"
        app:marginVer="20dp"/>

在使用的地方根据屏幕方向直接调用showTextByOrientation方法即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值