按钮单击时向左/向右旋转图像

时间:2016-09-01 10:32:14

标签: android image-rotation

使用picasso-transformations库进行图像编辑。左旋转和右旋转有两个单独的按钮。单击按钮,图像只旋转一次。我想在每个按钮上旋转图像,单击它们各自的方向。

 recyclerView.addOnItemTouchListener(new RecyclerClick(act, recyclerView, new RecyclerClickListener() {
            @Override
            public void onClick(View view, final int position) {
                switch (position) {
                    case 0:
                        Picasso.with(act)
                                .load(selectedPhotoUri)
                                .rotate(90f)
                                .into(photo);
                        break;
                    case 1:
                        Picasso.with(act)
                                .load(selectedPhotoUri)
                                .rotate(90f)
                                .into(photo);
                        break;
                }
            }

2 个答案:

答案 0 :(得分:2)

也许实例变量有帮助。

像这样:

recyclerView.addOnItemTouchListener(new RecyclerClick(act, recyclerView, new RecyclerClickListener() {

    int rotate = 0;

    @Override
    public void onClick(View view, final int position) {
        switch (position) {
            case 0:
                rotate += 90f
                break;
            case 1:
                rotate -= 90f;
                break;
        }
        Picasso.with(act)
                .load(selectedPhotoUri)
                .rotate(rotate)
                .into(photo);
    }
}));

答案 1 :(得分:1)

public static Image rotate(Image img, double angle) {
double sin = Math.abs(Math.sin(Math.toRadians(angle))),
       cos = Math.abs(Math.cos(Math.toRadians(angle)));

int w = img.getWidth(null), h = img.getHeight(null);

int neww = (int) Math.floor(w*cos + h*sin),
    newh = (int) Math.floor(h*cos + w*sin);

BufferedImage bimg = toBufferedImage(getEmptyImage(neww, newh));
Graphics2D g = bimg.createGraphics();

g.translate((neww-w)/2, (newh-h)/2);
g.rotate(Math.toRadians(angle), w/2, h/2);
g.drawRenderedImage(toBufferedImage(img), null);
g.dispose();

return toImage(bimg);
}

使用此代码旋转图像..并在按钮上单击

调用此方法