Android 圖片縮放實(shí)例詳解
本文實(shí)現(xiàn)Android中的圖片的縮放效果
首先設(shè)計(jì)布局:
<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è)置第一個(gè)bitmap的圖標(biāo) Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); iv1.setImageBitmap(bitmap1); // 新建一個(gè)bitmap Bitmap alterBitmap = Bitmap.createBitmap(bitmap1.getWidth(), bitmap1.getHeight(), bitmap1.getConfig()); // 以alterBitmap為模板新建畫(huà)布 Canvas canvas = new Canvas(alterBitmap); // 新建畫(huà)筆并設(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è)置畫(huà)布 canvas.drawBitmap(bitmap1, matrix, paint); iv2.setImageBitmap(alterBitmap); } }
如果你對(duì)矩陣的設(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)行項(xiàng)目效果如下:
以上就是對(duì)Android 圖片縮放的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)知識(shí),謝謝大家對(duì)本站的支持!
- Android仿QQ好友詳情頁(yè)下拉頂部圖片縮放效果
- Android控件實(shí)現(xiàn)圖片縮放功能
- Android實(shí)現(xiàn)手指觸控圖片縮放功能
- Android點(diǎn)擊WebView實(shí)現(xiàn)圖片縮放及滑動(dòng)瀏覽效果
- Android 自定義imageview實(shí)現(xiàn)圖片縮放實(shí)例詳解
- Android實(shí)現(xiàn)ImageView圖片縮放和拖動(dòng)
- Android應(yīng)用中實(shí)現(xiàn)手勢(shì)控制圖片縮放的完全攻略
- android 多點(diǎn)觸摸圖片縮放的具體實(shí)現(xiàn)方法
- Android 圖片縮放與旋轉(zhuǎn)的實(shí)現(xiàn)詳解
- Android實(shí)現(xiàn)圖片雙指縮放
相關(guān)文章
Android模擬開(kāi)關(guān)按鈕點(diǎn)擊打開(kāi)動(dòng)畫(huà)(屬性動(dòng)畫(huà)之平移動(dòng)畫(huà))
這篇文章主要介紹了Android模擬開(kāi)關(guān)按鈕點(diǎn)擊打開(kāi)動(dòng)畫(huà)(屬性動(dòng)畫(huà)之平移動(dòng)畫(huà))的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09Android開(kāi)發(fā)之ViewSwitcher用法實(shí)例
這篇文章主要介紹了Android開(kāi)發(fā)之ViewSwitcher用法,結(jié)合實(shí)例形式分析了ViewSwitcher的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-02-02Android 浮動(dòng)編輯框的具體實(shí)現(xiàn)代碼
本篇文章主要介紹了Android 浮動(dòng)編輯框的具體實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10android實(shí)現(xiàn)貝塞爾曲線之波浪效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)貝塞爾曲線之波浪效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Android getViewById和getLayoutInflater().inflate()的詳解及比較
這篇文章主要介紹了Android getViewById和getLayoutInflater().inflate()的詳解及比較的相關(guān)資料,這里對(duì)這兩種方法進(jìn)行了詳細(xì)的對(duì)比,對(duì)于開(kāi)始學(xué)習(xí)Android的朋友使用這兩種方法是個(gè)很好的資料,需要的朋友可以參考下2016-11-11Android UI組件LinearLayout線性布局詳解
這篇文章主要為大家詳細(xì)介紹了AndroidUI組件LinearLayout線性布局,具有一定的實(shí)用性,感興趣的小伙伴們可以參考一下2016-08-08將替代ListView的RecyclerView 的使用詳解(一)
這篇文章主要介紹了將替代ListView的RecyclerView 的使用詳解(一)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07