AutoTextView实现文字自动翻转效果

简介: AutoTextView实现文字自动翻转效果

image.png

效果图如上

代码如下:

AutoTextView

package com.jky.mobilebzt.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import com.jky.mobilebzt.R;
public class AutoTextView extends TextSwitcher implements
        ViewSwitcher.ViewFactory {
    private float mHeight;
    private Context mContext;
    //mInUp,mOutUp分别构成向下翻页的进出动画
    private Rotate3dAnimation mInUp;
    private Rotate3dAnimation mOutUp;
    //mInDown,mOutDown分别构成向下翻页的进出动画
    private Rotate3dAnimation mInDown;
    private Rotate3dAnimation mOutDown;
    public AutoTextView(Context context) {
        this(context, null);
    }
    public AutoTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.auto3d);
        mHeight = a.getDimension(R.styleable.auto3d_textSize, 36);
        a.recycle();
        mContext = context;
        init();
    }
    private void init() {
        setFactory(this);
        mInUp = createAnim(-90, 0, true, true);
        mOutUp = createAnim(0, 90, false, true);
        mInDown = createAnim(90, 0, true, false);
        mOutDown = createAnim(0, -90, false, false);
        //TextSwitcher主要用于文件切换,比如 从文字A 切换到 文字 B,
        //setInAnimation()后,A将执行inAnimation,
        //setOutAnimation()后,B将执行OutAnimation
        //初始化翻转
//        setInAnimation(mInUp);
//        setOutAnimation(mOutUp);
    }
    private Rotate3dAnimation createAnim(float start, float end, boolean turnIn, boolean turnUp) {
        final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end, turnIn, turnUp);
        rotation.setDuration(800);
        rotation.setFillAfter(false);
        rotation.setInterpolator(new AccelerateInterpolator());
        return rotation;
    }
    //这里返回的TextView,就是我们看到的View
    @Override
    public View makeView() {
        TextView t = new TextView(mContext);
//        t.setGravity(Gravity.CENTER);
        t.setTextSize(mHeight);
//        t.setTextColor(Resources.getSystem().getColor(android.R.color.black));
        t.setTextColor(mContext.getResources().getColor(R.color.white));
        t.setTextSize(13);
//        t.setGravity(Gravity.CENTER_VERTICAL);
        t.setPadding(0,15,0,15);
        t.setMaxLines(1);
        t.setEllipsize(TextUtils.TruncateAt.END);
        return t;
    }
    //定义动作,向下滚动翻页
    public void previous() {
        if (getInAnimation() != mInDown) {
            setInAnimation(mInDown);
        }
        if (getOutAnimation() != mOutDown) {
            setOutAnimation(mOutDown);
        }
    }
    //定义动作,向上滚动翻页
    public void next() {
        if (getInAnimation() != mInUp) {
            setInAnimation(mInUp);
        }
        if (getOutAnimation() != mOutUp) {
            setOutAnimation(mOutUp);
        }
    }
    class Rotate3dAnimation extends Animation {
        private final float mFromDegrees;
        private final float mToDegrees;
        private float mCenterX;
        private float mCenterY;
        private final boolean mTurnIn;
        private final boolean mTurnUp;
        private Camera mCamera;
        public Rotate3dAnimation(float fromDegrees, float toDegrees, boolean turnIn, boolean turnUp) {
            mFromDegrees = fromDegrees;
            mToDegrees = toDegrees;
            mTurnIn = turnIn;
            mTurnUp = turnUp;
        }
        @Override
        public void initialize(int width, int height, int parentWidth, int parentHeight) {
            super.initialize(width, height, parentWidth, parentHeight);
            mCamera = new Camera();
            mCenterY = getHeight() / 2;
            mCenterX = getWidth() / 2;
        }
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            final float fromDegrees = mFromDegrees;
            float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
            final float centerX = mCenterX;
            final float centerY = mCenterY;
            final Camera camera = mCamera;
            final int derection = mTurnUp ? 1 : -1;
            final Matrix matrix = t.getMatrix();
            camera.save();
            if (mTurnIn) {
                camera.translate(0.0f, derection * mCenterY * (interpolatedTime - 1.0f), 0.0f);
            } else {
                camera.translate(0.0f, derection * mCenterY * (interpolatedTime), 0.0f);
            }
            camera.rotateX(degrees);
            camera.getMatrix(matrix);
            camera.restore();
            matrix.preTranslate(-centerX, -centerY);
            matrix.postTranslate(centerX, centerY);
        }
    }
}

