如何在android中水平切换翻转Imageview
我有一个Imageview,每当用户点击名为翻转图片的按钮时,我想要水平翻转图片。当用户第二次点击此按钮时,它应该返回到原始状态,换句话说就是翻转。
I have an Imageview and I want to flip picture horizontally whenever user click on the Button named "Flip Picture" . and when User click this button second time it should return to orignal state in other words flip back.
所以它应该重复这种行为。我发现这个有用的代码可以在不使用外部库的情况下翻转图像视图,但不知道如何翻转:
So it should repeat this behavior. I found this useful code to flip the image view with out using external library , but do not know how to flip back:
以下是代码:
public Bitmap flipImage(Bitmap src, int type) {
// create new matrix for transformation
Matrix matrix = new Matrix();
// if vertical
if(type == FLIP_VERTICAL) {
// y = y * -1
matrix.preScale(1.0f, -1.0f);
}
// if horizonal
else if(type == FLIP_HORIZONTAL) {
// x = x * -1
// unknown type
} else {
return null;
}
// return transformed image
return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
}
以下是我试图在名为FlipImage的Image视图中应用它的方法
and here is how I am trying to apply it on my Image view named FlipImage
Flipimage.setImageBitmap(flipImage(BitmapFactory.decodeResource(getResources(), R.drawable.doom01),2));
这是一个很好的代码,可以帮助您解决问题问题。将位图图像传递给函数,函数返回位图数据类型。 ...
或 下载源代码
This is a good code that will help you solve your problem. Pass a bitmap image to the function and the function returns a bitmap data type. ... or download source code
public Bitmap FlipHorizontally(Bitmap originalImage) {
// The gap we want between the flipped image and the original image
final int flipGap = 4;
int width = originalImage.getWidth();
int height = originalImage.getHeight();
// This will not scale but will flip on the Y axis
Matrix matrix = new Matrix();
matrix.preScale(-1, 1);
// Create a Bitmap with the flip matrix applied to it.
// We only want the bottom half of the image
Bitmap flipImage = Bitmap.createBitmap(originalImage, 0,0 , width, height, matrix, true);
// Create a new bitmap with same width but taller to fit reflection
Bitmap bitmapWithFlip = Bitmap.createBitmap((width + width + flipGap), height, Bitmap.Config.ARGB_8888);
// Create a new Canvas with the bitmap that's big enough for
Canvas canvas = new Canvas(bitmapWithFlip);
//Draw original image
canvas.drawBitmap(originalImage, 0, 0, null);
//Draw the Flipped Image
canvas.drawBitmap(flipImage, width+flipGap, 0, null);
return bitmapWithFlip;
}