Android中imageView圖片放大縮小及旋轉(zhuǎn)功能示例代碼
一、簡介

二、方法
1)設(shè)置圖片放大縮小效果
第一步:將<ImageView>標(biāo)簽中的android:scaleType設(shè)置為"fitCenter"
android:scaleType="fitCenter"
第二步:獲取屏幕的寬度
DisplayMetrics dm=new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); dm.widthPixels
第三步:設(shè)置seekBar的最大progree值為屏幕寬度
sb_one.setMax(dm.widthPixels);
第四步:設(shè)置imageview的布局參數(shù),也就是寬和高,也就是畫布的寬高
int width=progress; int height=progress*3/4; iv_pic.setLayoutParams(new LinearLayout.LayoutParams(width, height));
2)設(shè)置圖片旋轉(zhuǎn)方法
第一步:給matrix設(shè)置角度,用于新的bitmap
private Matrix matrix; matrix.setRotate((int)(progress*3.60));
第二步:獲取bitmap資源
BitmapDrawable bitmapDrawable=(BitmapDrawable)(getResources().getDrawable(R.drawable.image1)); Bitmap bitmap=bitmapDrawable.getBitmap();
第三步:重建bitmap用于顯示
Bitmap newBitmap=bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),matrix,false);
第四步:給imageview設(shè)置新的bitmap
iv_pic.setImageBitmap(newBitmap);
三、代碼實(shí)例
效果圖:

設(shè)置大小和設(shè)置旋轉(zhuǎn)的效果圖


代碼:
fry.Activity02
package fry;
import com.example.iamgeViewDemo1.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class Activity02 extends Activity implements OnSeekBarChangeListener{
private ImageView iv_pic;
private SeekBar sb_one;
private SeekBar sb_two;
private Matrix matrix;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setTitle("imageView實(shí)現(xiàn)圖片縮放和旋轉(zhuǎn)");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity02);
iv_pic=(ImageView) findViewById(R.id.iv_pic);
sb_one=(SeekBar) findViewById(R.id.sb_one);
sb_two=(SeekBar) findViewById(R.id.sb_two);
//設(shè)置SeekBar的progress值改變監(jiān)聽事件
sb_one.setOnSeekBarChangeListener(this);
sb_two.setOnSeekBarChangeListener(this);
matrix=new Matrix();
// 1)設(shè)置圖片放大縮小效果
//
// 第一步:將<ImageView>標(biāo)簽中的android:scaleType設(shè)置為"fitCenter"
//
// 第二步:獲取屏幕的寬度
//
// 第三步:設(shè)置seekBar的最大progree值為屏幕寬度
//
// 第四步:設(shè)置imageview的布局參數(shù),也就是寬和高,也就是畫布的寬高
//設(shè)置圖片放大縮小效果
//第一步:獲取屏幕的寬度
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
//第二步:設(shè)置seekBar的最大progree值為屏幕寬度
sb_one.setMax(dm.widthPixels);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
switch (seekBar.getId()) {
case R.id.sb_one://放大或縮小
int width=progress;
int height=progress*3/4;
//第三步:設(shè)置imageview的布局參數(shù),也就是寬和高,也就是畫布的寬高
iv_pic.setLayoutParams(new LinearLayout.LayoutParams(width, height));
break;
case R.id.sb_two://旋轉(zhuǎn)
//設(shè)置旋轉(zhuǎn)度數(shù)
//設(shè)置圖片旋轉(zhuǎn)方法
//第一步:給matrix設(shè)置角度,用于新的bitmap
matrix.setRotate((int)(progress*3.60));
//第二步:獲取bitmap資源
BitmapDrawable bitmapDrawable=(BitmapDrawable)(getResources().getDrawable(R.drawable.image1));
Bitmap bitmap=bitmapDrawable.getBitmap();
//第三步:重建bitmap用于顯示
Bitmap newBitmap=bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),matrix,false);
//第四步:給imageview設(shè)置新的bitmap
iv_pic.setImageBitmap(newBitmap);
break;
default:
break;
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
activity02.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv_pic"
android:layout_width="match_parent"
android:layout_height="300dip"
android:background="@android:color/black"
android:scaleType="fitCenter"
android:src="@drawable/image1"
/>
<!-- 設(shè)置圖片的顯示方式:把圖片按比例擴(kuò)大/縮小到view的寬度,居中顯示 -->
<SeekBar
android:id="@+id/sb_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="100"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拖動來縮放圖片"
/>
<SeekBar
android:id="@+id/sb_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拖動來旋轉(zhuǎn)圖片"
/>
</LinearLayout>
四、收獲
1、設(shè)置圖像居中顯示
android:scaleType="fitCenter"
總結(jié)
以上所述是小編給大家介紹的Android中imageView圖片放大縮小及旋轉(zhuǎn)功能示例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Android自帶倒計(jì)時(shí)控件Chronometer使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android自帶倒計(jì)時(shí)控件Chronometer的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android實(shí)現(xiàn)新手引導(dǎo)半透明蒙層效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)新手引導(dǎo)半透明蒙層效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
Android天氣預(yù)報(bào)app改進(jìn)版
這篇文章主要為大家詳細(xì)介紹了改進(jìn)版的Android天氣預(yù)報(bào)app,內(nèi)容更加充實(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
Android Scroll實(shí)現(xiàn)彈性滑動_列表下拉彈性滑動的示例代碼
下面小編就為大家分享一篇Android Scroll實(shí)現(xiàn)彈性滑動_列表下拉彈性滑動的示例代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
AndroidManifest.xml <uses-feature>和<uses-permisstio
這篇文章主要介紹了AndroidManifest.xml <uses-feature>和<uses-permisstion>分析及比較的相關(guān)資料,需要的朋友可以參考下2017-06-06

