android實(shí)現(xiàn)滾動(dòng)文本效果
本文實(shí)例為大家分享了android實(shí)現(xiàn)滾動(dòng)文本效果的具體代碼,供大家參考,具體內(nèi)容如下
效果圖

實(shí)現(xiàn)方法
直接上代碼
首先是一個(gè)自定義layout,繼承自FrameLayout
public class AnimationTextLayout extends FrameLayout {
private static final String TAG = "AnimationTextLayout";
private List<String> tipList;
private List<Integer> displayList;
private List<TextView> viewList;
private List<VirtualPos> virtualPosList;
private double deviantAngle = 0;
public AnimationTextLayout(@NonNull Context context) {
super(context);
initView();
}
public AnimationTextLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView();
}
public AnimationTextLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
viewList = new ArrayList<>();
displayList = new ArrayList<>();
virtualPosList = new ArrayList<>();
}
/**
* 設(shè)置需要顯示的數(shù)據(jù)
*
* @param data 需要顯示的數(shù)據(jù)
*/
public void setData(List<String> data) {
this.tipList = data;
initTips();
}
private void initTips() {
while (tipList.size() > viewList.size()) {
addTipsView();
}
refreshTips();
initVirPos();
post(new Runnable() {
@Override
public void run() {
initPosition();
requestLayout();
}
});
}
/**
* 計(jì)算虛擬位置
*/
private void initVirPos() {
virtualPosList.clear();
for (int i = 0; i < viewList.size(); i++) {
double angle = (Math.PI * ((double) i / viewList.size())*2)+(deviantAngle*Math.PI*2);
if (angle>Math.PI*2){
angle-=Math.PI*2;
}
VirtualPos virtualPos = new VirtualPos();
virtualPos.text = tipList.get(i);
virtualPos.z = 100 * Math.sin(angle);
virtualPos.y = 100 * Math.cos(angle);
virtualPosList.add(virtualPos);
}
}
/**
* 將虛擬位置轉(zhuǎn)化為實(shí)際高度和位置
*/
private void initPosition() {
for (int i = 0; i < viewList.size(); i++) {
TextView textView = viewList.get(i);
VirtualPos virtualPos=virtualPosList.get(i);
int realY = (int) ((100 - virtualPos.y)/200 *getMeasuredHeight());
FrameLayout.LayoutParams layoutParams = (LayoutParams) textView.getLayoutParams();
layoutParams.topMargin = realY;
if (virtualPos.z>=0){
float textSize= (float) (( virtualPos.z)/100)*20+5;
textView.setTextSize(textSize);
textView.setVisibility(VISIBLE);
}else {
textView.setVisibility(GONE);
}
Log.d(TAG, "initPosition: y=="+realY);
}
}
private void refreshTips() {
for (int i = 0; i < viewList.size(); i++) {
TextView tip = viewList.get(i);
if (i < tipList.size()) {
tip.setVisibility(VISIBLE);
tip.setText(tipList.get(i));
continue;
}
tip.setVisibility(GONE);
}
}
private TextView addTipsView() {
TextView textView = new TextView(getContext());
textView.setTextSize(COMPLEX_UNIT_DIP, 12);
textView.setTextColor(Color.parseColor("#444444"));
textView.setPadding(ConvertUtil.dp2px(5), ConvertUtil.dp2px(3), ConvertUtil.dp2px(5), ConvertUtil.dp2px(3));
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity=Gravity.CENTER_HORIZONTAL;
layoutParams.rightMargin = ConvertUtil.dp2px(6);
addView(textView, layoutParams);
viewList.add(textView);
return textView;
}
/**
* 虛擬位置,最大x,y,z 最大值為100,最小值為-100
*/
public static class VirtualPos {
public double x;
public double y;
public double z;
public String text;
}
/**
* 滾動(dòng)的偏移值
* @param deviantAngle 最大為1
*/
public void setDeviantAngle(float deviantAngle) {
this.deviantAngle = deviantAngle;
initVirPos();
initPosition();
}
public double getDeviantAngle() {
return deviantAngle;
}
}
調(diào)用方布局
<FrameLayout 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="match_parent" android:layout_height="match_parent" tools:context=".AnimationTextActivity"> <com.lanlengran.test.view.AnimationTextLayout android:id="@+id/anim_text_layout" android:layout_width="match_parent" android:background="@color/colorAccent" android:layout_height="400dp"/> </FrameLayout>
調(diào)用方代碼
public class AnimationTextActivity extends Activity {
private AnimationTextLayout mAnimTextLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation_text);
mAnimTextLayout = findViewById(R.id.anim_text_layout);
List<String> testData = new ArrayList<>();
for (int i = 0; i < 30; i++) {
testData.add("測(cè)試數(shù)據(jù)" + i);
}
mAnimTextLayout.setData(testData);
ObjectAnimator animator = ObjectAnimator.ofFloat(mAnimTextLayout, "deviantAngle", 0f, 1f);
animator.setDuration(5000);
animator.setRepeatCount(-1);
animator.start();
}
}
注意事項(xiàng)
此處的動(dòng)畫(huà)只是為了演示??梢愿鶕?jù)需要改變自定view的改變滾動(dòng)值的方法,就可以使?jié)L輪滾動(dòng)。例如將滾動(dòng)的角度和手指拖動(dòng)相結(jié)合啥的
/** * 滾動(dòng)的偏移值 * @param deviantAngle 最大為1 */ public void setDeviantAngle(float deviantAngle)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Jetpack?Compose?的新型架構(gòu)?MVI使用詳解
這篇文章主要介紹了Jetpack?Compose?的新型架構(gòu)?MVI使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android開(kāi)發(fā)中的錯(cuò)誤及解決辦法總結(jié)
本文屬于個(gè)人平時(shí)項(xiàng)目開(kāi)發(fā)過(guò)程遇到的一些問(wèn)題,記錄下來(lái)并總結(jié)解決方案,希望能幫到大家解決問(wèn)題,需要的朋友可以參考下2022-02-02
Flutter應(yīng)用程序?qū)崿F(xiàn)隱私屏幕示例解析
這篇文章主要為大家介紹了Flutter應(yīng)用程序?qū)崿F(xiàn)隱私屏幕示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
淺談android組件化之ARouter簡(jiǎn)單使用
本篇文章主要介紹了淺談android組件化之ARouter簡(jiǎn)單使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
Android開(kāi)發(fā)之button事件監(jiān)聽(tīng)簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android開(kāi)發(fā)之button事件監(jiān)聽(tīng)實(shí)現(xiàn)方法,實(shí)例分析了事件監(jiān)聽(tīng)的使用技巧與注意事項(xiàng),需要的朋友可以參考下2015-05-05
Android進(jìn)程間使用Intent進(jìn)行通信
Android進(jìn)程間通信(IPC,Inter-Process Communication)底層采用的是 Binder 機(jī)制,具體到應(yīng)用層有網(wǎng)友根據(jù)安卓四大組件將進(jìn)程間通信方式分為對(duì)應(yīng)的四種方式 Activity, Broadcast, ContentProvider, Service2023-02-02
Android 動(dòng)態(tài)改變SeekBar進(jìn)度條顏色與滑塊顏色的實(shí)例代碼
在上次android開(kāi)發(fā)的項(xiàng)目中遇到個(gè)這樣的需求,要?jiǎng)討B(tài)改變seekbar進(jìn)度條顏色與滑塊顏色的需求,實(shí)現(xiàn)代碼也算比較簡(jiǎn)單,對(duì)實(shí)現(xiàn)過(guò)程感興趣的朋友可以通過(guò)本文學(xué)習(xí)下2016-11-11
Android 實(shí)現(xiàn)界面刷新的幾種方法
這篇文章主要介紹了Android 實(shí)現(xiàn)界面刷新的相關(guān)資料,這里提供了幾種方法及實(shí)例代碼,具有一定的參考價(jià)值,需要的朋友可以參考下2016-11-11

