簡體   English   中英

在特定時間段內為Android着色TextView

[英]Coloring TextView during a certain time period Android

有沒有一種方法可以在特定時間段內在TextViewAndroid中的其他顏色上為文本着色。 因此,它將以全白色文本開始,然后coloring從左到右移動並在一定duration填充滿。
因此,例如,如果duration10則整行應在10秒內變為彩色,但其移動速度也應相同。

看起來像這樣:

在此處輸入圖片說明

有一種方法可以在帶有CATextLayer IOSCATextLayer ,但是我還沒有找到在Android上執行此操作的方法。

我去年剛剛創建一個自定義TextView,這是我的課程

public class AKChangeColorTextView extends TextView {
    public AKChangeColorTextView(Context context) {
        this(context,null);
    }
    String TAG = "AKChangeColorTextView";
    public AKChangeColorTextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    RectF mRectF;
    float mX;
    float mY;
    public AKChangeColorTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLUE);
        PorterDuffXfermode mode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
        mPaint.setXfermode(mode);
        float x = 60;
        float y = 10;
        mY =    0;
        mRectF = new RectF(x, y, x + 50, y + 50);
        mTPaint = getPaint();
        mX = 0;
    }

    Paint mPaint;
    TextPaint mTPaint;
    Bitmap shadowBitmap ;
    Rect bounds = new Rect();
    Canvas textCanvas;

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        if (shadowBitmap == null) {
            shadowBitmap = Bitmap.createBitmap(getMeasuredWidth(),getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        }
        if (textCanvas == null) {
            textCanvas = new Canvas(shadowBitmap);
        }
        textCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        if (mTPaint == null) {
            mTPaint = getPaint();
        }
        String content  = getText().toString();
        mTPaint.getTextBounds(content,0,content.length(),bounds);
        textCanvas.drawText(content,0,bounds.height(),mTPaint);
        mRectF.set(colorLeft,mY,colorRight,mY+bounds.height()+10);
        textCanvas.drawRect(mRectF,mPaint);
        canvas.drawBitmap(shadowBitmap,0,0,null);
    }
    float colorLeft;
    float colorRight;
    public void setXOffset(float left,float right){
        colorLeft = left;
        colorRight = right;
        invalidate();
    }

}

我的演示代碼:

class MainActivity : AppCompatActivity() {
    val TAG = "MainActivity"
    lateinit var countDownTimer:CountDownTimer
    var currentOffsetx = 0
    var textWidth = 0
    var isIncrease = true
    lateinit var txt:AKChangeColorTextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
        (findViewById<View>(R.id.tv_hello) as AKChangeColorTextView).apply{
            txt = this
        }

        countDownTimer = object:CountDownTimer(300000,200){
            override fun onFinish() {
            }

            override fun onTick(millisUntilFinished: Long) {
                if (textWidth == 0) {
                    textWidth = txt.width
                }
                if (currentOffsetx <= textWidth) {
                    if (isIncrease) {
                        currentOffsetx += 10
                        currentOffsetx = min(currentOffsetx, textWidth)
                    } else {
                        currentOffsetx -= 10
                        currentOffsetx = max(currentOffsetx, 0)
                    }
                }
                if (currentOffsetx == textWidth) {
                    isIncrease = false
                }else if (currentOffsetx == 0) {
                    isIncrease = true
                }
                txt.setXOffset(0f,currentOffsetx.toFloat())
                Log.w(TAG,"check current tick:$millisUntilFinished,offsetx:$currentOffsetx,txtWidth:$textWidth")
            }
        }
        countDownTimer.start()
    }

}

我的xml布局:

<com.example.administrator.myapplication.AKChangeColorTextView
            android:id="@+id/tv_hello"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="I Found a Love For Aolphn"
            android:textColor="#000000"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

以下是效果

在此處輸入圖片說明

有一種方法可以實現優化,簡單,流暢和美觀: 矢量動畫可繪制對象(API21 +)

有很多教程(例如視頻)展示了如何制作精美的動畫。 基本上,步驟如下:

  • 為您的文本創建兩個SVG矢量圖像,一個為正常顏色,另一個為彩色字母。 例如,您可以在Adobe Illustrator中輕松完成此操作。
  • 將兩者都導入shapeshifter.design

  • 根據您的喜好為第二個(彩色圖層)創建動畫。

  • 將生成的xml文件復制到您的可繪制對象中,您就完成了!

祝好運!

您可以使用文本可跨度和前景顏色跨度並一次為一個字符設置動畫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM