国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

自定義跑馬燈,可控制速度與方向

2019-11-09 17:31:33
字體:
供稿:網(wǎng)友
主要是通過繼承自TextView實現(xiàn)自定義View,
使用drawText方法不斷重繪文字。
xml布局:
<com.ycq.myview.MarqueeText    android:id="@+id/test"    android:layout_width="match_parent"    android:layout_height="50dp"    android:background="#339320"    android:maxLines="1"    android:textColor="#FF00FF"    android:textSize="21sp"/>注意:需要設(shè)置為1行。可在XML中設(shè)置顏色,字號,不要設(shè)置文字內(nèi)容。
Activity調(diào)用:
test = (MarqueeText) this.findViewById(R.id.test);test.setMyContext("11111112225555");test.setL2r(true);
test.setMySpeed(10);test.setOnClickListener(new OnClickListener() {    @Override    public void onClick(View view) {        start();    }});
setL2r設(shè)置方向,默認為從左向右;
setMySpeed設(shè)置速度,默認為5,說明:若速度設(shè)置<0,則默認為1,若>15,則默認15.
@OverridePRotected void onPause() {    super.onPause();    stop();}@Overrideprotected void onResume() {    super.onResume();    start();}@Overrideprotected void onDestroy() {    super.onDestroy();    stop();}public void start() {    test.startScroll();}public void stop() {    test.stopScroll();}銷毀Activity,則不在執(zhí)行。
具體實現(xiàn):
package com.ycq.myview;import android.content.Context;import android.graphics.Canvas;import android.util.AttributeSet;import android.widget.TextView;/** * Created by yi on 2017/2/6. */public class MarqueeText extends TextView implements Runnable {    private int currentScrollX = 0;// 當(dāng)前滾動的位置    private boolean isStop = false;    private int textWidth;    private boolean isMeasure = false;    private String myContext = "";    private int vWidth;    private int mySpeed = 5;    private Boolean l2r = true;    //getPaint()獲取系統(tǒng)畫筆    public MarqueeText(Context context) {        super(context);    }    public MarqueeText(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MarqueeText(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        if (!isMeasure) {// 文字寬度只需獲取一次就可以了            textWidth = (int) getPaint().measureText(myContext);            vWidth = getWidth();            isMeasure = true;        }        float baseline = getHeight() / 2 + getPaint().getTextSize() / 2 - getPaint().getFontMetrics().descent;        canvas.drawText(myContext, currentScrollX, baseline, getPaint());    }    @Override    public void run() {        if (!l2r) {//向左運動            currentScrollX -= mySpeed;// 滾動速度            if (currentScrollX < 0) {                if (Math.abs(currentScrollX) >= textWidth) {                    currentScrollX = vWidth;                }            }        }        if (l2r) {//由左向右運動            currentScrollX += mySpeed;// 滾動速度            if (currentScrollX >= vWidth) {                currentScrollX = -textWidth;            }        }        invalidate();        postDelayed(this, 5);        if (isStop) {            return;        }    }    // 開始滾動    public void startScroll() {        isStop = false;        this.removeCallbacks(this);        post(this);    }    // 停止?jié)L動    public void stopScroll() {        isStop = true;    }    public String getMyContext() {        return myContext;    }    public void setMyContext(String myContext) {        this.myContext = myContext;        textWidth = (int) getPaint().measureText(myContext);    }    public int getMySpeed() {        return mySpeed;    }    public void setMySpeed(int mySpeed) {        this.mySpeed = mySpeed;        if (mySpeed <= 0) {            this.mySpeed = 1;        }        if (mySpeed >= 15) {            this.mySpeed = 15;        }    }    public Boolean getL2r() {        return l2r;    }    public void setL2r(Boolean l2r) {        this.l2r = l2r;    }}
float baseline = getHeight() / 2 + getPaint().getTextSize() / 2 - getPaint().getFontMetrics().descent;        canvas.drawText(myContext, currentScrollX, baseline, getPaint());保證繪制的文字在控件的豎直中線位置。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 佛教| 山丹县| 清远市| 嘉善县| 临猗县| 昆山市| 青龙| 四会市| 台江县| 镇沅| 河曲县| 托里县| 井冈山市| 垫江县| 寿宁县| 永安市| 公主岭市| 资溪县| 龙泉市| 油尖旺区| 建始县| 虞城县| 长宁县| 横峰县| 大渡口区| 方城县| 临西县| 韶山市| 石门县| 安仁县| 齐齐哈尔市| 浦城县| 义马市| 阿鲁科尔沁旗| 郁南县| 新田县| 河西区| 敖汉旗| 荥阳市| 崇左市| 汕头市|