Android:Marquee开始位置和变化速度

时间:2016-03-07 15:48:57

标签: android marquee

我有这个用于创建选框的代码,我可以在激活时更改它的速度和它的开始位置吗?我写了这些代码,当我点击按钮时,它会启动中心的选框。

<TextView
    android:id="@+id/mywidget"
    android:layout_width="fill_parent"
    android:textSize="20dp"
    android:layout_height="wrap_content"
    android:text="This is a test of marquee on the text view in android."
    android:ellipsize="marquee"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:focusable="false"
    android:marqueeRepeatLimit="marquee_forever"
    android:textStyle="bold"
    android:shadowColor="#f5067e"
    android:textColor="#ff000d"
    android:shadowDx="0.0"
    android:shadowDy="0.0"
    android:shadowRadius="8"
    />

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.mywidget);
    textView.setVisibility(View.INVISIBLE);


    anim = (Button) findViewById(R.id.anim);
    anim.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textView.setVisibility(View.VISIBLE);
            textView.setSelected(true);
        }
    });
}

1 个答案:

答案 0 :(得分:2)

我也想改变选框速度和开始位置。 我找到了速度的解决方案。 原帖 - Marquee Set Speed

确保仅在tv.setText()和tv.setSelected(true)之后调用此方法。否则它将无法工作。

public static void setMarqueeSpeed(TextView tv, float speed) {
    if (tv != null) {
        try {
            Field f = null;
            if (tv instanceof AppCompatTextView) {
                f = tv.getClass().getSuperclass().getDeclaredField("mMarquee");
            } else {
                f = tv.getClass().getDeclaredField("mMarquee");
            }
            if (f != null) {
                f.setAccessible(true);
                Object marquee = f.get(tv);
                if (marquee != null) {
                    String scrollSpeedFieldName = "mScrollUnit";
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        scrollSpeedFieldName = "mPixelsPerSecond";
                    }
                    Field mf = marquee.getClass().getDeclaredField(scrollSpeedFieldName);
                    mf.setAccessible(true);
                    mf.setFloat(marquee, speed);
                }
            } else {
                Logger.e("Marquee", "mMarquee object is null.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}