我想从imageView创建一个位图,但我首先需要在转换位图之前旋转我的imageView。我使用以下代码将imageView转换为Bitmap。
BitmapDrawable drawable = (BitmapDrawable) imgPhoto.getDrawable();
Bitmap photo = drawable.getBitmap();
imageView实现了一个旋转手势。我希望能够旋转imageView,然后使用旋转的imageView创建一个位图。但是,似乎我的Bitmap仍然是原始的imageView而不是旋转的位置。以下是完整的代码。谢谢。
public class MainActivity extends AppCompatActivity implements RotationGestureDetector.OnRotationGestureListener{
private ImageView imgPhoto, imgBackground, imgCombine;
private RotationGestureDetector mRotationDetector;
private Button btnCombine;
private float angle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRotationDetector = new RotationGestureDetector(this);
imgPhoto = (ImageView) findViewById(R.id.imgPhoto);
imgBackground = (ImageView) findViewById(R.id.imgBackground);
collageImage = (ImageView) findViewById(R.id.imgCombine);
btnCombine = (Button)findViewById(R.id.btnCombineImage);
btnCombine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BitmapDrawable drawable = (BitmapDrawable) imgPhoto.getDrawable();
Bitmap photo = drawable.getBitmap();
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event){
mRotationDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
@Override
public void OnRotation(RotationGestureDetector rotationDetector) {
angle = rotationDetector.getAngle();
Log.d("RotationGestureDetector", "Rotation: " + Float.toString(angle));
imgPhoto.setRotation(imgPhoto.getRotation() + (-angle));
}
}
答案 0 :(得分:0)
在onClick之后尝试使用此代码。它应该提供一个指定角度的旋转位图。
Matrix matrix = new Matrix();
matrix.postRotate(angle);
Bitmap rotatedPhoto = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
答案 1 :(得分:0)
旋转ImageView:
Studio does not have write access to /private/var/folders/8c/3ffqd8814c37fw3197jq6k0r0000gn/T/AppTranslocation/FA3D0850-457F-489F-BA09-221516148C30/d/Android Studio.app/Contents. Please run it by a privileged user to update.
来源:https://stackoverflow.com/a/10104318/7639113
然后,您可以使用DrawingCache
创建位图Matrix matrix = new Matrix();
imageView.setScaleType(ImageView.ScaleType.MATRIX); //required
matrix.postRotate((float) angle, pivotX, pivotY);
imageView.setImageMatrix(matrix);
答案 2 :(得分:0)
旋转效果实际上是在“画布”级别上应用的。因此,您可以做的就是获取当前的Canvas增强,然后将更改后的位图绘制到新的位图中。
if( align_mode == 1 ){
for( int i = 0 ; i < n_1 ; i++ ){
for( int j = 0 ; j < n_2 ; j++ ){
d_2 = pow( vStruct1[i]->R[0] - vStruct2[j]->R_sup[0], 2 ) + pow( vStruct1[i]->R[1] - vStruct2[j]->R_sup[1], 2 ) + pow( vStruct1[i]->R[2] - vStruct2[j]->R_sup[2], 2 );
score[i+1][j+1] = 1 / ( 1 + d_2 / d0_2 );
}
}
}