AnimatedPathView
介绍:
根据设置的path路径绘制图形的自定义view。结合属性动画就可以实现路径动画效果。AnimatedPathView的原理是用了DashPathEffect,DashPathEffect能将离散的点形成的路径模拟成连续的线条,这是一个非常有用的类。
运行效果:
使用说明:
关于path的使用请看这篇文章 Android画图之Path类的使用
package com.mattkula.animatedpathview.sample;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.LinearInterpolator;
import com.mattkula.animatedpathview.library.AnimatedPathView;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final AnimatedPathView view = (AnimatedPathView)findViewById(R.id.animated_path);
ViewTreeObserver observer = view.getViewTreeObserver();
if(observer != null){
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
float\[\]\[\] points = new float\[\]\[\]{
{0, 0},
{view.getWidth(), 0},
{view.getWidth(), view.getHeight()},
{0, view.getHeight()},
{0, 0},
{view.getWidth(), view.getHeight()},
{view.getWidth(), 0},
{0, view.getHeight()}
};
view.setPath(points);
}
});
}
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "percentage", 0.0f, 1.0f);
anim.setDuration(2000);
anim.setInterpolator(new LinearInterpolator());
anim.start();
}
});
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.mattkula.animatedpathview.sample"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.mattkula.animatedpathview.library.AnimatedPathView
android:id="@+id/animated_path"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:strokeColor="@android:color/holo_red_light"
app:strokeWidth="10"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Press top half of screen"
/>
</LinearLayout>
已下载
0