亲宝软件园·资讯

展开

android滚动文本 android实现滚动文本效果

lanlengran蓝冷然 人气:0

效果图

实现方法

直接上代码

首先是一个自定义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<>();
 }

 /**
  * 设置需要显示的数据
  *
  * @param data   需要显示的数据
  */
 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();
   }
  });

 }

 /**
  * 计算虚拟位置
  */
 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);
  }
 }

 /**
  * 将虚拟位置转化为实际高度和位置
  */
 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;
 }

 /**
  * 滚动的偏移值
  * @param deviantAngle 最大为1
  */
 public void setDeviantAngle(float deviantAngle) {
  this.deviantAngle = deviantAngle;
  initVirPos();
  initPosition();
 }

 public double getDeviantAngle() {
  return deviantAngle;
 }
}

调用方布局

<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>

调用方代码

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("测试数据" + i);
  }
  mAnimTextLayout.setData(testData);

  ObjectAnimator animator = ObjectAnimator.ofFloat(mAnimTextLayout, "deviantAngle", 0f, 1f);
  animator.setDuration(5000);
  animator.setRepeatCount(-1);
  animator.start();
 }
}

注意事项

此处的动画只是为了演示。可以根据需要改变自定view的改变滚动值的方法,就可以使滚轮滚动。例如将滚动的角度和手指拖动相结合啥的

 /**
  * 滚动的偏移值
  * @param deviantAngle 最大为1
*/
public void setDeviantAngle(float deviantAngle)

加载全部内容

相关教程
猜你喜欢
用户评论