您的位置:首页 > 其它

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

2015-08-15 13:18 337 查看
<?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) {
}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: