一直很瞧不上APP里面的跑马灯效果,觉得太low了,看着就不舒服,各种广告
天有不测风云,公司接了一个案子 ,里面就有跑马灯效果 ,这个还好不是很过分的那一种
项目到手直接网上找现成的案例是最好的,能省则省 ,看来看去 ,都达不到自己的效果 ,还是自己撸一串 。
当时参考了这位仁兄的博客 :http://blog.csdn.net/jdsjlzx/article/details/7760346
该博客实现了基本的功能,只有左进右出 ,明显有点反人类 。哈哈,就直接拿过来改了一下 ,添加了两边方向都可以的进出,和速度等相关属性
直接看图,没图说毛啊
代码如下(送佛到西,代码不多,全部献上)
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Button android:id="@+id/start_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="start" android:text="开始" /> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="stop" android:text="停止" /> <Button android:id="@+id/startfor0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="startFor0" android:text="从头开始" /> <demo.reeman.com.runhorselight.MarqueeText android:id="@+id/test" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="可以修改app属性,达到不同的效果" android:textColor="#000000" android:textSize="20dp" app:speed="6" app:type="left_out" /> </LinearLayout>
attrs.xml
自定义相关的属性
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MarqueeText"> <attr name="speed" format="integer" /> <attr name="type"> <enum name="left_out" value="1" /> <enum name="right_out" value="2" /> </attr> </declare-styleable> </resources>
核心代码
package demo.reeman.com.runhorselight; import android.content.Context; import android.content.pm.ProviderInfo; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.util.Log; public class MarqueeText extends android.support.v7.widget.AppCompatTextView implements Runnable { private int currentScrollX;// 当前滚动的位置 private boolean isStop = false; private int textWidth; private boolean isMeasure = false; public int timeSpeed = 10; int point; TYPE_STYLE type; enum TYPE_STYLE { //左出,右出两种方式 LEFY_OUT, RIGHT_OUT } public MarqueeText(Context context) { this(context, null); } public MarqueeText(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MarqueeText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MarqueeText); timeSpeed = a.getInteger(R.styleable.MarqueeText_speed, 10); point = a.getInteger(R.styleable.MarqueeText_type, 1); if (point == 1) { type = TYPE_STYLE.LEFY_OUT; } else { type = TYPE_STYLE.RIGHT_OUT; } a.recycle(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (!isMeasure) {// 文字宽度只需获取一次就可以了 getTextWidth(); isMeasure = true; } } /** * 获取文字宽度 */ private void getTextWidth() { Paint paint = this.getPaint(); String textDesc = this.getText().toString(); textWidth = (int) paint.measureText(textDesc); } @Override public void run() { if (type == TYPE_STYLE.LEFY_OUT) { currentScrollX += 1; scrollTo(currentScrollX, 0); if (isStop) { return; } if (getScrollX() >= textWidth) { Log.i("main", "====换行==" + currentScrollX + "/===" + textWidth + "//===" + getWidth()); scrollTo(-getWidth(), 0); currentScrollX = -getWidth(); } } else if (type == TYPE_STYLE.RIGHT_OUT) { currentScrollX -= 1; // 滚动速度 scrollTo(currentScrollX, 0); if (isStop) { return; } if (getScrollX() <= -(this.getWidth())) { scrollTo(textWidth, 0); currentScrollX = textWidth; } } postDelayed(this, timeSpeed); } // 开始滚动 public void startScroll() { isStop = false; this.removeCallbacks(this); post(this); } // 停止滚动 public void stopScroll() { isStop = true; } // 从头开始滚动 public void startForStart() { currentScrollX = 0; startScroll(); } }
主界面代码 ,还是保持了一贯的简洁,通俗
public class MainActivity extends AppCompatActivity { private MarqueeText test; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); test = (MarqueeText) this.findViewById(R.id.test); } public void start(View v) { test.startScroll(); } public void stop(View v) { test.stopScroll(); } public void startFor0(View v) { test.setText("一切尽在掌握之中"); test.startForStart(); } }