android镜像imagereston的src中的imageresource

时间:2014-04-23 11:16:28

标签: android android-imagebutton

我以编程方式设置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中)不会破坏简单的代码? ;)

3 个答案:

答案 0 :(得分:1)

将此方法添加到您的代码

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)
        )
    );

结果:

enter image description here

<强> [编辑]

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

答案 1 :(得分: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);
}

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

答案 2 :(得分: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);
} 

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