android镜像imagebutton的src中的imageresource

android镜像imagebutton的src中的imageresource,android,android-imagebutton,Android,Android Imagebutton,我以编程方式设置ImageButton的ImageResource,而ImageButton本身是用xml创建的: <ImageButton android:id="@+id/id_of_image_button" android:layout_width="88dp" android:layout_height="88dp" android:layout_below="@+id

我以编程方式设置ImageButton的ImageResource,而ImageButton本身是用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" />

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

如果要在代码中翻转它,可以执行以下操作:

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);
}
如果要在X轴上镜像,可以切换
预缩放值。

试试这个

假设要在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);
} 
注意:
mirrorY
使用
矩阵。postConcat()
生成关于Y轴的镜像。

将此方法添加到代码中

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)
        )
    );
结果:

[编辑]

您可以使用以下矩阵获得垂直镜像:
mtx.preScale(1,-1)

您可以使用以下矩阵获得水平+垂直镜像:
mtx.preScale(-1,-1)

我不太明白这个问题。是否要使用相同的图像资源创建第二个图像按钮?啊,抱歉。不,我有一个可绘制的,显示在两个ImageButton上,但其中一个必须显示镜像的资源。现在我明白了,你可以用matrixOk来实现这一点!您有两种可能的解决方案:如果图像是静态的,可以使用图像编辑器将其翻转。如果图像源可以更改,则需要应用类似矩阵的变换,这对设备非常重要;矩阵旋转后(180);ib.setImageMatrix(矩阵)
ib.setImageResource(图像按钮的R.drawable.src_)之后-不起作用:(复制/粘贴并崩溃
java.lang.IllegalArgumentException:y+高度必须是我修复的错误。并添加了结果的演示图片。原始图像是左边的头部,“镜像”图像是右边的。我喜欢你!工作正常:)我通过
ib.setImageBitmap(null)重置位图bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.ic\u action\u search)!接下来,您需要从刚才使用
drawable d=new-bitmappdrawable(getResources(),bitmap)创建的位图创建一个drawable!现在您有了资源的DrawableBitmap,您可以调用我提供的方法,该方法将镜像您的资源。希望它能为你工作,并为缺乏良好的格式感到抱歉:P
    final ImageView imgMirror = (ImageView) findViewById(R.id.imgMirror);
    imgMirror.setImageBitmap
    (
        makeImageMirror
        (
            BitmapFactory.decodeResource(getResources(), R.drawable.head_prof)
        )
    );