Android开发一个TextView像一个画卷一样从右边卷到左边这样消失掉的效果有示例代码吗
以下是一个示例代码,实现了一个TextView从右边卷到左边消失的效果:
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animateTextView();
}
});
}
private void animateTextView() {
ObjectAnimator animator = ObjectAnimator.ofFloat(textView, "translationX", 0, -textView.getWidth());
animator.setDuration(1000);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
textView.setVisibility(View.INVISIBLE);
textView.setTranslationX(0);
}
});
animator.start();
}
}
在XML布局文件中,需要添加一个TextView:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="24sp"
android:padding="16dp"/>
在上述代码中,我们使用ObjectAnimator实现了一个平移动画,将TextView从初始位置移动到目标位置。在动画结束时,我们将TextView设置为不可见,并将其平移位置重置为0,以便下一次动画播放
原文地址: https://www.cveoy.top/t/topic/iYDM 著作权归作者所有。请勿转载和采集!