View的渐变动画效果(代码实现)

渐变动画(也叫消失)通常指渐渐的淡出某个UI组件,同时同步地淡入另一个。当App想切换内容或View的情况下,这种动画很有用。渐变简短不易察觉,同时又提供从一个界面到下一个之间流畅的转换。如果在需要转换的时候没有使用任何动画效果,这会使得转换看上去感到生硬而仓促。


  1. xml布局
<LinearLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/click_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击按钮"
        />

        <TextView
            android:id="@+id/content"
            style="?android:textAppearanceMedium"
            android:lineSpacingMultiplier="1.2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="aaaaaaaaaaaaaaaaaaaaaa"
            android:padding="16dp" />

        <ProgressBar android:id="@+id/loading_spinner"
            style="?android:progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"

            />

</LinearLayout>

效果是点击按钮后,渐渐隐藏ProgressBar , 显示 TextView

public class MainActivity extends AppCompatActivity {

    private View mContentView;
    private View mLoadingView;
    private int mShortAnimationDuration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContentView = findViewById(R.id.content);
        mLoadingView = findViewById(R.id.loading_spinner);
        mContentView.setVisibility(View.GONE);

        // Retrieve and cache the system's default "short" animation time.
        mShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);

        findViewById(R.id.click_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //渐变动画
                crossfade();
            }
        });
    }


    private void crossfade() {

        // Set the content view to 0% opacity but visible, so that it is visible
        // (but fully transparent) during the animation.
        mContentView.setAlpha(0f);
        mContentView.setVisibility(View.VISIBLE);

        // Animate the content view to 100% opacity, and clear any animation
        // listener set on the view.
        mContentView.animate()
                .alpha(1f)
                .setDuration(2000)
                .setListener(null);

        // Animate the loading view to 0% opacity. After the animation ends,
        // set its visibility to GONE as an optimization step (it won't
        // participate in layout passes, etc.)
        mLoadingView.animate()
                .alpha(0f)
                .setDuration(2000)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mLoadingView.setVisibility(View.GONE);
                    }
                });
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现一个view的扫光效果,可以使用属性动画结合自定义绘制来实现。步骤如下: 1.创建一个自定义View,并在onDraw方法中绘制需要扫光的图形。 2.使用属性动画,例如ValueAnimator或ObjectAnimator,设置扫光的起点和终点,并在动画更新的回调中调用invalidate()方法,触发onDraw方法重绘。 3.在onDraw方法中根据动画的进度计算每一帧扫光的位置和大小,并绘制扫光。 4.为了让扫光效果更加自然,可以添加一些渐变效果,例如使用RadialGradient或LinearGradient实现扫光渐变。 以下是一个简单的示例代码: ```java public class SweepView extends View { private Paint mPaint; private int mStartX, mStartY, mEndX, mEndY; private int mSweepRadius; private int mMaxRadius; private ValueAnimator mAnimator; public SweepView(Context context) { super(context); init(); } public SweepView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { mPaint = new Paint(); mPaint.setColor(Color.BLUE); mPaint.setStyle(Paint.Style.FILL); mStartX = 0; mStartY = 0; mEndX = getWidth(); mEndY = getHeight(); mMaxRadius = (int) Math.sqrt(getWidth() * getWidth() + getHeight() * getHeight()); mAnimator = ValueAnimator.ofInt(0, mMaxRadius); mAnimator.setDuration(2000); mAnimator.setRepeatCount(ValueAnimator.INFINITE); mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { mSweepRadius = (int) animation.getAnimatedValue(); invalidate(); } }); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int centerX = (mStartX + mEndX) / 2; int centerY = (mStartY + mEndY) / 2; RadialGradient gradient = new RadialGradient(centerX, centerY, mSweepRadius, Color.TRANSPARENT, Color.BLUE, Shader.TileMode.CLAMP); mPaint.setShader(gradient); canvas.drawCircle(centerX, centerY, mSweepRadius, mPaint); } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); mAnimator.start(); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); mAnimator.cancel(); } } ``` 在这个示例中,我们使用RadialGradient实现扫光渐变效果动画每次重复时会从0开始,到达最大半径后再从0开始。在onAttachedToWindow和onDetachedFromWindow方法中启动和停止动画
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值