android裁剪图像扩展到其原始裁剪

时间:2018-10-05 22:09:08

标签: java android image android-studio crop

我想在android中显示一个裁剪的图像,该图像每秒扩展30秒,变成其原始裁剪。如果这没有任何意义,请告诉我,我会尽力解释。

是否可以从URL加载图像?如果可以,怎么办?

图片1:

enter image description here

图片4:

enter image description here

图片5:

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用SurfaceView执行此操作。

只需在画布上绘制位图的特定部分,并在绘制线程的while循环中每次迭代将其扩展即可。

如果我理解正确,那么此代码即可完成工作:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class ExpandView extends SurfaceView implements SurfaceHolder.Callback {

    private DrawThread drawThread;

    public ExpandView(Context context) {
        super(context);
    }

    public ExpandView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getHolder().addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.YOUR_DRAWABLE_ID);

        drawThread = new DrawThread(holder, bitmap);
        drawThread.setRunning(true);
        drawThread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

        boolean retry = true;

        drawThread.setRunning(false);

        while (retry) {
            try {
                drawThread.join();
                retry = false;
            } catch (InterruptedException e) {

            }
        }
    }

    private class DrawThread extends Thread {
        private static final int FRAME_RATE = 30;
        private static final float ANIMATION_SPEED_MILLIS = 10_000;

        private Bitmap newBitmap;
        private int canvasWidth;
        private int canvasHeight;
        private int bitmapWidth;
        private int bitmapHeight;

        private boolean isRunning;
        private final SurfaceHolder surfaceHolder;
        private Bitmap bitmap;
        private long startTime;
        private long lastDrawTime;
        private Paint paint;

        //crop rate should be >=0 and <= 1
        private float startCropRate = 0.2f;
        private float targetCropRate = 1;
        private float currentCropRate;

        private boolean expand;

       private int verticalOffset;
       private int horizontalOffset;
       private int newWidth;
       private int newHeight;
       private int newX;
       private int newY;

        public DrawThread(SurfaceHolder surfaceHolder, Bitmap bitmap) {
            this.surfaceHolder = surfaceHolder;

            canvasWidth = surfaceHolder.getSurfaceFrame().width();
            canvasHeight = surfaceHolder.getSurfaceFrame().height();

            this.bitmap = resizeBitmap(bitmap, canvasHeight, canvasWidth);

            bitmapWidth =  this.bitmap.getWidth();
            bitmapHeight =  this.bitmap.getHeight();

            paint = new Paint(Paint.DITHER_FLAG);

            expand = startCropRate > targetCropRate;
        }

        private Bitmap resizeBitmap(Bitmap bm, int canvasHeight, int canvasWidth) {
            int width = bm.getWidth();
            int height = bm.getHeight();

            float scale = Math.min(canvasWidth/(float)width, canvasHeight/(float)height);

            Matrix matrix = new Matrix();
            matrix.setScale(scale, scale);

            Bitmap resizedBitmap = Bitmap.createBitmap(
                    bm, 0, 0,width,height, matrix, false);
            bm.recycle();

            return resizedBitmap;
        }

        @Override
        public void run() {

            Canvas canvas;
            long currentTime;
            long animationTime;
            float newScale;

            startTime = System.currentTimeMillis();

            while (isRunning) {

                if (currentCropRate == targetCropRate) {
                    isRunning = false;
                    return;
                }

                currentTime = System.currentTimeMillis();

                if (currentTime - lastDrawTime < 1000 / FRAME_RATE) {
                    continue;
                }

                animationTime = currentTime - startTime;


                newScale = startCropRate + (targetCropRate - startCropRate) * (animationTime / ANIMATION_SPEED_MILLIS);

                currentCropRate = expand ? Math.max(targetCropRate, newScale) : Math.min(newScale, targetCropRate);

                lastDrawTime = currentTime;

                canvas = null;

                try {

                    canvas = surfaceHolder.lockCanvas(null);

                    synchronized (surfaceHolder) {

                        canvas.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR);

                        drawImage(canvas);
                    }

                } catch (Exception e) {
                e.printStackTrace();
                } finally {
                    if (canvas != null) {
                        surfaceHolder.unlockCanvasAndPost(canvas);
                    }
                }
            }

        }

        private void drawImage(Canvas canvas) {

            newWidth = (int) (bitmapWidth * currentCropRate);
            newHeight = (int) (bitmapHeight * currentCropRate);

            newX = (bitmapWidth - newWidth) / 2;
            newY = (bitmapHeight - newHeight) / 2;

            newBitmap = Bitmap.createBitmap(bitmap, newX, newY,newWidth,newHeight);

            verticalOffset = (canvasHeight - newBitmap.getHeight()) / 2;
            horizontalOffset =  (canvasWidth - newBitmap.getWidth()) / 2;

            canvas.drawBitmap(newBitmap, horizontalOffset, verticalOffset, paint);
        }


        private void setRunning(boolean running) {
            this.isRunning = running;
        }
    }
}

您需要创建一种方法来将图像资源ID或位图传递给此视图,因为此示例适用于硬编码值。 而且,您可以添加不同的方法来重新启动动画,按需播放动画以及不同的自定义行为。

在这里查看我有关在解码图像资源时如何防止OutOfMemoryError的答案: https://stackoverflow.com/a/52428066/10382361

有一个例子:

Bitmap bitmap = null;

try{
bitmap =  BitmapFactory.decodeResource(getResources(), R.drawable.cleaf5, options);
}catch(OutOfMemoryError e)    
   try{
   BitmapFactory.Options options = new BitmapFactory.Options();
   options.inSampleSize = 8;
   bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.cleaf5, options);
   }catch(OutOfMemoryError e) {}
}

if(bitmap != null){
//do smth
}