如何在 android 中旋转搜索栏上的 imageView
How to rotate an imageView on seekbar in android
我做了一个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);
}
}
您忘记将 OnSeekBarChangeListener 附加到您的 SeekBar:
void init() {
iv_image = (ImageView) findViewById(R.id.iv_image);
seekbar = (SeekBar) findViewById(R.id.seekbar);
seekbar.setOnSeekBarChangeListener(this); // <-----
}
我建议你使用 Square 的 Picasso 库。这个需求在这里会简单明了
我做了一个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);
}
}
您忘记将 OnSeekBarChangeListener 附加到您的 SeekBar:
void init() {
iv_image = (ImageView) findViewById(R.id.iv_image);
seekbar = (SeekBar) findViewById(R.id.seekbar);
seekbar.setOnSeekBarChangeListener(this); // <-----
}
我建议你使用 Square 的 Picasso 库。这个需求在这里会简单明了