通过SeekBar对ImageView进行缩放,旋转

来源:互联网 发布:excel找出a列重复数据 编辑:程序博客网 时间:2024/06/05 21:17
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <ImageView
        android:id="@+id/iv"
        android:layout_width="200dp"
        android:layout_height="150dp"
        android:src="@drawable/psb" />


    <TextView
        android:id="@+id/tv_tr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="图片高度150.宽度200" />


    <SeekBar
        android:id="@+id/sb1"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:max="240"
        android:progress="120" />

    <TextView
        android:id="@+id/tv_sc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="0度" />

    <SeekBar
        android:id="@+id/sb2"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:max="360" />

</LinearLayout>


public class MainActivity extends Activity implements OnSeekBarChangeListener {
// private int minWidth = 20;
private ImageView iv;
private TextView tv1, tv2;    

      private Matrix matrix = new Matrix();
private SeekBar sb1, sb2;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView) this.findViewById(R.id.iv);
tv1 = (TextView) this.findViewById(R.id.tv_tr);
tv2 = (TextView) this.findViewById(R.id.tv_sc);
sb1 = (SeekBar) this.findViewById(R.id.sb1);
sb2 = (SeekBar) this.findViewById(R.id.sb2);
sb1.setOnSeekBarChangeListener(this);
sb2.setOnSeekBarChangeListener(this);
// DisplayMetrics可以得到分辨率等信息.widthPixels屏幕宽.heightPixels 屏幕高 .density 屏幕密度
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

                //设置缩放最大不能超过当前屏幕的宽度
sb1.setMax(dm.widthPixels);// - minWidth);

}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
switch (seekBar.getId()) {
case R.id.sb1:
int newWidth = progress;// + minWidth;
int newHeigth = newWidth * 3 / 4;
iv.setLayoutParams(new LinearLayout.LayoutParams(newWidth, newHeigth));
tv1.setText("图像宽" + newWidth + "图像高" + newHeigth);
break;
case R.id.sb2:
Bitmap bitmap = ((BitmapDrawable) (getResources().getDrawable(R.drawable.psb))).getBitmap();
matrix.setRotate(progress);
//从原始位图剪切图像,这是一种高级的方式。可以用Matrix(矩阵)来实现旋转等高级方式截图
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
iv.setImageBitmap(bitmap);

tv2.setText(progress + "度");
break;
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
}


0 0
原创粉丝点击