旋转图片

未使用Camera实现的,其实使用Camera也就几行代码的事。我这叫闲的蛋疼。

package com.czy.roll3dimageview.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.czy.roll3dimageview.R;

/**
 * Created by admin on 2017/4/12.
 */

public class MatrixSetPolyToPolyTest extends View {

    public static final String TAG = MatrixSetPolyToPolyTest.class.getSimpleName();

    private Bitmap mBitmap;             // 要绘制的图片
    private Matrix mPolyMatrix;         // 测试setPolyToPoly用的Matrix

    private int viewWidth,viewHeight;

    private int offsetX = 0,offsetY = 0;

    private ValueAnimator vaX,vaY;

    public MatrixSetPolyToPolyTest(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewWidth = w;
        viewHeight = h;
        scalePic();
        initAnimatoin();
    }

    public void initAnimatoin(){
        vaX = ValueAnimator.ofInt(0,viewWidth);
        vaX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                offsetX = (int) animation.getAnimatedValue();
                invalidate();
            }
        });
        vaY = ValueAnimator.ofInt(0,viewHeight);
        vaY.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                offsetY = (int) animation.getAnimatedValue();
                invalidate();
            }
        });
        vaX.setDuration(2000);
        vaX.setInterpolator(new DecelerateInterpolator());
        vaY.setDuration(2000);
        vaY.setInterpolator(new DecelerateInterpolator());
    }

    public void startRorate(){
        vaX.start();
        vaY.start();
    }

    public void scalePic(){
        // Get the dimensions of the bitmap
        Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.img5);
        mBitmap = Bitmap.createBitmap(viewWidth,viewHeight, Bitmap.Config.RGB_565);
        Rect dstRect = new Rect(getLeft(),getTop(),viewWidth,viewHeight);
        Rect srcRect = new Rect(0,0,srcBitmap.getWidth(),srcBitmap.getHeight());
        Canvas c = new Canvas(mBitmap);
        c.drawBitmap(srcBitmap,srcRect,dstRect,null);
    }

    private void initBitmapAndMatrix() {
//        mBitmap = BitmapFactory.decodeResource(getResources(),
//                R.drawable.img5);

        mPolyMatrix = new Matrix();

        //picture coordinator
        float[] src = {0, 0,                                    // 左上
                viewWidth, 0,                          // 右上
                viewWidth, viewHeight,        // 右下
                0, viewHeight};                        // 左下
        //dst coordinator
        int localY;
        if (offsetY <= viewHeight / 2){
            localY = offsetY;
        }else{
            localY = viewHeight - offsetY;
        }
        float[] dst = {offsetX, 0,                                    // 左上
                viewWidth - offsetX, localY,                        // 右上
                viewWidth - offsetX, viewHeight - localY,  // 右下
                offsetX, viewHeight};                        // 左下

        // 核心要点
        //参数说明
        //float[] src,    //原始数组 src [x,y],存储内容为一组点
        //int srcIndex,     // 原始数组开始位置
        //float[] dst,  // 目标数组 dst [x,y],存储内容为一组点
        //int dstIndex,     // 目标数组开始位置
        //int pointCount)   // 测控点的数量 取值范围是: 0到4
        mPolyMatrix.setPolyToPoly(src, 0, dst, 0, src.length >> 1); // src.length >> 1 为位移运算 相当于处以2

        // 此处为了更好的显示对图片进行了等比缩放和平移(图片本身有点大)
//        mPolyMatrix.postScale(0.5f, 0.5f);
//        mPolyMatrix.postTranslate(0, 200);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initBitmapAndMatrix();
        // 根据Matrix绘制一个变换后的图片
        canvas.drawBitmap(mBitmap, mPolyMatrix, null);
    }
}

你可能感兴趣的:(旋转图片)