`
阅读更多

 使用ImageView旋转图片或缩放图像

 

Bitmap bm = ((BitmapDrawable)getResources().getDrawable(R.drawable.ic_launcher)).getBitmap();
		Matrix matrix = new Matrix();
		//matrix.setScale((float)1.5, (float)1.5);//放大1.5倍
		matrix.setRotate(-45);//旋转-45度
		
		Bitmap newBm = Bitmap.createBitmap(bm,0,0,bm.getWidth(),bm.getHeight(),matrix,true);
		
		iv.setImageBitmap(newBm);

 

 如上面代码所示:使用ImageView旋转或缩放图片的时候,首先你需要将图片转为Bitmap,对Bitmap进行旋转或缩放(通过设置Matrix),最后调用createBitmap来得到处理后的图片,利用ImageView显示出来。

注意这里setScale和setRotate不能同时起作用。

 

让ImageView显示部分图像

 

方法一:

重新创建Bitmap

Bitmap bm = ((BitmapDrawable)getResources().getDrawable(R.drawable.ic_launcher)).getBitmap();
		Bitmap newBm = Bitmap.createBitmap(bm,0,0,50,50);//截取某一部分的像素显示(从图片的左上角开始,向右向下各50个像素)
		iv.setImageBitmap(newBm);

 

方法二:

使用图像剪切资源

1.首先在res/drawable中建立clip_drawable.xml:(命名可以随意,只要符合规范即可)

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="horizontal|vertical"
    android:drawable="@drawable/ic_launcher"
    android:gravity="center">

</clip>

 通过配置clipOrientation和gravity两个属性,可以实现对图的灵活剪切,比如上面的配置,实现的是对图的上下左右同时剪切相同的比例,至于剪切的比例,要在代码中控制。

 

2.先看看布局文件中的ImageView:(这里引用的是上面定义的clip_drawable.xml)

<ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
	android:src="@drawable/clip_drawable"
         />

 

3.再看代码:

ClipDrawable clipDrawable = (ClipDrawable)iv.getDrawable();
clipDrawable.setLevel(3000);//设置截取图像的30%(100%是10000)
iv.setImageDrawable(clipDrawable);

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics