1

I'm using this example of gridview http://developer.android.com/resources/tutorials/views/hello-gridview.html just with less images and a rotate button in one corner. I want to click a image, then click rotate button to rotate that image, but I can't find how to do it.

Anyone have any idea?

4
  • have you tried rotating the View using an Animation object?
    – slayton
    May 2, 2012 at 20:31
  • Do you want the rotation to the image to be permanent? In other words do you want it to affect the image file? May 2, 2012 at 20:55
  • @slayton No I haven't try the animation will try that one.
    – R-Roadster
    May 2, 2012 at 21:34
  • @Peter Ajtai No I don't want it to affect the file.
    – R-Roadster
    May 2, 2012 at 21:35

1 Answer 1

1

Rotating the image can be done using setImageMatrix() within getView if you keep track of which images you want rotated. Something like this:

HashMap<Integer, Matrix> mImageTransforms = new HashMap<Integer,Matrix>();
Matrix mIdentityMatrix = new Matrix();

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    Matrix m = mImageTransforms.get(position);
    if ( null == m ) {
        m = mIdentityMatrix;
    }
    imageView.setImageMatrix(m);
    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

void setImageRotation(int position, int degrees) {
    Matrix m = mImageTransforms.remove(position);        
    if ( degrees != 0 ) {
        if ( null == m ) m = new Matrix();
        m.setRotate(degrees);
        mImageTransforms.put(position, m);
    }
    notifyDataSetChanged();
}

One thing to note. In touch mode lists and grids do not have a selection per-se, however they can be checked. It's a bit tricky to get a grid view to show the checked state, that being said the Honeypad Tutorial shows how to use checked views within a list view which may be helpful.

1
  • ok let me see if I understand your code, the void setImageRotation(int position, int degrees) should be called with the rotate button?
    – R-Roadster
    May 3, 2012 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.