vectalign-samples变形动画_源码分享

我们知道4.4以后AnimatedVectorDrawable可以让两个SVG图像无缝过渡 (称为变形动画),但是这两个svg图像的path必须参数个数要相等,同时这些参数的类型要匹配(也就是说格式要对齐),如果不对齐会产生异常。简单的 path可以手动修改对齐,但是复杂点的就比较难了。这个工具就是通过命令行的方式将任意两个svg资源转换成对齐的模式,而不会改变原始图像的外观。

运行效果:

  • vectalign-samples变形动画_源码分享


完整源码地址:http://www.itlanbao.com/code/20150909/10000/100516.html


package org.bonnyfone.vectalignsamples;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.wnafee.vector.MorphButton;

public class ActivityMainActivity extends AppCompatActivity {

    private ViewGroup parent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_activity_main);
        parent = (ViewGroup) findViewById(R.id.base_view);

        //Default image
        handleMenu(R.id.action_droid_to_apple);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        handleMenu(item.getItemId());
        return true;
    }

    private void handleMenu(int id){
        switch (id){
            case R.id.action_github:
                final String url = "https://github.com/bonnyfone/vectalign";
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);
                break;

            case R.id.action_droid_to_apple:
               swapView(createMorphableView(R.drawable.animated_vector_droid_for_apple, R.drawable.animated_vector_apple, Color.parseColor("#4CAF50")));
                break;

            case R.id.action_heart:
               swapView(createMorphableView(R.drawable.animated_vector_twitter, R.drawable.animated_vector_heart, Color.parseColor("#039BE5")));
                break;

            case R.id.action_cart_to_droid:
               swapView(createMorphableView(R.drawable.animated_vector_cart, R.drawable.animated_vector_droid_for_cart, Color.parseColor("#FF5252")));
                break;

            case R.id.action_icons:
               swapView(createMorphableView(R.drawable.animated_vector_icon_a, R.drawable.animated_vector_icon_b, Color.parseColor("#311B92")));
                break;

            case R.id.action_wifi_to_retwwet:
               swapView(createMorphableView(R.drawable.animated_vector_wifi, R.drawable.animated_vector_retweet, Color.parseColor("#607D8B")));
                break;

        }
    }

    private void swapView(View newView){
        View toRemove = parent.findViewById(R.id.morph_id);
        parent.removeView(toRemove);
        newView.setId(R.id.morph_id);
        parent.addView(newView, toRemove.getLayoutParams());
    }

    private View createMorphableView(int startDrawable, int endDrawable, int color){
        MorphButton mb = new MorphButton(this);
        mb.setForegroundTintList(ColorStateList.valueOf(color));
        mb.setForegroundTintMode(PorterDuff.Mode.MULTIPLY);
        mb.setBackgroundColor(Color.TRANSPARENT);
        mb.setStartDrawable(startDrawable);
        mb.setEndDrawable(endDrawable);
        mb.setState(MorphButton.MorphState.START);
        return  mb;
    }

}

你可能感兴趣的:(vectalign-samples变形动画_源码分享)