高级视图动画的Andr​​oid [英] Advanced view animation in Android

查看:174
本文介绍了高级视图动画的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发与美丽为Android的应用程序,但难以实现设计与吨的自定义动画。现在,我需要实现动画按钮,它必须是动态一目了然动画会影响按钮边框和它的文本,并且必须从左至右。这里是这样的动画在IOS应用的一个例子。

I'm developing an app for Android with beautiful, but hard-to-implement design with tons of custom animations. Now I need to implement animation for button, it must be dynamic glance animation that affects both button border and its text, and must move from left to right. Here is an example of such animation in iOS app.

所以,我希望你擦肩而过的主要思想。我试图用房产动画,但它不是我真正需要的。此外,我发现了许多自定义库,并尝试了很多从SDK中的例子,但也有没有类似的。

So I hope you catched the main idea. I tried to use Property Animation, but it's not what I really need. Also I found many custom libraries and tried a lot of examples from SDK, but there are nothing similar.

所以,如果你知道如何可以做到,回答我。

So if you know how it can be do, answer me.

推荐答案

试试这个包装类:

class FL extends FrameLayout implements ValueAnimator.AnimatorUpdateListener {
    private static final int W = 200;
    private final LinearGradient gradient;
    private final Paint paint;
    private float x;

    public FL(View child) {
        super(child.getContext());
        addView(child);
        int[] colors = {0, 0xaaffffff, 0};
        gradient = new LinearGradient(0, 0, W, 0, colors, null, Shader.TileMode.CLAMP);
        paint = new Paint();
        paint.setShader(gradient);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        x = -W;
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.saveLayer(null, null, 0);
        super.dispatchDraw(canvas);
        canvas.translate(x, 0);
        float h = getHeight();
        canvas.rotate(20, W / 2, h / 2);
        canvas.drawRect(0, -h, W, 2 * h, paint);
        canvas.restore();
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        x = (int) animation.getAnimatedValue();
        invalidate();
    }

    public void startMagic() {
        ValueAnimator a = ValueAnimator.ofInt(-W, getWidth());
        a.addUpdateListener(this);
        a.setDuration(1000).setInterpolator(new AccelerateDecelerateInterpolator());
        a.start();
    }
}

round_frame.xml是:

round_frame.xml is:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="20dp" />
    <stroke android:color="#a00" android:width="4dp" />
</shape>

和与在的onCreate以下测试它

and test it with the following in onCreate:

    Button b = new Button(this);
    final FL fl = new FL(b);
    b.setTextSize(28);
    b.setTextColor(0xffaa0000);
    b.setText("click me to see the magic");
    b.setBackgroundResource(R.drawable.round_frame);
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fl.startMagic();
        }
    };
    b.setOnClickListener(listener);
    addContentView(fl, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

注意:如果你想只使用它的按钮,你没有一个FL包装:您可以扩展Button类,并覆盖绘制方法和我一样在佛罗里达州的包装类

NOTE: if you want it to use it in the Button only, you don't have a FL wrapper: you can extend Button class and override drawing methods as i did in FL wrapper class

这篇关于高级视图动画的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