【问题标题】:Rotate image left/right by an angle on button click单击按钮时将图像向左/向右旋转一个角度
【发布时间】:2016-09-01 10:32:14
【问题描述】:

使用 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;
                }
            }

【问题讨论】:

  • 你的意思是按钮点击只能工作一次,每次都应该旋转?

标签: android image-rotation


【解决方案1】:

也许实例变量有帮助。

像这样:

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

【讨论】:

    【解决方案2】:
    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);
    }
    

    使用此代码旋转图像.. 并在您的按钮单击时调用此方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      相关资源
      最近更新 更多