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"
/>
這里需要注意下:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-800775.html
- 需要限制textview的寬度,不能設(shè)置為wrap_content
- 啟動(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)!