国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android 實(shí)現(xiàn)跑馬燈效果

這篇具有很好參考價(jià)值的文章主要介紹了Android 實(shí)現(xiàn)跑馬燈效果。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

Android 實(shí)現(xiàn)跑馬燈效果

Android中實(shí)現(xiàn)跑馬燈效果有多種方式,本篇簡(jiǎn)單介紹下:

1: TextView屬性實(shí)現(xiàn)

    <TextView
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:background="#77000000"
        android:padding="5dp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:scrollHorizontally="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:text="這是textview的跑馬燈效果"
        android:id="@+id/tv1"
        />

這里需要注意下:

  1. 需要限制textview的寬度,不能設(shè)置為wrap_content
  2. 啟動(dòng)跑馬燈效果需要獲取焦點(diǎn)requestFocus().

2: 代碼實(shí)現(xiàn)

tv2.setSingleLine();
tv2.setHorizontallyScrolling(true);
tv2.setEllipsize(TextUtils.TruncateAt.MARQUEE);
tv2.setMarqueeRepeatLimit(-1);
tv2.setFocusable(true);
tv2.setFocusableInTouchMode(true);
tv2.requestFocus();

3: 自定義 view實(shí)現(xiàn)

這里可以使用動(dòng)畫(huà)的效果實(shí)現(xiàn).文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-800775.html

package com.test.marquee;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

public class MarqueeView extends View {
    private String text;
    private Paint paint;
    private float textWidth;
    private float textX;
    private float viewWidth;
    private ValueAnimator animator;

    public MarqueeView(Context context) {
        super(context);
        init();
    }

    public MarqueeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MarqueeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        text = "This is a marquee";
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextSize(50);
        paint.setColor(Color.BLACK);
        textWidth = paint.measureText(text);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewWidth = w;
        textX = viewWidth;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawText(text, textX, getHeight() / 2, paint);
    }

    public void startMarquee() {
        animator= ValueAnimator.ofFloat(viewWidth, -textWidth);
        animator.setDuration(5000);
        animator.setInterpolator(new LinearInterpolator());
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.addUpdateListener(animation -> {
            textX = (float) animation.getAnimatedValue();
            invalidate();
        });
        animator.start();
    }

    public void stopMarquee() {
        // 停止動(dòng)畫(huà)
        if (animator!=null) animator.cancel();
    }
}

4: 實(shí)現(xiàn)豎直效果的跑馬燈

package com.test.marquee;

import android.content.Context;
import android.graphics.Canvas;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.AttributeSet;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

public class VerticalMarqueeTextView extends AppCompatTextView {
    private float offsetY;

    public VerticalMarqueeTextView(Context context) {
        super(context);
        init();
    }

    public VerticalMarqueeTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setSingleLine();
        setEllipsize(TextUtils.TruncateAt.MARQUEE);
        setMarqueeRepeatLimit(-1);
        setFocusable(true);
        setFocusableInTouchMode(true);
        setHorizontallyScrolling(true);
        setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.translate(0, offsetY);
        super.onDraw(canvas);
    }

    @Override
    public boolean isFocused() {
        return true;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        post(new Runnable() {
            @Override
            public void run() {
                offsetY -= 1;
                if (offsetY <= -getHeight()) {
                    offsetY = 0;
                }
                invalidate();
                postDelayed(this, 20);
            }
        });
    }
}