attrs.xml

   <declare-styleable name="auto3d">
        <attr name="textSize" format="dimension" />
    </declare-styleable>

xml

        <com.xx.xx.view.AutoTextView
                    android:id="@+id/atv_auto_qy"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/margin_5" />

activity

    private void showQyInfo() {
//        qyPowers.clear();
        isShowQyView = true;
        mQyInfoView.setVisibility(View.VISIBLE);
        if(timer == null){
            String[] qyinfo = context.getResources().getStringArray(R.array.qy_power);
            qyPowers = Arrays.asList(qyinfo);
            timer = new Timer();
            timer.schedule(timerTask, 100, 3000);
            powerPosition = 0;
        }
    }
    //计时
    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            if(isShowQyView) {
                Message message = new Message();
                message.what = 0x001;
                handler.sendMessage(message);
            }
        }
    };
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x001:
                    atvQy.next();
                    atvQy.setText(qyPowers.get(powerPosition));
                    powerPosition ++;
                    powerPosition = powerPosition % 3;
            }
        }
    };

strings.xml

<array name="qy_power">
        <item>8000+本建筑行业标准在线查看</item>
        <item>随时创建反馈,企业专家、云服务专家组、标准规范起草人为您解答</item>
        <item>移动课堂专享课程免费学习</item>
    </array>


相关文章
|
3月前
|
前端开发 定位技术 API
文字展示、坐标点给咱们返回
该React组件实现了基于高德地图API的地图功能,通过循环遍历后台数据动态创建并添加带有标签的标记(markers)至地图上。左侧的图例盒子采用绝对定位实现,包含缩放按钮与图例说明。点击+/-按钮可分别实现地图的放大与缩小,同时限制了地图的最大最小缩放级别为18和3。
14 0
|
索引
echarts x轴文字显示不全(xAxis文字倾斜比较全面的3种做法值得推荐)
echarts x轴标签文字过多导致显示不全 如图: 解决办法1:xAxis.axisLabel 属性 axisLabel的类型是object ,主要作用是:坐标轴刻度标签的相关设置。(当然yAxis也是一样有这个属性的) ...
4161 0
|
6月前
|
存储 Cloud Native Linux
OpenCV鼠标操作(画红色方框截取图像)
OpenCV鼠标操作(画红色方框截取图像)
|
7月前
如何实现图片多种颜色的文字混排并且带的背景颜色范围比文字图片范围大
如何实现图片多种颜色的文字混排并且带的背景颜色范围比文字图片范围大
46 1
|
7月前
如何实现按钮的图片在右边,文字在左边
如何实现按钮的图片在右边,文字在左边
110 0
|
7月前
echars 设置文字倾斜 图表根据鼠标滑轮自动改变x轴数据
echars 设置文字倾斜 图表根据鼠标滑轮自动改变x轴数据
102 1
|
7月前
如何在屏幕的任意位置拖拽控制图片的移动?
如何在屏幕的任意位置拖拽控制图片的移动?
133 1
|
算法 前端开发 JavaScript
图片转ASCII字符图案的原理(可调整亮度对比度 宽高度)
平时看代码会看到很多标点符号的字符拼起来的图案, 特别有趣, 像kong(一个高性能API网关), 除了源代码里面有图案, 命令行也藏了彩蛋. 我今天要玩的会深入一点: 基于图片的灰度值来生成图案. 此时的图片不单单有轮廓, 还有光影效果, 也就是素描中提及的黑白灰.
97 0