Android圖像視圖ImageView實現(xiàn)圖像拉伸效果
本文實例為大家分享了Android圖像視圖ImageView實現(xiàn)圖像拉伸效果的具體代碼,供大家參考,具體內(nèi)容如下
在layout調(diào)整屬性src指定圖形來源。Activity中setScaleType設置圖形的拉伸類型。
MainActivity
package com.example.junior;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
// 頁面類直接實現(xiàn)點擊監(jiān)聽器的接口View.OnClickListener
public class ScaleActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView iv_scale; // 聲明一個圖像視圖的對象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scale);
// 從布局文件中獲取名叫iv_scale的圖像視圖
iv_scale = findViewById(R.id.iv_scale);
// 下面通過七個按鈕,分別演示不同拉伸類型的圖片拉伸效果
findViewById(R.id.btn_center).setOnClickListener(this);
findViewById(R.id.btn_fitCenter).setOnClickListener(this);
findViewById(R.id.btn_centerCrop).setOnClickListener(this);
findViewById(R.id.btn_centerInside).setOnClickListener(this);
findViewById(R.id.btn_fitXY).setOnClickListener(this);
findViewById(R.id.btn_fitStart).setOnClickListener(this);
findViewById(R.id.btn_fitEnd).setOnClickListener(this);
}
@Override
public void onClick(View v) { // 一旦監(jiān)聽到點擊動作,就觸發(fā)監(jiān)聽器的onClick方法
if (v.getId() == R.id.btn_center) {
// 將拉伸類型設置為“按照原尺寸居中顯示”
iv_scale.setScaleType(ImageView.ScaleType.CENTER);
} else if (v.getId() == R.id.btn_fitCenter) {
// 將拉伸類型設置為“保持寬高比例,拉伸圖片使其位于視圖中間”
iv_scale.setScaleType(ImageView.ScaleType.FIT_CENTER);
} else if (v.getId() == R.id.btn_centerCrop) {
// 將拉伸類型設置為“拉伸圖片使其充滿視圖,并位于視圖中間”
iv_scale.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else if (v.getId() == R.id.btn_centerInside) {
// 將拉伸類型設置為“保持寬高比例,縮小圖片使之位于視圖中間(只縮小不放大)”
iv_scale.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
} else if (v.getId() == R.id.btn_fitXY) {
// 將拉伸類型設置為“拉伸圖片使其正好填滿視圖(圖片可能被拉伸變形)”
iv_scale.setScaleType(ImageView.ScaleType.FIT_XY);
} else if (v.getId() == R.id.btn_fitStart) {
// 將拉伸類型設置為“保持寬高比例,拉伸圖片使其位于視圖上方或左側(cè)”
iv_scale.setScaleType(ImageView.ScaleType.FIT_START);
} else if (v.getId() == R.id.btn_fitEnd) {
// 將拉伸類型設置為“保持寬高比例,拉伸圖片使其位于視圖下方或右側(cè)”
iv_scale.setScaleType(ImageView.ScaleType.FIT_END);
}
}
}
layout
<?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_scale"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:src="@drawable/apple1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_fitCenter"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="fitCenter"
android:textColor="#000000"
android:textSize="11sp" />
<Button
android:id="@+id/btn_centerCrop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="centerCrop"
android:textColor="#000000"
android:textSize="11sp" />
<Button
android:id="@+id/btn_centerInside"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="centerInside"
android:textColor="#000000"
android:textSize="11sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="center"
android:textColor="#000000"
android:textSize="11sp" />
<Button
android:id="@+id/btn_fitXY"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="fitXY"
android:textColor="#000000"
android:textSize="11sp" />
<Button
android:id="@+id/btn_fitStart"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="fitStart"
android:textColor="#000000"
android:textSize="11sp" />
<Button
android:id="@+id/btn_fitEnd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="fitEnd"
android:textColor="#000000"
android:textSize="11sp" />
</LinearLayout>
</LinearLayout>
result

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 中基于TabLayout+ViewPager實現(xiàn)標簽卡效果
這篇文章主要介紹了Android 中基于TabLayout+ViewPager實現(xiàn)標簽卡效果,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-12-12
Android中WebView實現(xiàn)點擊超鏈接啟動QQ的方法
這篇文章主要給大家介紹了在Android中WebView如何實現(xiàn)點擊超鏈接啟動QQ的方法,文中給出了詳細的示例代碼,相信對大家的學習或者工作具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04
Android自定義Seekbar滑動條 Pop提示跟隨滑動按鈕滑動
這篇文章主要為大家詳細介紹了Android自定義Seekbar滑動條,Pop提示跟隨滑動按鈕滑動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
Android使用Scroller實現(xiàn)彈性滑動效果
這篇文章主要介紹了Android使用Scroller實現(xiàn)彈性滑動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android開發(fā)中類加載器DexClassLoader的簡單使用講解
這篇文章主要介紹了Android開發(fā)中類加載器DexClassLoader的簡單使用講解,DexClassLoader可以看作是一個特殊的Java中的ClassLoader,需要的朋友可以參考下2016-04-04
Android開源項目PullToRefresh下拉刷新功能詳解
這篇文章主要為大家詳細介紹了Android開源項目PullToRefresh下拉刷新功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Android自定義view實現(xiàn)側(cè)滑欄詳解
之前一直沒有寫側(cè)滑菜單的實現(xiàn)方法,今天計劃補上。手機開發(fā)中,往往存在很多功能沒處放的問題。我們可能會把功能放入一個菜單列表,但現(xiàn)在一種流行的做法是側(cè)滑菜單2022-11-11

