Android中ImageView的使用方法
更新時間:2021年05月21日 10:43:19 作者:nuist__NJUPT
這篇文章主要為大家詳細介紹了Android中ImageView的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
Android中ImageView的使用:點擊按鈕,改變圖片透明度,切換圖片
布局是三個按鈕組件和一個ImageView組件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/addAlpha" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="增加圖片透明度" /> <Button android:id="@+id/downAlpha" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="減小圖片透明度" /> <Button android:id="@+id/next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="切換圖片" /> </LinearLayout> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="280dp" android:layout_marginTop="100dp" android:scaleType="fitCenter" android:src="@drawable/spring" /> </LinearLayout>
.java文件,控制增加和減少透明度以及圖片切換
public class MainActivity extends AppCompatActivity { private Button addAlpha; private Button downAlpha; private Button next; private ImageView imageView1; int [] images = new int[]{ //用數(shù)組存儲春,夏,秋,冬四張圖片 R.drawable.spring, R.drawable.summer, R.drawable.fall, R.drawable.winter } ; int currentImage = 0 ; //定義當前默認顯示的圖片 int alpha = 255 ;//定義圖片起始透明度 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_imageview); addAlpha = (Button) findViewById(R.id.addAlpha); downAlpha = (Button) findViewById(R.id.downAlpha); next = (Button) findViewById(R.id.next); imageView1 = (ImageView) findViewById(R.id.imageView1); //對增加圖片透明度按鈕設置監(jiān)聽事件 addAlpha.setOnClickListener(new View.OnClickListener() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onClick(View v) { if(alpha >= 255){ //255是透明度上線 alpha = 255 ; } else{ //每點擊增加透明度按鈕,透明度增加20 alpha += 20 ; } imageView1.setImageAlpha(alpha); //為圖片設置透明度 } }); //對減少透明度按鈕設置監(jiān)聽事件 downAlpha.setOnClickListener(new View.OnClickListener() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onClick(View v) { if(alpha <= 0){ //透明度下限 alpha = 0 ; } else{ alpha -= 20 ; } imageView1.setImageAlpha(alpha); //為圖片設置透明度 } }); //對切換圖片按鈕設置監(jiān)聽事件 next.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //控制顯示下一張圖片 imageView1.setImageResource(images[++ currentImage % images.length]); } }); } }
效果圖如下,點擊按鈕可以改變透明度和切換圖片
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android如何創(chuàng)建自定義ActionBar
這篇文章主要教大家如何創(chuàng)建自定義的ActionBar,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11Android 模擬器(JAVA)與C++ socket 通訊 分享
Android 模擬器(JAVA)與C++ socket 通訊 分享,需要的朋友可以參考一下2013-05-05Android中wifi與數(shù)據(jù)流量的切換監(jiān)聽詳解
本文主要介紹了Android中wifi與數(shù)據(jù)流量的切換監(jiān)聽的方法步驟。具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01Android OpenGL入門之GLSurfaceView
這篇文章主要介紹了OpenGL入門知識,如何在Android中使用GLSurfaceView,如果對OpenGL感興趣的同學,可以參考下2021-04-04Android Studio4.0解決Gradle下載超時問題
這篇文章主要介紹了Android Studio4.0解決Gradle下載超時問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10詳解Android開發(fā)技巧之PagerAdapter實現(xiàn)類的封裝
這篇文章主要介紹了詳解Android開發(fā)技巧之PagerAdapter實現(xiàn)類的封裝,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11