Android做一个跑马灯的效果

布局:

使用一个系统自带控件 //需要设置两个动画属性 分别为  anim_come开始  和  anim_get结束

<ViewFlipper
    android:id="@+id/text_middle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:autoStart="true"
    android:flipInterval="2000"
    android:inAnimation="@anim/anim_come"
    android:outAnimation="@anim/anim_get"></ViewFlipper>

开始结束动画

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="100%p"
        android:toYDelta="0"
        android:duration = "1000"></translate>
</set>//开始
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="0"
        android:toYDelta="-100%p"
        android:duration="1000"/>
</set>//结束

逻辑:

public class ShowActivity extends AppCompatActivity {
    private ViewFlipper textMiddle;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        //初始化控件
        textMiddle =  findViewById(R.id.text_middle);
       	//动态添加跑马灯的布局
        for (int i = 1; i <= 4; i++) {
            if(i==1){
                View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.layout_custom,null);
                textMiddle.addView(view);
            }else if(i==2){
                View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.layout_custom2,null);
                textMiddle.addView(view);
            }else if(i==3){
                View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.layout_custom3,null);
                textMiddle.addView(view);
            }else {
                View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.layout_custom4,null);
                textMiddle.addView(view);
            }
        }
}

走马灯布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_margin="20dp">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="床前明月光"
        android:textSize="20sp"
        android:textColor="#f00"/>
</LinearLayout>

ok一个简单的走马灯效果就完成了

猜你喜欢

转载自blog.csdn.net/yz1743585120/article/details/83991491