gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:44:42 25 4
gpt4 key购买 nike

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

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

图片 1:

enter image description here

图片 4:

enter image description here

图片 5:

enter image description here

最佳答案

您可以使用 SurfaceView 来完成此操作。

只需在 Canvas 上绘制位图的特定部分,并在绘制线程的 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 或位图传递到此 View ,因为此示例使用硬编码值。您还可以添加不同的方法来重新启动动画、按需播放动画以及不同的自定义行为。

另请参阅我的关于在解码图像资源时如何防止 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
}

关于java - android 裁剪图像扩展到其原始裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52673829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com