到了這里,關(guān)于Android 實(shí)現(xiàn)跑馬燈效果的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 使用Vue.js實(shí)現(xiàn)文字跑馬燈效果

    使用Vue.js實(shí)現(xiàn)文字跑馬燈效果

    實(shí)現(xiàn)文字跑馬燈效果,首先用到 substring()截取 和 setInterval計(jì)時(shí)器 clearInterval()清除計(jì)時(shí)器 效果如下: 實(shí)現(xiàn)代碼如下: 以上是實(shí)現(xiàn)文字跑馬燈效果,如有不足的地方,歡迎在評(píng)論區(qū)留言。

    2023年04月19日
    瀏覽(94)
  • 探究前端的跑馬燈效果是如何用css實(shí)現(xiàn)的

    探究前端的跑馬燈效果是如何用css實(shí)現(xiàn)的

    ?? 作者簡(jiǎn)介:大家好,我是阿牛,全棧領(lǐng)域優(yōu)質(zhì)創(chuàng)作者?? ?? 個(gè)人主頁(yè):館主阿牛?? ?? 支持我:點(diǎn)贊??+收藏??+留言?? ?? 系列專欄:前端實(shí)用小demo?? ??格言:迄今所有人生都大寫(xiě)著失敗,但不妨礙我繼續(xù)向前!?? 無(wú)意見(jiàn)看到了一個(gè)網(wǎng)站的一個(gè)動(dòng)畫(huà)的跑馬燈效果

    2024年04月10日
    瀏覽(32)
  • flutter開(kāi)發(fā)實(shí)戰(zhàn)-實(shí)現(xiàn)marquee根據(jù)文本長(zhǎng)度顯示文本跑馬燈效果

    flutter開(kāi)發(fā)實(shí)戰(zhàn)-實(shí)現(xiàn)marquee根據(jù)文本長(zhǎng)度顯示文本跑馬燈效果

    flutter開(kāi)發(fā)實(shí)戰(zhàn)-實(shí)現(xiàn)marquee文本跑馬燈效果 最近開(kāi)發(fā)過(guò)程中需要marquee文本跑馬燈效果,這里使用到了flutter的插件marquee 效果圖如下 1.1 引入marquee 在pubspec.yaml中引入marquee 1.2 marquee使用 marquee使用也是非常方便的。比如直接指定文本text 或者設(shè)置更多屬性值 根據(jù)Text文本的大小判斷

    2024年02月13日
    瀏覽(28)
  • uni小程序 跑馬燈效果

    寫(xiě)在前面 前幾天幫一個(gè)朋友咋小程序上加一個(gè)類似于跑馬燈的效果,本自己手寫(xiě)了一個(gè)。(代碼和截圖都在下方) 效果展示 等我截圖~~~ 代碼展示(布局代碼) 主要就是圖片css哪里加了一個(gè)“ flex-shrink: 0; ”,因?yàn)橹皇潜镜氐囊粋€(gè)功能,所以我就圖片數(shù)據(jù)就寫(xiě)死了,需要的自

    2024年02月12日
    瀏覽(25)
  • 前端原生 CSS 跑馬燈效果,無(wú)限輪播(橫豎版本,帶漸變遮罩,簡(jiǎn)單實(shí)用)
  • 應(yīng)廣單片機(jī)實(shí)現(xiàn)跑馬燈

    ? ? ? ? 應(yīng)廣單片機(jī)處處體現(xiàn)其mini的特性,非常適合做各種方案開(kāi)發(fā),特別是點(diǎn)燈,什么跑馬燈,氛圍燈,遙控?zé)?,感?yīng)燈,拍拍燈等,用應(yīng)廣都OK。 ? ? ? ?跑馬燈是基礎(chǔ)中的基礎(chǔ),我搭了一個(gè)框架,要進(jìn)行擴(kuò)展或是修改也很容易。不多說(shuō),上代碼。 #include?? ?\\\"extern.h\\\"

    2024年02月09日
    瀏覽(21)
  • 應(yīng)廣單片機(jī)跑馬燈實(shí)現(xiàn)--阻塞式編程模式

    ? ? ? ?我這邊再寫(xiě)了一個(gè)跑馬燈程序,使用阻塞式編程模式,看起來(lái)會(huì)更簡(jiǎn)單直觀。對(duì)于初學(xué)者來(lái)說(shuō),阻塞式編程比較直觀好理解。在一些任務(wù)單一或是任務(wù)不多的程序來(lái)說(shuō),還是不錯(cuò)的選擇。但是建議還是采用任務(wù)式/查詢式編程,這樣使程序會(huì)有更好的擴(kuò)展性能,減少推

    2024年02月09日
    瀏覽(45)
  • Qt+C++實(shí)現(xiàn)燈帶動(dòng)畫(huà)運(yùn)動(dòng)位置變換移動(dòng)跑馬燈圖片輪播

    Qt+C++實(shí)現(xiàn)燈帶動(dòng)畫(huà)運(yùn)動(dòng)位置變換移動(dòng)跑馬燈圖片輪播

    ?程序示例精選 Qt+C++實(shí)現(xiàn)燈帶動(dòng)畫(huà)運(yùn)動(dòng)位置變換移動(dòng)跑馬燈圖片輪播 如需安裝運(yùn)行環(huán)境或遠(yuǎn)程調(diào)試,見(jiàn)文章底部個(gè)人 QQ 名片,由專業(yè)技術(shù)人員遠(yuǎn)程協(xié)助! 這篇博客針對(duì)Qt+C++實(shí)現(xiàn)燈帶動(dòng)畫(huà)運(yùn)動(dòng)位置變換移動(dòng)跑馬燈圖片輪播編寫(xiě)代碼,代碼整潔,規(guī)則,易讀。 學(xué)習(xí)與應(yīng)用推薦首

    2024年02月13日
    瀏覽(33)
  • 單片機(jī)(3)跑馬燈,按鈕控制的跑馬燈(2種編程)

    單片機(jī)(3)跑馬燈,按鈕控制的跑馬燈(2種編程)

    ?先上電路圖(圖示的是高電平點(diǎn)亮的跑馬燈) ?這個(gè)是程序截圖(keil5):我的建議是是先自己打一遍,邊打邊試著理解程序的意思。 下面的是沒(méi)有注釋的代碼 下一個(gè)是另外一種編程思路 ?

    2024年02月11日
    瀏覽(27)
  • 3.跑馬燈

    3.跑馬燈

    推挽模式輸出:因?yàn)長(zhǎng)ED0和LED1陽(yáng)極都是3.3V,需要將陰極設(shè)置為低電平才可以點(diǎn)亮LED; 操作io口時(shí),必須引入源文件和頭文件; 關(guān)于時(shí)鐘的文件存放在rcc中; void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct); 作用:初始化一個(gè)或多個(gè)io口(同一組)的工作方式和速度, 該函數(shù)

    2024年02月10日
    瀏覽(23)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包