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
});
最后運(yùn)行項目效果如下:
以上就是對Android 圖片縮放的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)知識,謝謝大家對本站的支持!
- Android仿QQ好友詳情頁下拉頂部圖片縮放效果
- Android控件實現(xiàn)圖片縮放功能
- Android實現(xiàn)手指觸控圖片縮放功能
- Android點(diǎn)擊WebView實現(xiàn)圖片縮放及滑動瀏覽效果
- Android 自定義imageview實現(xiàn)圖片縮放實例詳解
- Android實現(xiàn)ImageView圖片縮放和拖動
- Android應(yīng)用中實現(xiàn)手勢控制圖片縮放的完全攻略
- android 多點(diǎn)觸摸圖片縮放的具體實現(xiàn)方法
- Android 圖片縮放與旋轉(zhuǎn)的實現(xiàn)詳解
- Android實現(xiàn)圖片雙指縮放
相關(guān)文章
Android模擬開關(guān)按鈕點(diǎn)擊打開動畫(屬性動畫之平移動畫)
這篇文章主要介紹了Android模擬開關(guān)按鈕點(diǎn)擊打開動畫(屬性動畫之平移動畫)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09Android開發(fā)之ViewSwitcher用法實例
這篇文章主要介紹了Android開發(fā)之ViewSwitcher用法,結(jié)合實例形式分析了ViewSwitcher的功能、使用方法與相關(guān)注意事項,需要的朋友可以參考下2016-02-02Android getViewById和getLayoutInflater().inflate()的詳解及比較
這篇文章主要介紹了Android getViewById和getLayoutInflater().inflate()的詳解及比較的相關(guān)資料,這里對這兩種方法進(jìn)行了詳細(xì)的對比,對于開始學(xué)習(xí)Android的朋友使用這兩種方法是個很好的資料,需要的朋友可以參考下2016-11-11Android UI組件LinearLayout線性布局詳解
這篇文章主要為大家詳細(xì)介紹了AndroidUI組件LinearLayout線性布局,具有一定的實用性,感興趣的小伙伴們可以參考一下2016-08-08將替代ListView的RecyclerView 的使用詳解(一)
這篇文章主要介紹了將替代ListView的RecyclerView 的使用詳解(一)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07