Bitmap和Drawable互转以及属性动画

一,属性动画

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);

    }

猜你喜欢

转载自blog.csdn.net/eyishion/article/details/103085015