[英]Android java : rotate image at imageview
I have try out a lot of research to rotate my imageview picture. 我已经尝试了很多研究来旋转我的imageview图片。 Still not working . 还是行不通 。 Bellow have my app screen shoot . 贝娄有我的应用屏幕截图。 I wish to have clockwise rotation. 我希望顺时针旋转。 Any idea to do that ? 有什么想法吗? I have edit the previous code and this is the current code: 我已经编辑了之前的代码,这是当前的代码:
imb_rotate = (ImageButton) findViewById(R.id.imb_rotate);
imb_rotate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Matrix matrix=new Matrix();
imgView.setScaleType(ScaleType.MATRIX); //required
matrix.postRotate(90);
imgView.setImageMatrix(matrix);
}
});
The following link is the image of my apps: https://plus.google.com/u/0/photos?pid=5991339872494743442&oid=114816496239615013742 as you can see the tin are actually horizontal . 以下链接是我的应用程序的图像: https : //plus.google.com/u/0/photos?pid=5991339872494743442&oid=114816496239615013742正如您所看到的,锡纸实际上是水平的。 I wish when I press the rotate button it can become vertical. 我希望当我按下旋转按钮时,它可以变为垂直。
You can use this class to rotate the image: 您可以使用此类旋转图像:
public class RotateImageView extends ImageView {
private static Animation mRotation;
public boolean isAnimating = false;
public RotateImageView(Context context) {
super(context);
Init(null);
}
public RotateImageView(Context context, AttributeSet attrs) {
super(context, attrs);
Init(attrs);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public RotateImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Init(attrs);
}
private void Init(AttributeSet attrs) {
startAnimation();
}
public void startAnimation() {
if(!isAnimating){
if (mRotation == null) {
mRotation = AnimationUtils.loadAnimation(getContext(), R.anim.rotate);
mRotation.setRepeatCount(Animation.INFINITE);
}
this.startAnimation(mRotation);
isAnimating = true;
}
}
public void stopAnimation() {
if (isAnimating){
mRotation.cancel();
isAnimating = false;
}
}
@Override
public void setVisibility(int visibility) {
if (visibility == GONE || visibility == INVISIBLE) {
this.clearAnimation();
} else if (visibility == VISIBLE) {
this.startAnimation(mRotation);
}
super.setVisibility(visibility);
}
}
then : 然后 :
//global var
static RotateImageView rotateimageview ;
//onCreate() function
rotateimageview = new RotateImageView(getActivity());
//inside your function(ex. onClickListener())
rotateimageview.startAnimation();
//or
rotateimageview.stopAnimation();
your layout.xml : 您的layout.xml:
<yourname.RotateImageView
android:id="@+id/rotateimageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/yourimage"
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.