最简单翻页效果

/**
 * zhaoerlei
 * Camera绘制是以圆点开始旋转,所有我们必须用Canvas先移动到图片中心位置在继续,
 * canvas动画是倒序,所以先写回来的动画在写移动的动画
 * Cameraycanvas相反的。
 */

public class Aimmer extends View {
    private Bitmap bitmap;
    private Camera camera=new Camera();
    private int degree;
    private ObjectAnimator animator=ObjectAnimator.ofInt(this,"degree",0,180);
    private Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
    public Aimmer(Context context) {
        this(context,null);
    }

    public Aimmer(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public Aimmer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        bitmap= BitmapFactory.decodeResource(getResources(), R.mipmap.maps);
        animator.setDuration(2500);
        animator.setInterpolator(new LinearInterpolator());//匀速
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setRepeatMode(ValueAnimator.REVERSE);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        animator.end();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        animator.start();
    }

    public void setDegree(int degree) {
        this.degree = degree;
        invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int height=getHeight();
        int wiht=getWidth();
        int wightbimap=bitmap.getWidth()/2;
        int heightbimap=bitmap.getHeight()/2;
        int left = (getWidth() - bitmap.getWidth()) / 2;
        int top = (getHeight() - bitmap.getHeight()) / 2;

        int bitmapWith=bitmap.getWidth();
        int bitmapHeight=bitmap.getHeight();
        int cenWith=getWidth()/2;
        int centerX = getWidth() / 2;//画布信息
        int centerY = getHeight() / 2;
        int x=cenWith-bitmapWith/2;
        int y=centerY-bitmapHeight/2;
        // 第一遍绘制:上半部分
        canvas.save();
        canvas.clipRect(x,y,x+bitmapWith, y+heightbimap);//left, top, right, bottom//x+bitmapWith, y+bitmapHeight/2
        canvas.drawBitmap(bitmap, x, y, paint);
        canvas.restore();

//        // 第二遍绘制:下半部分
        canvas.save();
        if (degree < 90) {
            canvas.clipRect(x, y+heightbimap, x+bitmapWith, y+bitmapHeight);
        } else {
            canvas.clipRect(x,y,x+bitmapWith, y+heightbimap);//绘制保持原样子,因为没有重制,所以可以跟原来以押宝牛股;
        }
        //camera旋转中间是以view圆点,所以必须把图片中间移动到圆点位置;动画是先执行后面的在执行前面的;
        camera.save();
        camera.rotateX(degree);
        canvas.translate(centerX, centerY);
        camera.applyToCanvas(canvas);//投影到画布上
        canvas.translate(-centerX, -centerY);
        camera.restore();
        canvas.drawBitmap(bitmap, x, y, paint);
        canvas.restore();


    }

你可能感兴趣的:(最简单翻页效果)