2

我以编程方式设置 ImageButton 的 ImageResource,而它本身是在 xml 中创建的:

        <ImageButton
            android:id="@+id/id_of_image_button"
            android:layout_width="88dp"
            android:layout_height="88dp"
            android:layout_below="@+id/id_of_other_image_button"
            android:layout_centerHorizontal="true"
            android:background="@drawable/background_of_image_button"
            android:contentDescription="@string/description_of_image_button"
            android:onClick="onButtonClick"
            android:scaleType="fitCenter" />

在java中我设置了src(取决于其他代码......)

    ImageButton ib = (ImageButton) findViewById(R.id.id_of_image_button);
    ib.setImageResource(R.drawable.src_of_image_button);

如何镜像 ImageResource(仅限 src,而不是背景)?是否有任何解决方案(在 Java/XML 中)不会破坏简单的代码?;)

4

3 回答 3

2

将此方法添加到您的代码中

private final static Bitmap makeImageMirror(final Bitmap bmp)
{
    final int width = bmp.getWidth();
    final int height = bmp.getHeight();

    // This will not scale but will flip on the X axis.
    final Matrix mtx = new Matrix();
    mtx.preScale(-1, 1);

    // Create a Bitmap with the flip matrix applied to it.
    final Bitmap reflection = Bitmap.createBitmap(bmp, 0, 0, width, height, mtx, false);

    // Create a new Canvas with the bitmap.
    final Canvas cnv = new Canvas(reflection);

    // Draw the reflection Image.
    cnv.drawBitmap(reflection, 0, 0, null);

    //
    final Paint pnt = new Paint(Paint.ANTI_ALIAS_FLAG);
    // Set the Transfer mode to be porter duff and destination in.
    pnt.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

    // Draw a rectangle using the paint.
    cnv.drawRect(0, 0, width, height, pnt);

    return reflection;
}

然后,像这样得到你的镜像:

    final ImageView imgMirror = (ImageView) findViewById(R.id.imgMirror);
    imgMirror.setImageBitmap
    (
        makeImageMirror
        (
            BitmapFactory.decodeResource(getResources(), R.drawable.head_prof)
        )
    );

结果:

在此处输入图像描述

[编辑]

您可以使用此矩阵获得 VERTICAL 镜像:mtx.preScale(1, -1);
您可以使用此矩阵获得 HORIZONTAL + VERTICAL 镜像:mtx.preScale(-1, -1);

于 2014-04-23T11:42:38.007 回答
0

如果你想在代码中翻转它,你可以这样做:

BitmapDrawable flip(BitmapDrawable d)
{
    Matrix m = new Matrix();
    m.preScale(-1, 1);
    Bitmap src = d.getBitmap();
    Bitmap dst = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, false);
    dst.setDensity(DisplayMetrics.DENSITY_DEFAULT);
    return new BitmapDrawable(dst);
}

preScale如果要在 X 轴上镜像,可以切换值。

于 2014-04-23T11:35:30.987 回答
0

试试这个

假设您要在 Y 轴上镜像

private void mirrorMatrix(){
       float[] mirrorY = {  -1, 0, 0, 
                       0, 1, 0,  
                       0, 0, 1    
                   };
       matrixMirrorY = new Matrix();
       matrixMirrorY.setValues(mirrorY);    
   }

然后获取镜像位图并将其设置为下一个imageButton

private void drawMatrix()
{

  Matrix matrix = new Matrix();
  matrix.postConcat(matrixMirrorY); 

  Bitmap mirrorBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
  ib.setImageBitmap(mirrorBitmap);
} 

注意: mirrorYMatrix.postConcat()生成关于 Y 轴的镜像。

于 2014-04-23T11:35:39.170 回答