简体   繁体   中英

Android java : rotate image at imageview

I have try out a lot of research to rotate my imageview picture. 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 . 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM