您好,登錄后才能下訂單哦!
要實現ImageView的自定義過渡效果,可以通過使用屬性動畫和自定義View的方式來實現。以下是一個簡單的示例代碼,實現ImageView的自定義過渡效果:
public class CustomTransitionImageView extends AppCompatImageView {
private static final int DEFAULT_DURATION = 500;
private boolean mIsAnimating = false;
private int mDuration = DEFAULT_DURATION;
public CustomTransitionImageView(Context context) {
super(context);
}
public CustomTransitionImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public CustomTransitionImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
private void init(AttributeSet attrs) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTransitionImageView);
mDuration = a.getInt(R.styleable.CustomTransitionImageView_duration, DEFAULT_DURATION);
a.recycle();
}
public void setDuration(int duration) {
mDuration = duration;
}
public void startTransition() {
if (mIsAnimating) {
return;
}
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(this, View.SCALE_X, 0, 1);
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(this, View.SCALE_Y, 0, 1);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(scaleXAnimator, scaleYAnimator);
animatorSet.setDuration(mDuration);
animatorSet.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
mIsAnimating = true;
}
@Override
public void onAnimationEnd(Animator animator) {
mIsAnimating = false;
}
@Override
public void onAnimationCancel(Animator animator) {
mIsAnimating = false;
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
animatorSet.start();
}
}
在xml布局文件中使用CustomTransitionImageView,并設置過渡效果的時間:
<com.example.CustomTransitionImageView
android:id="@+id/customTransitionImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:duration="1000"
android:src="@drawable/image" />
在代碼中調用startTransition方法即可開始過渡效果的動畫:
CustomTransitionImageView customTransitionImageView = findViewById(R.id.customTransitionImageView);
customTransitionImageView.startTransition();
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。