Android 圖片縮放實例詳解
本文實現(xiàn)Android中的圖片的縮放效果
首先設(shè)計布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/iv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/iv_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
邏輯代碼如下:
public class MainActivity extends Activity {
private ImageView iv1;
private ImageView iv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv1 = (ImageView) findViewById(R.id.iv_1);
iv2 = (ImageView) findViewById(R.id.iv_2);
// 設(shè)置第一個bitmap的圖標(biāo)
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
iv1.setImageBitmap(bitmap1);
// 新建一個bitmap
Bitmap alterBitmap = Bitmap.createBitmap(bitmap1.getWidth(),
bitmap1.getHeight(), bitmap1.getConfig());
// 以alterBitmap為模板新建畫布
Canvas canvas = new Canvas(alterBitmap);
// 新建畫筆并設(shè)置屬性
Paint paint = new Paint();
paint.setColor(Color.BLACK);
//新建矩陣并設(shè)置縮放值
Matrix matrix = new Matrix();
matrix.setValues(new float[] {
0.5f, 0, 0,
0, 1, 0,
0, 0, 1
});
//設(shè)置畫布
canvas.drawBitmap(bitmap1, matrix, paint);
iv2.setImageBitmap(alterBitmap);
}
}
如果你對矩陣的設(shè)置不清楚,還可以使用下列api提供的方法替換上面標(biāo)記部分的代碼:
matrix.setScale(0.5f, 1);
注意: 新建矩陣并設(shè)置縮放值
Matrix matrix = new Matrix();
matrix.setValues(new float[] {
0.5f, 0, 0,
0, 1, 0,
0, 0, 1
});
最后運行項目效果如下:

以上就是對Android 圖片縮放的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)知識,謝謝大家對本站的支持!
相關(guān)文章
快速解決fragment中onActivityResult不調(diào)用的問題
下面小編就為大家?guī)硪黄焖俳鉀Qfragment中onActivityResult不調(diào)用的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
新版Android studio導(dǎo)入微信支付和支付寶官方Demo問題解決大全
這篇文章主要為大家詳細(xì)介紹了新版Android studio導(dǎo)入微信支付和支付寶官方Demo問題的解決大全,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07
Android折疊式Toolbar使用完全解析(CollapsingToolbarLayout)
這篇文章主要為大家詳細(xì)介紹了Android折疊式Toolbar的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
Android第三方文件選擇器aFileChooser使用方法詳解
這篇文章主要介紹了Android第三方文件選擇器aFileChooser的使用方法詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
Android文本視圖TextView實現(xiàn)跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了Android文本視圖TextView實現(xiàn)跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05
Android 實現(xiàn)長按彈出PopupMenu 菜單欄
這篇文章主要介紹了Android 實現(xiàn)長按彈出PopupMenu 菜單欄,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12

