设置图片大小和旋转

xml

<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/left"
android:scaleType="fitCenter"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拖动改变图片大小"
android:textSize="30px"
/>
<SeekBar
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"

/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拖动旋转图片"
android:textSize="30px"
/>
<SeekBar
android:id="@+id/sb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"

/>
有两个seekbar
sb实现setOnSeekBarChangeListener(this);接口
sb1实现setOnSeekBarChangeListener(this);接口
拖动滚动条来缩放的思想是这样的,首先要把sb的最大值设置成屏幕宽度
然后通过拖动的返回值来改变imagview容器的大小就可以改变图片的大小
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm); //获得屏幕的宽度
int w = dm.widthPixels;
sb.setMax(w); //设置sb的最大值


拖动滚动条来旋转的思想首先要声明一个Matrix matrix = new Matrix();

对图片的操作都用这个类。设置他旋转的度数,然后装载资源,创建一个旋转后的图片
将这个图片设置成旋转之后的图片。
对图片的旋转并不是真的让这个图片旋转,而是新或得一个图片然后让这个图片旋转,把这个旋转后的图片替换成img

 


就会重写里面的方法 对象 拖动值
public void onProgressChanged(SeekBar seekbar, int pro, boolean arg2) {
int a=seekbar.getId;
switch(a)
case R.id.sb:
int w = pro;
int h = pro*3/4;
LinearLayout.LayoutParams pra = new LinearLayout.LayoutParams(w, h);
img.setLayoutParams(pra);
break;

case R.id.sb1:
matrix.setRotate(pro);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.left);
Bitmap bit = bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);//创建一个旋转后的图片,返回值为bitmap要创建一个新的 bitmap来接收
img.setImageBitmap(bit); 将旋转后的图片替换成这个图片
break;

}

 

posted @ 2015-10-11 17:39  aaddrrooiidd  阅读(302)  评论(0编辑  收藏  举报