Android 自定义View实现跑马灯效果。没有任何焦点和长度限制。
来源:互联网 发布:ai高版本软件 编辑:程序博客网 时间:2024/06/07 04:02
项目中因为需要用一个跑马灯的效果,我们都知道Android的textView 是可以实现跑马灯的效果的,但是有一些必要的条件,例如:文字的长度必须小于Textview的宽度,TextView必须有焦点,,这样在实际开发中当然是不可行的。于是就需要我们自己去自定义了。
先来张原理图:
下面是代码(Ps:代码是参考别人的:原文链接:http://www.cnblogs.com/hsbd-zyb/p/5849964.html,亲测有用,当然网上也有很多别的方法,但是都各有优缺点,这个是我自己用起来没有出现任何问题的。):
import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.os.Parcel;import android.os.Parcelable;import android.util.AttributeSet;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import com.hisw.sjrd.utils.ScreenUtil;public class AutoScrollTextView extends android.support.v7.widget.AppCompatTextView implements OnClickListener { public final static String TAG = AutoScrollTextView.class.getSimpleName(); private float textLength = 0f;//文本长度 private float viewWidth = 0f; private float step = 0f;//文字的横坐标 private float y = 0f;//文字的纵坐标 private float temp_view_plus_text_length = 0.0f;//用于计算的临时变量 private float temp_view_plus_two_text_length = 0.0f;//用于计算的临时变量 public boolean isStarting = false;//是否开始滚动 private Paint paint = null;//绘图样式 private String text = "";//文本内容 /** 字幕字体颜色 */ private int mTextColor; /** 字幕字体大小 */ private float mTextSize; public AutoScrollTextView(Context context) { super(context); initView(); } public AutoScrollTextView(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(); } private void initView() { setOnClickListener(this); } public void init(Context context) { paint = getPaint(); text = getText().toString(); mTextColor = getCurrentTextColor(); mTextSize = getTextSize(); paint.setColor(mTextColor); paint.setTextSize(mTextSize); textLength = paint.measureText(text); viewWidth = getWidth(); Log.e("zmm","--->"+textLength+"--->"+viewWidth+"-->"+getTextSize()); if(viewWidth == 0) { viewWidth = ScreenUtil.getScreenWidth(context);//获取屏幕的宽度 } Log.e("zmm","屏幕的宽度-->"+ ScreenUtil.getScreenWidth(context)); step = textLength;//文字真实的长度 temp_view_plus_text_length = viewWidth + textLength; temp_view_plus_two_text_length = viewWidth + textLength * 2;//文字移动的距离应该是控件的长度+左边一个文字的长度+右边一个文字的长度 y = getTextSize() + getPaddingTop(); } @Override public Parcelable onSaveInstanceState() { Parcelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); ss.step = step; ss.isStarting = isStarting; return ss; } @Override public void onRestoreInstanceState(Parcelable state) { if (!(state instanceof SavedState)) { super.onRestoreInstanceState(state); return; } SavedState ss = (SavedState)state; super.onRestoreInstanceState(ss.getSuperState()); step = ss.step; isStarting = ss.isStarting; } public static class SavedState extends BaseSavedState { public boolean isStarting = false; public float step = 0.0f; SavedState(Parcelable superState) { super(superState); } @Override public void writeToParcel(Parcel out, int flags) { super.writeToParcel(out, flags); out.writeBooleanArray(new boolean[]{isStarting}); out.writeFloat(step); } public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() { public SavedState[] newArray(int size) { return new SavedState[size]; } @Override public SavedState createFromParcel(Parcel in) { return new SavedState(in); } }; private SavedState(Parcel in) { super(in); boolean[] b = null; in.readBooleanArray(b); if(b != null && b.length > 0) isStarting = b[0]; step = in.readFloat(); } } public void startScroll() { isStarting = true; invalidate(); } public void stopScroll() { isStarting = false; invalidate(); } @Override public void onDraw(Canvas canvas) { canvas.drawText(text, temp_view_plus_text_length - step, y, paint); if(!isStarting) { return; } step += 0.5;//0.5为文字滚动速度。 if(step > temp_view_plus_two_text_length) step = textLength; invalidate(); } @Override public void onClick(View v) { if(isStarting) stopScroll(); else startScroll(); }}
然后在Activity中
tvRun.init(context); tvRun.startScroll();
布局文件中:
<com.hisw.sjrd.widgets.AutoScrollTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_run" android:text="text" android:textSize="18sp" android:textColor="@color/color_red" />
注意:在调用完setText方法之后,需要再调用一下init方法,重新进行初始化和相关参数的计算。
至此跑马灯的效果就实现了,感谢文中提到的博主。
每日一语录:
你失去的,最终都会以不同的方式回到你的身边。。。
加油!!!!
阅读全文
0 0
- Android 自定义View实现跑马灯效果。没有任何焦点和长度限制。
- android 自定义view实现跑马灯效果
- Android 自定义View实现竖直跑马灯效果
- Android wiki:05.跑马灯文字流动效果(自定义带焦点的view对象)
- 自定义view实现跑马灯效果
- 自定义view 实现跑马灯效果
- android 跑马灯效果 自定义View
- Android自定义不需要焦点的TextView以实现跑马灯效果
- 自定义View-跑马灯效果
- Android自定义TextView实现跑马灯效果
- Android 自定义textview实现跑马灯效果
- listview实现跑马灯效果,和焦点获取
- 自定义View—实现滚动TextView(跑马灯)效果
- 自定义ListView实现任意View跑马灯效果
- 自定义View实现文本水平方向的跑马灯效果
- 自定义View实现文字跑马灯效果(垂直滚动和水平滚动)
- android 自定义跑马灯(不限制字体长度且字体居中)
- android自定义的TextView,实现跑马灯效果
- mybatis逆向工程外键映射成相应的对象解决方法
- 用图来理解Java
- 视频教程:Java七大外企经典面试套路之基础篇
- Unity3D游戏开发之Unity3D场景编辑器扩展开发
- RxJava 2.0 从入门到精通(一)
- Android 自定义View实现跑马灯效果。没有任何焦点和长度限制。
- 每周荐书:Web扫描、HTML 5、Python(评论送书)
- Open and run Grasshopper from a batch file 用批处理命令自动打开指定的3dm文件和GH文件
- Python学习笔记(15)-Python代码转换为exe可执行程序详解
- ubuntu 设置su,sudo不用输入密码
- 跳跃表原理
- 调用FTPClient 的retrieveFileStream(String remote)方法后的处理
- LINUX中mysql命令
- (搬运).net起步 (用配置文件属性维护网站用户信息 2)