如何在android中的搜索栏上旋转imageView

时间:2015-04-10 09:53:13

标签: android android-imageview image-rotation

我已经在seekbar progresschange中制作了一个用于图像旋转的演示项目。我已经成功地完成了按钮更改,但它不适用于更换搜索栏。这是我试过的:

XML

<ImageView
        android:id="@+id/iv_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/enr" />

    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:max="360"
        android:padding="1dp"
        android:progress="0" />

    <Button
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/seekbar"
        android:layout_centerHorizontal="true"
        android:text="Rotate" />

public class MainActivity extends Activity implements OnSeekBarChangeListener {

    ImageView iv_image;
    SeekBar seekbar;
    Bitmap source;
    float angle = 0;
    Button buttonClick;
    Bitmap rotatedImage;
    static Matrix matrix;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        buttonClick = (Button) findViewById(R.id.bt);
        buttonClick.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                angle += 90;
                iv_image.buildDrawingCache();
                source = iv_image.getDrawingCache();
                rotatedImage = rotateImage(source, angle);
                iv_image.setImageBitmap(rotatedImage);
            }
        });
    }

    void init() {
        iv_image = (ImageView) findViewById(R.id.iv_image);
        seekbar = (SeekBar) findViewById(R.id.seekbar);
        seekbar.setOnSeekBarChangeListener(this);

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub

        angle = progress;
        iv_image.buildDrawingCache();
        source = iv_image.getDrawingCache();
        rotatedImage = rotateImage(source, angle);
        iv_image.setImageBitmap(null);
        iv_image.setImageBitmap(rotatedImage);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    public static Bitmap rotateImage(Bitmap sourceImage, float angle) {
        matrix = new Matrix();
        matrix.postRotate(angle);
        return Bitmap.createBitmap(sourceImage, 0, 0, sourceImage.getWidth(),
                sourceImage.getHeight(), matrix, true);

    }
}

2 个答案:

答案 0 :(得分:1)

您忘记将OnSeekBarChangeListener附加到您的SeekBar:

void init() {
    iv_image = (ImageView) findViewById(R.id.iv_image);
    seekbar = (SeekBar) findViewById(R.id.seekbar);
    seekbar.setOnSeekBarChangeListener(this); // <-----
} 

答案 1 :(得分:0)

我建议您使用Square的Picasso库。对于这种需求,这将更简单直接