java xy轴_java – 在X轴或Y轴上翻转Drawable

这似乎是一个愚蠢的问题,但我看不到使用Drawable类中的方法做任何方法.然后我想也许我不得不以某种方式翻转画布……仍然找不到合适的方法.

我只需要在它的y轴上“翻转”Drawable …中心y最好.我怎样才能做到这一点?

解决方法:

从10k英尺的高度,您需要创建一个新的位图并指定一个转换矩阵来翻转位图.

这可能有点矫枉过正,但这里有一个小样本应用程序,说明了如何执行此操作.如上所述,变换矩阵预分频(-1.0f,1.0f)在x方向上翻转图像,预分频(1.0f,-1.0f)将在y方向上翻转它.

public class flip extends Activity{

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//Set view to our created view

setContentView(new drawView(this));

}

private class drawView extends View{

public drawView(Context context){

super(context);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

//Load the jellyfish drawable

Bitmap sprite = BitmapFactory.decodeResource(this.getResources(), R.drawable.jellyfish);

//Create a matrix to be used to transform the bitmap

Matrix mirrorMatrix = new Matrix();

//Set the matrix to mirror the image in the x direction

mirrorMatrix.preScale(-1.0f, 1.0f);

//Create a flipped sprite using the transform matrix and the original sprite

Bitmap fSprite = Bitmap.createBitmap(sprite, 0, 0, sprite.getWidth(), sprite.getHeight(), mirrorMatrix, false);

//Draw the first sprite

canvas.drawBitmap(sprite, 0, 0, null);

//Draw the second sprite 5 pixels to the right of the 1st sprite

canvas.drawBitmap(fSprite, sprite.getWidth() + 5, 0, null);

}

}

}

标签:java,android,drawable

来源: https://codeday.me/bug/20190610/1212003.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值