我需要在渲染脚本中旋转图像,并且我有以下代码:
private ScriptC_flip mScript;
Button flip = (Button)view.findViewById(R.id.flipVertical);
flip.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mScript.set_direction(1);
flip();
}
});
mBitmapIn = loadBitmap(R.drawable.face2);
in = (ImageView) view.findViewById(R.id.displayin);
in.setImageBitmap(mBitmapIn);
createScript();
需要以下功能:
protected void flip() {
mScript.invoke_filter();
mOutAllocation.copyTo(mBitmapIn);
mRS.destroy();
mInAllocation.destroy();
mOutAllocation.destroy();
mScript.destroy();
createScript();
in.invalidate();
}
private void createScript() {
mRS = RenderScript.create(getActivity());
mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn,
Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
mOutAllocation = Allocation.createTyped(mRS, mInAllocation.getType());
mScript = new ScriptC_flip(mRS, getResources(), R.raw.flip);
mScript.set_width(mBitmapIn.getWidth());
mScript.set_height(mBitmapIn.getHeight());
mScript.set_gIn(mInAllocation);
mScript.set_gOut(mOutAllocation);
mScript.set_gScript(mScript);
}
private Bitmap loadBitmap(int resource) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
return BitmapFactory.decodeResource(getResources(), resource, options);
}
这是我的 RenderSCript 代码:
#pragma version(1)
#pragma rs java_package_name(com.example.android.rs.hellocompute)
rs_allocation gIn;
rs_allocation gOut;
rs_script gScript;
int width;
int height;
int direction = 0; // 0 - flip horizontally, 1 - flip vertically
float rotation;
void init(){
rotation = 0.0f;
}
void root(const uchar4 *v_in, uchar4 *v_out, const void *usrData, uint32_t x, uint32_t y) {
if(direction == 4){ // rotate right
const uchar4 *element = rsGetElementAt(gIn, x, y);
float4 color = rsUnpackColor8888(*element);
float4 output = {color.r, color.g, color.b};
*v_out = rsPackColorTo8888(output);
rs_matrix4x4 matrix;
rsMatrixLoadIdentity(&matrix);
rsMatrixTranslate(&matrix, 100.0f, 100.0f, 0.0f);
rsMatrixRotate(&matrix, rotation++, 0.0f, 0.0f, 1.0f);
// rsgProgramVertexLoadModelMatrix(&matrix);
}else if(direction == 5){ // rotate right
const uchar4 *element = rsGetElementAt(gIn, y, height - x);
float4 color = rsUnpackColor8888(*element);
float4 output = {color.r, color.g, color.b};
*v_out = rsPackColorTo8888(output);
}
}
void filter() {
rsForEach(gScript, gIn, gOut, 0);
}
如果我尝试对这一行进行注释:
// rsgProgramVertexLoadModelMatrix(&matrix);
我收到此方法不存在的错误。为什么会这样?我在其他渲染脚本示例中使用了它。唯一的区别是那里我有一个 RSSurfaceView,在这里,我将结果设置在图像视图上。现在我怎样才能让它旋转?如果我将“方向”设置为 5,那么它会向右旋转 90 度。如果我尝试使用“direction”= 4,它不会做任何事情。我从一个例子中得到了这个,它会一遍又一遍地旋转一个网格