在gridview中旋转图像

时间:2012-05-02 20:16:13

标签: android image gridview rotation

我正在使用gridview http://developer.android.com/resources/tutorials/views/hello-gridview.html的这个示例,只需少量图像和一个角落的旋转按钮。我想点击图片,然后点击旋转按钮旋转该图片,但我找不到怎么做。

有人有任何想法吗?

1 个答案:

答案 0 :(得分:1)

如果您跟踪要旋转的图像,可以使用getView中的setImageMatrix()来旋转图像。像这样:

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

有一点需要注意。在触摸模式下,列表和网格本身没有选择,但可以检查它们。获取网格视图以显示已检查状态有点棘手,即Honeypad Tutorial显示如何在列表视图中使用选中的视图,这可能会有所帮助。