Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ImageView绘制位图是否水平翻转?_Android_Bitmap_Imageview - Fatal编程技术网

Android ImageView绘制位图是否水平翻转?

Android ImageView绘制位图是否水平翻转?,android,bitmap,imageview,Android,Bitmap,Imageview,我有一个图像视图和一个位图。我希望ImageView绘制水平镜像的位图。我试着用矩阵来做,就像这样: ImageView iv = ...; Matrix matrix = new Matrix(); iv.setScaleType(ScaleType.MATRIX); matrix.preScale(-1, 1); iv.setImageMatrix(matrix); 但是,使用上述命令后,不会显示ImageView。如果我把上面的注释掉,它看起来还可以。我该怎么做 谢谢 < P>你可以考虑

我有一个图像视图和一个位图。我希望ImageView绘制水平镜像的位图。我试着用矩阵来做,就像这样:

ImageView iv = ...;
Matrix matrix = new Matrix();
iv.setScaleType(ScaleType.MATRIX);
matrix.preScale(-1, 1);
iv.setImageMatrix(matrix);
但是,使用上述命令后,不会显示ImageView。如果我把上面的注释掉,它看起来还可以。我该怎么做


谢谢

< P>你可以考虑尝试将位图传递到图像视图中,而不是翻转图像视图本身。以下是一个例子:

public class Main extends Activity {
 ImageView imViewAndroid;
 public static final int FLIP_VERTICAL = 1;
 public static final int FLIP_HORIZONTAL = 2;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  imViewAndroid = (ImageView) findViewById(R.id.imViewAndroid);
imViewAndroid.setImageBitmap(flipImage(BitmapFactory.decodeResource(getResources(), R.drawable.car),2));
 }
 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
   matrix.preScale(-1.0f, 1.0f);
   // unknown type
  } else {
   return null;
  }

  // return transformed image
  return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
 }
}

这样做有效,翻转后需要偏移图像的宽度:

ImageView iv = ...;
Matrix matrix = new Matrix();
matrix.preScale(-1, 1);
matrix.postTranslate(iv.getLayoutParams().width, 0);

iv.setScaleType(ScaleType.MATRIX); 
iv.setImageMatrix(matrix);

首先从您的可绘制图像中获取位图

BitmapFactory.decodeResource(getResources(), R.drawable.car),2);
然后

public enum Direction { VERTICAL, HORIZONTAL };

/**
    Creates a new bitmap by flipping the specified bitmap
    vertically or horizontally.
    @param src        Bitmap to flip
    @param type       Flip direction (horizontal or vertical)
    @return           New bitmap created by flipping the given one
                      vertically or horizontally as specified by
                      the <code>type</code> parameter or
                      the original bitmap if an unknown type
                      is specified.
**/
public static Bitmap flip(Bitmap src, Direction type) {
    Matrix matrix = new Matrix();

    if(type == Direction.VERTICAL) {
        matrix.preScale(1.0f, -1.0f);
    }
    else if(type == Direction.HORIZONTAL) {
        matrix.preScale(-1.0f, 1.0f);
    } else {
        return src;
    }

    return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
}

这不需要创建新的位图吗?仅仅让图形层绘制相同的翻转位图不是更有效吗?是的,这需要创建一个新的位图。我还没有看到一个不需要创建新位图的解决方案。