2
我想要使用imageview矩阵随机缩放和旋转图像,请参阅下面的代码。我遇到两个问题,可能会或可能没有关系。使用矩阵运算,部分图像消失并且旋转不起作用
第一:旋转不起作用,图像不出现任何旋转。
第二:缩放的作品,在图像重新调整的范围内。但是,如果比例常数大于1,则图像的一部分被切掉,从而只有部分图像出现在屏幕上。我认为这与imageView的边界有关,但我该如何解决它?
在此先感谢您的帮助!
final ImageView imageView = getImageView(); //Help function that provides imageViews
if(imageView == null) return;
Matrix imageMatrix = imageView.getImageMatrix();
imageMatrix.postRotate(randGen.nextFloat()*360);
float randomScale = randGen.nextFloat()*1.5f + 0.5f;
imageMatrix.postScale(randomScale, randomScale);
imageView.setImageBitmap(usedBitmap);
imageView.setImageMatrix(imageMatrix);
imageView.setScaleType(ScaleType.MATRIX);
RelativeLayout.LayoutParams layPar = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layPar.leftMargin = pCenter.x; //pCenter is the point where the image is drawn.
layPar.topMargin = pCenter.y;
relativeLayout.addView(imageView, layPar);
更新:我现在已经没有这个问题了一段时间,我没有鱼翅使用上述方法解决,所以我试着子类查看代替。这是我的View子类,图像既不旋转也不缩放。我真的不明白为什么这些解决方案不起作用。任何人?
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.View;
public class ScaleRotateView extends View {
Bitmap image;
float rotation;
float scaleX;
float scaleY;
public ScaleRotateView(Context context, Bitmap image, float rotation, float scaleX, float scaleY) {
super(context);
this.image = image;
this.rotation = rotation;
this.scaleX = scaleX;
this.scaleY = scaleY;
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
canvas.drawBitmap(image, 0, 0, new Paint());
Matrix matrix = canvas.getMatrix();
matrix.preRotate(rotation, image.getWidth()/2, image.getHeight()/2);
matrix.setScale(scaleX, scaleY);
canvas.setMatrix(matrix);
//I also tried using canvas.rotate() and canvas.scale, but to no avail.
}
}
我只是想你的代码和旋转运行良好的内部。尽管使用了4.0.4设备。 – pgsandstrom 2012-04-04 13:55:18
我用我的HTC测试,并且旋转不起作用。我希望应用程序能够运行在渴望生成的设备上。我究竟做错了什么?桑迪斯,你有没有体验过没有被画出的部分图像? – 2012-04-12 16:28:02
是的,有时图像的某些部分没有绘制。但是这很可能是因为画布试图在视图边界之外绘制图像,并且这不应该是您的问题,因为您的图像未旋转。对我来说,解决方案就是将图像放在更大的视野中。不幸的是我不知道为什么你的图片不旋转:/ – pgsandstrom 2012-04-12 18:08:22