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ù)補充相關(guān)知識,謝謝大家對本站的支持!
相關(guān)文章
Android模擬開關(guān)按鈕點擊打開動畫(屬性動畫之平移動畫)
這篇文章主要介紹了Android模擬開關(guān)按鈕點擊打開動畫(屬性動畫之平移動畫)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
Android開發(fā)之ViewSwitcher用法實例
這篇文章主要介紹了Android開發(fā)之ViewSwitcher用法,結(jié)合實例形式分析了ViewSwitcher的功能、使用方法與相關(guān)注意事項,需要的朋友可以參考下2016-02-02
Android getViewById和getLayoutInflater().inflate()的詳解及比較
這篇文章主要介紹了Android getViewById和getLayoutInflater().inflate()的詳解及比較的相關(guān)資料,這里對這兩種方法進行了詳細的對比,對于開始學(xué)習(xí)Android的朋友使用這兩種方法是個很好的資料,需要的朋友可以參考下2016-11-11
Android UI組件LinearLayout線性布局詳解
這篇文章主要為大家詳細介紹了AndroidUI組件LinearLayout線性布局,具有一定的實用性,感興趣的小伙伴們可以參考一下2016-08-08
將替代ListView的RecyclerView 的使用詳解(一)
這篇文章主要介紹了將替代ListView的RecyclerView 的使用詳解(一)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07

