Android java:在imageview上旋转图像

时间:2014-03-16 10:25:38

标签: java android rotation android-imageview

我已经尝试了很多研究来旋转我的imageview图片。还是行不通 。 贝娄有我的应用程序屏幕拍摄。我希望顺时针旋转。有没有想过这样做? 我编辑了以前的代码,这是当前的代码:

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


            }
        });      

以下链接是我的应用的图片: https://plus.google.com/u/0/photos?pid=5991339872494743442&oid=114816496239615013742 你可以看到锡实际上是水平的。我希望当我按下旋转按钮时,它可以变成垂直。

1 个答案:

答案 0 :(得分:0)

您可以使用此类旋转图像:

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

然后:

 //global var
static RotateImageView rotateimageview ;
 //onCreate() function
rotateimageview = new RotateImageView(getActivity());
 //inside your function(ex. onClickListener())
 rotateimageview.startAnimation();
 //or
 rotateimageview.stopAnimation();

你的layout.xml:

    <yourname.RotateImageView 
        android:id="@+id/rotateimageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/yourimage"
        />