一,屬性動畫
1,ViewPropertyAnimator
只能操作系統提供的屬性
比如下面
view.animate()
.translationX(200)
.translationY(100)
.rotation(180)//中心點旋轉
.setStartDelay(1000)
.setDuration(200)
.start();
2,ObjectAnimator
可以定義任意的屬性,來做動畫
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float raidus;
public float getRaidus() {
return raidus;
}
public void setRaidus(float raidus) {
this.raidus = raidus;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(getWidth()/2,getHeight()/2,raidus,paint);
}
讓這個raidus做屬性動畫
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "raidus", 100, 300);
animator.start();
3,PropertyValuesHolder
同時做動畫,同一個View的多個屬性同時做動畫
PropertyValuesHolder bottomAnimHolder = PropertyValuesHolder.ofInt("bottomFilp", 45);
PropertyValuesHolder rotateAnimHolder = PropertyValuesHolder.ofInt("rotateDegree", 270);
PropertyValuesHolder topAnimHolder = PropertyValuesHolder.ofInt("topFilp", -45);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(cameraView, bottomAnimHolder, rotateAnimHolder, topAnimHolder);
animator.setStartDelay(1000);
animator.setDuration(1500);
animator.start();
4,Keyframe
同一個View,同一個屬性,分階段做動畫
float length = UiUtils.dp2px(300);
Keyframe frame1 = Keyframe.ofFloat(0, 0);
Keyframe frame2 = Keyframe.ofFloat(0.2f, 1.5f * length);
Keyframe frame3 = Keyframe.ofFloat(0.8f, 0.8f * length);
Keyframe frame4 = Keyframe.ofFloat(1.0f, 1.0f * length);
PropertyValuesHolder holder = PropertyValuesHolder.ofKeyframe("translationX", frame1, frame2, frame3, frame4);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(cameraView, holder);
animator.setStartDelay(1000);
animator.setDuration(2000);
animator.start();
5,TypeEvaluator
ObjectAnimator animator = ObjectAnimator.ofObject(cameraView, "point", new PointEvaluator(), new Point((int) UiUtils.dp2px(300), (int) UiUtils.dp2px(400)));
animator.setStartDelay(1000);
animator.setDuration(2000);
animator.start();
public class PointEvaluator implements TypeEvaluator<Point> {
@Override
public Point evaluate(float fraction, Point startValue, Point endValue) {
Point point = new Point();
point.x = (int) (startValue.x + (endValue.x - startValue.x) * fraction);
point.y = (int) (startValue.y + (endValue.y - startValue.y) * fraction);
return point;
}
}
二,硬件加速
//對整個View起作用
setLayerType(View.LAYER_TYPE_HARDWARE, null);//開啓離屏緩衝
setLayerType(View.LAYER_TYPE_SOFTWARE, null);//關閉硬件加速
setLayerType(View.LAYER_TYPE_NONE, null);//開啓硬件加速
三,Bitmap和Drawable
Bitmap和Drawable互轉
//drawable convert bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if (bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if ((drawable.getIntrinsicWidth() <= 0) || (drawable.getIntrinsicHeight() <= 0)) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
}
else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
//bitmap convert drawable
public static Drawable bitmapToDrawable(Context context, Bitmap bitmap) {
return new BitmapDrawable(context.getResources(), bitmap);
}